/*! * retina.js v1.3.0 * * copyright 2014 imulus, llc * released under the mit license * * retina.js is an open source script that makes it easy to serve * high-resolution images to devices with retina displays. */ (function() { var root = (typeof exports === 'undefined' ? window : exports); var config = { // an option to choose a suffix for 2x images retinaimagesuffix : '@2x', // ensure content-type is an image before trying to load @2x image // https://github.com/imulus/retinajs/pull/45) check_mime_type: true, // resize high-resolution images to original image's pixel dimensions // https://github.com/imulus/retinajs/issues/8 force_original_dimensions: true }; function retina() {} root.retina = retina; retina.configure = function(options) { if (options === null) { options = {}; } for (var prop in options) { if (options.hasownproperty(prop)) { config[prop] = options[prop]; } } }; retina.init = function(context) { if (context === null) { context = root; } var existing_onload = context.onload || function(){}; context.onload = function() { var images = document.getelementsbytagname('img'), retinaimages = [], i, image; for (i = 0; i < images.length; i += 1) { image = images[i]; if (!!!image.getattributenode('data-no-retina')) { retinaimages.push(new retinaimage(image)); } } existing_onload(); }; }; retina.isretina = function(){ var mediaquery = '(-webkit-min-device-pixel-ratio: 1.5), (min--moz-device-pixel-ratio: 1.5), (-o-min-device-pixel-ratio: 3/2), (min-resolution: 1.5dppx)'; if (root.devicepixelratio > 1) { return true; } if (root.matchmedia && root.matchmedia(mediaquery).matches) { return true; } return false; }; var regexmatch = /\.\w+$/; function suffixreplace (match) { return config.retinaimagesuffix + match; } function retinaimagepath(path, at_2x_path) { this.path = path || ''; if (typeof at_2x_path !== 'undefined' && at_2x_path !== null) { this.at_2x_path = at_2x_path; this.perform_check = false; } else { if (undefined !== document.createelement) { var locationobject = document.createelement('a'); locationobject.href = this.path; locationobject.pathname = locationobject.pathname.replace(regexmatch, suffixreplace); this.at_2x_path = locationobject.href; } else { var parts = this.path.split('?'); parts[0] = parts[0].replace(regexmatch, suffixreplace); this.at_2x_path = parts.join('?'); } this.perform_check = true; } } root.retinaimagepath = retinaimagepath; retinaimagepath.confirmed_paths = []; retinaimagepath.prototype.is_external = function() { return !!(this.path.match(/^https?\:/i) && !this.path.match('//' + document.domain) ); }; retinaimagepath.prototype.check_2x_variant = function(callback) { var http, that = this; if (this.is_external()) { return callback(false); } else if (!this.perform_check && typeof this.at_2x_path !== 'undefined' && this.at_2x_path !== null) { return callback(true); } else if (this.at_2x_path in retinaimagepath.confirmed_paths) { return callback(true); } else { http = new xmlhttprequest(); http.open('head', this.at_2x_path); http.onreadystatechange = function() { if (http.readystate !== 4) { return callback(false); } if (http.status >= 200 && http.status <= 399) { if (config.check_mime_type) { var type = http.getresponseheader('content-type'); if (type === null || !type.match(/^image/i)) { return callback(false); } } retinaimagepath.confirmed_paths.push(that.at_2x_path); return callback(true); } else { return callback(false); } }; http.send(); } }; function retinaimage(el) { this.el = el; this.path = new retinaimagepath(this.el.getattribute('src'), this.el.getattribute('data-at2x')); var that = this; this.path.check_2x_variant(function(hasvariant) { if (hasvariant) { that.swap(); } }); } root.retinaimage = retinaimage; retinaimage.prototype.swap = function(path) { if (typeof path === 'undefined') { path = this.path.at_2x_path; } var that = this; function load() { if (! that.el.complete) { settimeout(load, 5); } else { if (config.force_original_dimensions) { that.el.setattribute('width', that.el.offsetwidth); that.el.setattribute('height', that.el.offsetheight); } that.el.setattribute('src', path); } } load(); }; if (retina.isretina()) { retina.init(root); } })();