Created
September 3, 2016 23:52
-
-
Save santaklouse/25e2ee3c4a9b21f5309be32b1e29e2da to your computer and use it in GitHub Desktop.
Angular 1.* ; Preloads images in background by using js (using lodash https://lodash.com/)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.service('imagePreload', ['$filter', 'createHeadLink', 'hash', function($filter, createHeadLink, hash) { | |
var preloadedHashes = {}, | |
notLoaded = {}, | |
findInPreloaded = function(obj) { | |
return preloadedHashes[hash(obj.id)] && preloadedHashes[hash(obj.id)] === obj.src; | |
}, | |
isShouldBePreloaded = function(obj) { | |
return obj && obj.src && !findInPreloaded(obj); | |
}, | |
addPreloaded = function(obj) { | |
preloadedHashes[hash(obj.id)] = obj.src; | |
}; | |
return { | |
preloaded: preloadedHashes, | |
isShouldBePreloaded: isShouldBePreloaded, | |
isNotLoaded: function(obj) { | |
return notLoaded[obj.id] && notLoaded[obj.id] === obj.src; | |
}, | |
preload: function(objects) { | |
_.chain(objects) | |
.map(function(obj){ | |
if (isShouldBePreloaded(obj)) { | |
return obj; | |
} | |
}) | |
.compact() | |
.forEach(function(obj){ | |
var img = new Image(); | |
img.onerror = function() { | |
notLoaded[obj.id] = obj.src; | |
}; | |
img.src = obj.src; | |
addPreloaded(obj); | |
}); | |
}, | |
addPreloadLinks: function(obj) { | |
if (!isShouldBePreloaded(obj)) { | |
return; | |
} | |
if (page_data['preload']) { | |
createHeadLink('preload', obj.src, 'image'); | |
} | |
} | |
} | |
}]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.service('createHeadLink', ['$document', function($document) { | |
return function(type, href, asType) { | |
var params, elemTag = 'link'; | |
if (_.isObject(type) && _.isUndefined(href)) { | |
params = type; | |
} | |
if (!_.isObject(type) && _.isEmpty(href)) { | |
return; | |
} | |
if (params) { | |
elemTag = params.tag; | |
} | |
var element = document.createElement(elemTag); | |
if (params && _.isArray(params.attrs)) { | |
_.forEach(params.attrs, function(attr) { | |
element.setAttribute(attr.name, attr.value); | |
}); | |
} else { | |
if (type === 'preload') { | |
element.setAttribute("as", asType); | |
} | |
element.setAttribute("rel", type); | |
element.setAttribute("href", href); | |
} | |
$document.find("head").append(element); | |
} | |
}]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.service('hash', ['$filter', function($filter) { | |
/** | |
* Javascript implementation of Java’s String.hashCode() method | |
* @param {String} str | |
* @param {Boolean} inHex | |
* @returns {String} | |
*/ | |
var hashCode = function(str, inHex, radix) { | |
if (_.isObject(str)) { | |
str = _.toPairs(str); | |
} | |
if (!_.isString(str)) { | |
str = !str ? '0' : str.toString(); | |
} | |
var hash = 0, i = 0, | |
length = str.length; | |
if (!length) { | |
return hash; | |
} | |
for (; i < length; i++) { | |
hash = ((hash << 5) - hash) + str.charCodeAt(i); | |
hash = hash & hash; | |
} | |
radix = inHex ? 16 : (radix ? radix : 10); | |
return Math.abs(hash).toString(radix); | |
} | |
return function(value) { | |
return hashCode(value, 0, 36); | |
} | |
}]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var imagesToPreload = [ | |
{ | |
id:1, | |
src: 'https://www.google.com.ua/images/branding/googlelogo/1x/googlelogo_white_background_color_272x92dp.png', | |
{ | |
id:2, | |
src: 'https://www.google.com.ua/images/nav_logo229.png' | |
} | |
]; | |
imagePreload = $injector.get('imagePreload'); //or any other way to inject | |
imagePreload.preload(imagesToPreload); //preloading images | |
imagePreload.isNotLoaded(obj) // can be checked loaded image or not |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment