1/* ============================================================ 2 * retina-replace.js v1.0 3 * http://github.com/leonsmith/retina-replace-js 4 * ============================================================ 5 * Author: Leon Smith 6 * Twitter: @nullUK 7 * 8 * Licensed under the Apache License, Version 2.0 (the "License"); 9 * you may not use this file except in compliance with the License. 10 * You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, software 15 * distributed under the License is distributed on an "AS IS" BASIS, 16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 * See the License for the specific language governing permissions and 18 * limitations under the License. 19 * ============================================================ */ 20 (function($) { 21 "use strict"; 22 23 var retinaReplace = function(element, options) { 24 try { 25 26 this.options = options; 27 var $el = $(element); 28 var is_image = $el.is('img'); 29 var normal_url = is_image ? $el.attr('src') : $el.backgroundImageUrl(); 30 var retina_url = $el.attr('data-at2x') ? $el.attr('data-at2x') : '';; 31 32 if ( retina_url == '' ) { 33 if ( $el.attr('data-retina-auto') ) { 34 retina_url = this.options.generateUrl($el, normal_url); 35 } else { 36 return true; 37 } 38 } 39 40 41 $('<img/>').attr('src', retina_url).load(function() { 42 43 if (is_image) { 44 45 $el.css({ width: $el.width(), height: $el.height() }); 46 $el.attr('data-orginal-src', $el.attr('src')); 47 $el.attr('src', $(this).attr('src')); 48 49 } else { 50 51 $el.backgroundImageUrl($(this).attr('src')); 52 $el.backgroundSize($(this)[0].width, $(this)[0].height); 53 54 } 55 56 $el.attr('data-retina', 'complete'); 57 58 }); 59 60 } catch (e) {} 61 62 63 } 64 65 retinaReplace.prototype = { 66 constructor: retinaReplace 67 } 68 69 $.fn.retinaReplace = function(option) { 70 // Finish if we arn't a retina device 71 return this.each(function() { 72 var $this = $(this); 73 var data = $this.data('retinaReplace'); 74 var options = $.extend({}, $.fn.retinaReplace.defaults, $this.data(), typeof option == 'object' && option); 75 if (!data) { $this.data('retinaReplace', (data = new retinaReplace(this, options))); } 76 if (typeof option == 'string') { data[option](); } 77 }); 78 } 79 80 $.fn.retinaReplace.defaults = { 81 suffix: '@2x', 82 generateUrl: function(element, url) { 83 if ( typeof url == 'undefined' ) { 84 return ''; 85 } 86 var dot_index = url.lastIndexOf('.'); 87 var extension = url.substr(dot_index + 1); 88 var file = url.substr(0, dot_index); 89 return file + this.suffix + '.' + extension; 90 } 91 } 92 93 $.fn.retinaReplace.Constructor = retinaReplace; 94 95 $.fn.backgroundImageUrl = function(url) { 96 return url ? this.each(function () { 97 $(this).css("background-image", 'url("' + url + '")') 98 }) : $(this).css("background-image").replace(/url\(|\)|"|'/g, ""); 99 } 100 101 $.fn.backgroundSize = function(retinaWidth, retinaHeight) { 102 var sizeValue = Math.floor(retinaWidth/2) + 'px ' + Math.floor(retinaHeight/2) + 'px'; 103 $(this).css("background-size", sizeValue); 104 $(this).css("-webkit-background-size", sizeValue); 105 } 106 107 // Trigger replacement on elements that hav been marked up 108 var init = function(){ 109 $(":not([data-at2x='']):visible").retinaReplace(); 110 } 111 112 113 // Check is retina 114 var isRetina = function(){ 115 var root = (typeof exports == 'undefined' ? window : exports); 116 var mediaQuery = "(-webkit-min-device-pixel-ratio: 1.5),\ 117 (min--moz-device-pixel-ratio: 1.5),\ 118 (-o-min-device-pixel-ratio: 3/2),\ 119 (min-resolution: 1.5dppx)"; 120 121 if (root.devicePixelRatio > 1) { 122 return true; 123 } 124 125 if (root.matchMedia && root.matchMedia(mediaQuery).matches) { 126 return true; 127 } 128 129 //return true; 130 return false; 131 }; 132 133 134 if ( isRetina() ) { 135 //$(init); 136 $('html').removeClass('no-retina').addClass('retina'); 137 $(window).on('load', init ); 138 $(window).smartresize( init ); 139 $(window).bind( 'make@2x', init ); 140 } 141 142 143})(window.jQuery);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.