/*This software is MIT licensed.
Copyright (c) 2012 Imazen LLC
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
(function ($) {
//Polling methods
//$('obj').ImageStudio('api').getStatus({'restoreSuspendedCommands':true, 'removeEditingConstraints':true, 'useEditingServer':false} ) returns { url, path, query };
//$('obj').ImageStudio('api').setOptions({height:600});
//$('obj').ImageStudio('api').setOptions({url:'newimageurl.jpg'}); //Yes, you can switch images like this.. as long as you're not in the middle of cropping. That's not supported yet.
//$('obj').ImageStudio('api').getOptions();
//$('obj').ImageStudio('api').destroy();
//labels and icon values cannot be updated after initialization.
var defaults = {
url: null, //The image URL to load for editing.
width: null, //To set the width of the area
accordionWidth: 230,
height: 560, //To constrain the height of the area.
panes: ['rotateflip', 'crop', 'adjust', 'effects', 'redeye', 'carve', 'faces'], //A list of panes to display, in order.
editingServer: null, //If set, an alternate server will be used during editing. For example, using cloudfront during editing is counter productive
editWithSemicolons: null, //If true, semicolon notation will be used with the editing server.
finalWithSemicolons: null, //If true, semicolons will be used in the final URLs. Defaults to true if the input URL uses semicolons.
//A list of commands to temporarily remove from the URL during editing so that position-dependent operations aren't affected.
//Any commands used by the editor should be in here also, such as 'cache', 'memcache', 'maxwidth',and 'maxheight'
suspendKeys: ['width', 'height', 'maxwidth', 'maxheight',
'scale', 'rotate', 'flip', 'anchor',
'paddingwidth', 'paddingcolor', 'borderwidth', 'bordercolor', 'margin',
'cache', 'scache', 'process', 'shadowwidth', 'shadowcolor', 'shadowoffset', 'mode'],
editingCommands: { cache: 'no', scache: 'mem' },
onchange: null, //The callback to fire whenever an edit occurs.
cropratios: [[0, "Custom"], ["current", "Current"], [4 / 3, "4:3"], [16 / 9, "16:9 (Widescreen)"], [3 / 2, "3:2"]],
cropPreview: { width: '175px', height: '175px', 'margin-left': '-15px' },
icons: {
rotateleft: 'arrowreturnthick-1-w',
rotateright: 'arrowreturnthick-1-e',
flipvertical: 'arrowthick-2-n-s',
fliphorizontal: 'arrowthick-2-e-w',
reset: 'cancel',
autofix: 'image',
autowhite: 'image',
blackwhite: 'image',
sepia: 'image',
negative: 'image'
},
labels: {
pane_rotateflip: 'Rotate & Flip',
rotateleft: 'Rotate Left',
rotateright: 'Rotate Right',
flipvertical: 'Flip Vertical',
fliphorizontal: 'Flip Horizontal',
reset: 'Reset',
pane_crop: 'Crop',
aspectratio: 'Aspect Ratio',
crop_crop: 'Crop',
crop_modify: 'Modify Crop',
crop_cancel: 'Cancel',
crop_done: 'Done',
pane_adjust: 'Adjust Image',
autofix: 'Auto-Fix',
autowhite: 'Auto-Balance',
contrast: 'Contrast',
saturation: 'Saturation',
brightness: 'Brightness',
pane_effects: 'Effects & Filters',
blackwhite: 'Black & White',
sepia: 'Sepia',
negative: 'Negative',
sharpen: 'Smart Sharpen',
noiseremoval: 'Noise Removal',
oilpainting: 'Oil Painting',
posterize: 'Posterize',
blur: 'Gaussian Blur',
pane_redeye: 'Red-Eye Removal',
redeye_auto: 'Auto-detect Eyes',
redeye_start: 'Fix Red-Eye',
redeye_preview: 'Toggle Preview',
redeye_clear: 'Clear',
pane_faces: "Face Selection",
faces_auto: "Auto-detect Faces",
faces_start: "Select Faces",
faces_clear: "Clear",
cancel: 'Cancel',
done: 'Done',
pane_carve: 'Object Removal',
carve_start: 'Remove objects',
carve_preview: 'Preview result'
}
};
/* Coding style notes:
Within each pane, a closure object name 'cl' is used to track state within the pane.
Each pane function is passed a reference to the instance-wide options object, which can be used to modify the image URL, lock/unlock the accordion, etc.
*/
$.fn.ImageStudio = function (options) {
var processOptions = function (options) {
var defs = $.extend(true, {}, defaults);
if (options.labels) defs.labels = $.extend(true, defs.labels, options.labels);
if (options.icons) defs.icons = $.extend(true, defs.icons, options.icons);
return $.extend(defs, options);
};
var result = this;
this.each(function () {
var div = $(this);
if (div.data('ImageStudio')) {
// The API can be requested this way (undocumented)
if (options == 'api') {
result = div.data('ImageStudio');
return;
}
// Otherwise, we just reset the options...
else div.data('ImageStudio').setOptions(options);
} else {
div.data('ImageStudio', init(div, processOptions(options)));
}
});
return result;
};
function init(div, opts) {
div = $(div);
div.empty();
div.removeClass("imagestudio"); div.addClass("imagestudio");
//UGH! a table. But the alternative is nasty cross-browser.
var tr = $('
').appendTo($('
').css('width', '100%').appendTo(div));
//Add accordion
var atd = $('
').appendTo(tr);
var a = $("").addClass("controls").width(opts.accordionWidth).appendTo(atd);
//Add image
var itd = $('
').addClass("imageCell").css('vertical-align', 'middle').css('text-align', 'center').css('padding-left', '10px').css('padding-right', '10px').appendTo(tr);
var idiv = $('').css('text-align', 'center').appendTo(itd);
var img = $('').addClass("studioimage").appendTo(idiv);
opts.img = img; //Save a reference to the image object in options
opts.imgDiv = idiv;
opts.container = div;
opts.accordion = a;
var updateOptions = function (changedOpts) {
//Called by both init and setOptions.
var o = changedOpts;
//When init calls, 'changedOpts' and 'opts' reference the same object
var isUpdate = (o != opts);
//See if we can skip the URL update.
var skipUrlUpdate = isUpdate && (!o.url || o.url == opts.url);
//If we're updating the URL, see if we are using semicolons, and set appropriate properties
if (!skipUrlUpdate && o.url.indexOf('?') < 0 && o.url.indexOf(';') > -1 && (o.finalWithSemicolons === undefined || o.finalWithSemicolons == null)) o.finalWithSemicolons = true;
if (o.finalWithSemicolons && _.all([o.editWithSemicolons, o.editingServer, opts.editWithSemicolons, opts.editingServer], function (v) { return v === null || v === undefined; }))
o.editWithSemicolons = true;
//If this is an update, not an init, override old values with new ones.
if (isUpdate) $.extend(opts, o);
if (o.width) { div.width(o.width); }
if (o.height) div.height(o.height);
if (o.height) a.height(o.height);
if (o.accordionWidth) { a.width(o.accordionWidth); atd.width(o.accordionWidth); }
if (!skipUrlUpdate) {
opts.original = ImageResizer.Utils.parseUrl(opts.url);
opts.editPath = opts.original.path;
if (opts.editingServer) opts.editPath = ImageResizer.Utils.changeServer(opts.editPath, opts.editingServer);
opts.originalQuery = opts.original.obj;
opts.filteredQuery = new ImageResizer.Instructions(opts.originalQuery);
opts.suspendedItems = opts.filteredQuery.remove(opts.suspendKeys);
var withConstraints = new ImageResizer.Instructions(opts.filteredQuery);
withConstraints.maxwidth = div.width() - opts.accordionWidth - 30;
withConstraints.maxheight = opts.height - 28;
withConstraints.mergeWith(opts.editingCommands, true);
opts.editQuery = withConstraints;
opts.editUrl = opts.editPath + withConstraints.toQueryString(opts.editWithSemicolons);
img.attr('src', opts.editUrl);
//This event lets 'involved' panes like crop, object removal, faces, red-eye, etc. exit when we change the source image.
img.triggerHandler('sourceImageChanged', [opts.url]);
//This event keeps all sliders, toggles, etc in sync
img.triggerHandler('query', [new ImageResizer.Instructions(opts.editUrl)]);
}
}; updateOptions(opts);
//Add requested panes
var panes = { 'rotateflip': addRotateFlipPane, 'crop': addCropPane, 'adjust': addAdjustPane, 'redeye': addRedEyePane, 'carve': addCarvePane, 'effects': addEffectsPane, 'faces': addFacesPane };
for (var i = 0; i < opts.panes.length; i++) {
a.append('