Skip to content

Commit

Permalink
First Release
Browse files Browse the repository at this point in the history
  • Loading branch information
dwoutsourcing committed Feb 8, 2015
1 parent fa38b94 commit d3ce429
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.settings
.project
.buildpath
40 changes: 40 additions & 0 deletions imageSRC.jquery.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"name": "imageSRC",
"version": "1.0.0",
"title": "jQuery Image SRC",
"author": {
"name": "dwoutsourcing",
"email": "[email protected]",
"url": "https://github.com/dwoutsourcing"
},
"licenses": [
{
"type": "MIT",
"url": "https://github.com/dwoutsourcing/imageSRC/blob/master/LICENSE"
}
],
"dependencies": {
"jquery": ">=1.5"
},
"description": "jQuery plugin to set image src attribute from either dataURL strings, File instances or string values and adding progress event.",
"keywords": [
"image",
"image tag",
"image attributes",
"image tag attribute",
"image src attribute",
"image progress event",
"image source",
"image load from File"
],
"maintainers": [
{
"name": "dwoutsourcing",
"email": "[email protected]",
"url": "https://github.com/dwoutsourcing"
}
],
"bugs": "https://github.com/dwoutsourcing/imageSRC/issues",
"homepage": "https://github.com/dwoutsourcing/imageSRC",
"docs": "https://github.com/dwoutsourcing/imageSRC"
}
86 changes: 86 additions & 0 deletions jquery.dw.imagesrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* Author: dwoutsourcing ([email protected])
* License: MIT
*/
(function($, undefined) {
function getSourceType(source) {
var type;

if(/^data:image/.test(source))
type = 'dataURL';
else {
if(typeof source == 'string')
type = 'path';
else if(source.name)
type = 'localFile';
}

return type;
}

function initializeLoader($loader, $scope) {
return $loader
.on('progress', function(event, originalEvent, perc, loaded, total) {
$scope.trigger('progress', [originalEvent, perc, loaded, total]);
})
.on('loadstart', function(event, originalEvent) {
$scope.trigger('loadstart', [originalEvent]);
})
.on('loadend', function(event, originalEvent, result) {
$scope.trigger('loadend', [originalEvent, result]);
})
.on('error', function(event, originalEvent, result) {
$scope.trigger('error');
})
;
}

$.fn.extend({
src: function(source) {
var sourceType = getSourceType(source);
var $loader;

this.each(function() {
var $this = $(this);
$this.data('dw-source-type', sourceType);

switch(sourceType) {
case 'dataURL':
this.src = source;
break;

case 'path':
if($.canLoadData()) {
$loader = initializeLoader($.dataLoader(), $this)
.on('load', function(event, originalEvent, response) {
var blob = new Blob([response]);
var result = window.URL.createObjectURL(blob);

$this.attr('src', result);
})
.loadData(source, null, 'arraybuffer')
;
}
else
this.src = source;
break;

case 'localFile':
$loader = initializeLoader($.fileReader(), $this)
.on('load', function(event, originalEvent, result) {
$this.attr('src', result);
})
.readAsDataURL(source)
;
break;

}

$this.abort = function() {
if($loader)
$loader.abort();
};
});
}
});
}( jQuery ));

0 comments on commit d3ce429

Please sign in to comment.