', {
"class": 'mercury-uploader',
style: 'display:none'
});
this.element.append('
', {
"class": 'mercury-uploader-overlay',
style: 'display:none'
});
this.element.appendTo((_ref = jQuery(this.options.appendTo).get(0)) != null ? _ref : 'body');
return this.overlay.appendTo((_ref1 = jQuery(this.options.appendTo).get(0)) != null ? _ref1 : 'body');
},
bindEvents: function() {
var _this = this;
return Mercury.on('resize', function() {
return _this.position();
});
},
appear: function() {
var _this = this;
this.fillDisplay();
this.position();
this.overlay.show();
return this.overlay.animate({
opacity: 1
}, 200, 'easeInOutSine', function() {
_this.element.show();
return _this.element.animate({
opacity: 1
}, 200, 'easeInOutSine', function() {
_this.visible = true;
return _this.loadImage();
});
});
},
position: function() {
var height, width;
width = this.element.outerWidth();
height = this.element.outerHeight();
return this.element.css({
top: (Mercury.displayRect.height - height) / 2,
left: (Mercury.displayRect.width - width) / 2
});
},
fillDisplay: function() {
var details;
details = [Mercury.I18n('Name: %s', this.file.name), Mercury.I18n('Size: %s', this.file.readableSize), Mercury.I18n('Type: %s', this.file.type)];
return this.element.find('.mercury-uploader-details').html(details.join('
'));
},
loadImage: function() {
var _this = this;
if (Mercury.uploader.fileReaderSupported()) {
return this.file.readAsDataURL(function(result) {
_this.element.find('.mercury-uploader-preview b').html(jQuery('
', {
src: result
}));
return _this.upload();
});
} else {
return this.upload();
}
},
upload: function() {
var formData, xhr,
_this = this;
xhr = new XMLHttpRequest;
jQuery.each(['onloadstart', 'onprogress', 'onload', 'onabort', 'onerror'], function(index, eventName) {
return xhr.upload[eventName] = function(event) {
return _this.uploaderEvents[eventName].call(_this, event);
};
});
xhr.onload = function(event) {
var response, src;
if (event.currentTarget.status >= 400) {
_this.updateStatus('Error: Unable to upload the file');
Mercury.notify('Unable to process response: %s', event.currentTarget.status);
return _this.hide();
} else {
try {
response = Mercury.config.uploading.handler ? Mercury.config.uploading.handler(event.target.responseText) : jQuery.parseJSON(event.target.responseText);
src = response.url || response.image.url;
if (!src) {
throw 'Malformed response from server.';
}
Mercury.trigger('action', {
action: 'insertImage',
value: {
src: src
}
});
return _this.hide();
} catch (error) {
_this.updateStatus('Error: Unable to upload the file');
Mercury.notify('Unable to process response: %s', error);
return _this.hide();
}
}
};
xhr.open('post', Mercury.config.uploading.url, true);
xhr.setRequestHeader('Accept', 'application/json, text/javascript, text/html, application/xml, text/xml, */*');
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
xhr.setRequestHeader(Mercury.config.csrfHeader, Mercury.csrfToken);
if (Mercury.uploader.fileReaderSupported()) {
return this.file.readAsBinaryString(function(result) {
var multipart;
multipart = new Mercury.uploader.MultiPartPost(Mercury.config.uploading.inputName, _this.file, result);
_this.file.updateSize(multipart.delta);
xhr.setRequestHeader('Content-Type', 'multipart/form-data; boundary=' + multipart.boundary);
return xhr.sendAsBinary(multipart.body);
});
} else {
formData = new FormData();
formData.append(Mercury.config.uploading.inputName, this.file.file, this.file.file.name);
return xhr.send(formData);
}
},
updateStatus: function(message, loaded) {
var percent;
this.element.find('.mercury-uploader-progress span').html(Mercury.I18n(message).toString());
if (loaded) {
percent = Math.floor(loaded * 100 / this.file.size) + '%';
this.element.find('.mercury-uploader-indicator div').css({
width: percent
});
return this.element.find('.mercury-uploader-indicator b').html(percent).show();
}
},
hide: function(delay) {
var _this = this;
if (delay == null) {
delay = 0;
}
return setTimeout(function() {
return _this.element.animate({
opacity: 0
}, 200, 'easeInOutSine', function() {
return _this.overlay.animate({
opacity: 0
}, 200, 'easeInOutSine', function() {
_this.overlay.hide();
_this.element.hide();
_this.reset();
_this.visible = false;
return Mercury.trigger('focus:frame');
});
});
}, delay * 1000);
},
reset: function() {
this.element.find('.mercury-uploader-preview b').html('');
this.element.find('.mercury-uploader-indicator div').css({
width: 0
});
this.element.find('.mercury-uploader-indicator b').html('0%').hide();
return this.updateStatus('Processing...');
},
uploaderEvents: {
onloadstart: function() {
return this.updateStatus('Uploading...');
},
onprogress: function(event) {
return this.updateStatus('Uploading...', event.loaded);
},
onabort: function() {
this.updateStatus('Aborted');
return this.hide(1);
},
onload: function() {
return this.updateStatus('Successfully uploaded...', this.file.size);
},
onerror: function() {
this.updateStatus('Error: Unable to upload the file');
return this.hide(3);
}
}
});
Mercury.uploader.File = (function() {
function File(file) {
var errors;
this.file = file;
this.fullSize = this.size = this.file.size || this.file.fileSize;
this.readableSize = this.size.toBytes();
this.name = this.file.name || this.file.fileName;
this.type = this.file.type || this.file.fileType;
errors = [];
if (this.size >= Mercury.config.uploading.maxFileSize) {
errors.push(Mercury.I18n('Too large'));
}
if (!(Mercury.config.uploading.allowedMimeTypes.indexOf(this.type) > -1)) {
errors.push(Mercury.I18n('Unsupported format'));
}
if (errors.length) {
this.errors = errors.join(' / ');
}
}
File.prototype.readAsDataURL = function(callback) {
var reader,
_this = this;
if (callback == null) {
callback = null;
}
reader = new FileReader();
reader.readAsDataURL(this.file);
return reader.onload = function() {
if (callback) {
return callback(reader.result);
}
};
};
File.prototype.readAsBinaryString = function(callback) {
var reader,
_this = this;
if (callback == null) {
callback = null;
}
reader = new FileReader();
reader.readAsBinaryString(this.file);
return reader.onload = function() {
if (callback) {
return callback(reader.result);
}
};
};
File.prototype.updateSize = function(delta) {
return this.fullSize = this.size + delta;
};
return File;
})();
Mercury.uploader.MultiPartPost = (function() {
function MultiPartPost(inputName, file, contents, formInputs) {
this.inputName = inputName;
this.file = file;
this.contents = contents;
this.formInputs = formInputs != null ? formInputs : {};
this.boundary = 'Boundaryx20072377098235644401115438165x';
this.body = '';
this.buildBody();
this.delta = this.body.length - this.file.size;
}
MultiPartPost.prototype.buildBody = function() {
var boundary, name, value, _ref;
boundary = '--' + this.boundary;
_ref = this.formInputs;
for (name in _ref) {
if (!__hasProp.call(_ref, name)) continue;
value = _ref[name];
this.body += "" + boundary + "\r\nContent-Disposition: form-data; name=\"" + name + "\"\r\n\r\n" + (unescape(encodeURIComponent(value))) + "\r\n";
}
return this.body += "" + boundary + "\r\nContent-Disposition: form-data; name=\"" + this.inputName + "\"; filename=\"" + this.file.name + "\"\r\nContent-Type: " + this.file.type + "\r\nContent-Transfer-Encoding: binary\r\n\r\n" + this.contents + "\r\n" + boundary + "--";
};
return MultiPartPost;
})();
}).call(this);
; FI"required_assets_digest; F"%1da62830e1c7ecbfadf76c24ac83ffe8I"
_version; F"%6776f581a4329e299531e1d52aa59832