jquery.galleryManager.min.js 6.6 KB

1
  1. (function(e){"use strict";var a={csrfToken:e("meta[name=csrf-token]").attr("content"),csrfTokenName:e("meta[name=csrf-param]").attr("content"),nameLabel:"Name",descriptionLabel:"Description",hasName:true,hasDesc:true,uploadUrl:"",deleteUrl:"",updateUrl:"",arrangeUrl:"",photos:[]};function t(t,s){var o=e.extend({},a,s);var r=o.csrfToken?"&"+o.csrfTokenName+"="+o.csrfToken:"";var n={};var i=e(t);if(!o.hasName){if(!o.hasDesc){i.addClass("no-name-no-desc");e(".edit_selected",i).hide()}else i.addClass("no-name")}else if(!o.hasDesc)i.addClass("no-desc");var c=e(".sorter",i);var l=e(".images",c);var d=e(".editor-modal",i);var p=e(".progress-overlay",i);var h=e(".upload-progress",p);var f=e(".form",d);function v(e){return String(e).replace(/&/g,"&amp;").replace(/"/g,"&quot;").replace(/'/g,"&#39;").replace(/</g,"&lt;").replace(/>/g,"&gt;")}function u(a,t,s,r){var n='<div class="photo-editor row">'+'<div class="col-xs-4">'+'<img src="'+v(t)+'" style="max-width:100%;">'+"</div>"+'<div class="col-xs-8">'+(o.hasName?'<div class="form-group">'+'<label class="control-label" for="photo_name_'+a+'">'+o.nameLabel+":</label>"+'<input class="form-control" type="text" name="photo['+a+'][name]" class="input-xlarge" value="'+v(s)+'" id="photo_name_'+a+'"/>'+"</div>":"")+(o.hasDesc?'<div class="form-group">'+'<label class="control-label" for="photo_description_'+a+'">'+o.descriptionLabel+":</label>"+'<textarea class="form-control" name="photo['+a+'][description]" rows="3" cols="40" class="input-xlarge" id="photo_description_'+a+'">'+v(r)+"</textarea>"+"</div>":"")+"</div>"+"</div>";return e(n)}var m='<div class="photo">'+'<div class="image-preview"><img src=""/></div><div class="caption">';if(o.hasName){m+="<h5></h5>"}if(o.hasDesc){m+="<p></p>"}m+='</div><div class="actions">';if(o.hasName||o.hasDesc){m+='<span class="editPhoto btn btn-primary btn-xs"><i class="glyphicon glyphicon-pencil glyphicon-white"></i></span> '}m+='<span class="deletePhoto btn btn-danger btn-xs"><i class="glyphicon glyphicon-remove glyphicon-white"></i></span>'+'</div><input type="checkbox" class="photo-select"/></div>';function g(a,t,s,r,i){var c=e(m);n[a]=c;c.data("id",a);c.data("rank",i);e("img",c).attr("src",t);if(o.hasName){e(".caption h5",c).text(s)}if(o.hasDesc){e(".caption p",c).text(r)}l.append(c);return c}function k(a){var t=a.length;var s=f.empty();for(var o=0;o<t;o++){var r=a[o];var i=n[r],c=e("img",i).attr("src"),l=e(".caption h5",i).text(),p=e(".caption p",i).text();s.append(u(r,c,l,p))}if(t>0){d.modal("show")}}function D(a){e.ajax({type:"POST",url:o.deleteUrl,data:"id[]="+a.join("&id[]=")+r,success:function(e){if(e=="OK"){for(var t=0,s=a.length;t<s;t++){n[a[t]].remove();delete n[a[t]]}}else{alert(e)}}})}function b(a){a.preventDefault();var t=e(this).closest(".photo");var s=t.data("id");D([s]);return false}function x(a){a.preventDefault();var t=e(this).closest(".photo");var s=t.data("id");k([s]);return false}function w(){var a=e(".photo.selected",c).length;e(".select_all",i).prop("checked",e(".photo",c).length==a);if(a==0){e(".edit_selected, .remove_selected",i).addClass("disabled")}else{e(".edit_selected, .remove_selected",i).removeClass("disabled")}}function y(){var a=e(this);if(a.is(":checked"))a.closest(".photo").addClass("selected");else a.closest(".photo").removeClass("selected");w()}l.on("click",".photo .deletePhoto",b).on("click",".photo .editPhoto",x).on("click",".photo .photo-select",y);e(".images",c).sortable({tolerance:"pointer"}).disableSelection().bind("sortstop",function(){var a=[];e(".photo",c).each(function(){var t=e(this);a.push("order["+t.data("id")+"]="+t.data("rank"))});e.ajax({type:"POST",url:o.arrangeUrl,data:a.join("&")+r,dataType:"json"}).done(function(e){for(var a in e[a]){n[a].data("rank",e[a])}})});if(window.FormData!==undefined){var T=e(".afile",i).attr("name");var _=function(e){if(e.length==0)return;p.show();h.css("width","5%");var a=e.length;var t=0;var s=[];for(var r=0;r<a;r++){var n=new FormData;n.append(T,e[r]);if(o.csrfToken){n.append(o.csrfTokenName,o.csrfToken)}var i=new XMLHttpRequest;i.open("POST",o.uploadUrl,true);i.onload=function(){t++;if(this.status==200){var e=JSON.parse(this.response);g(e["id"],e["preview"],e["name"],e["description"],e["rank"]);s.push(e["id"])}else{}h.css("width",""+(5+95*t/a)+"%");if(t===a){h.css("width","100%");p.hide();if(o.hasName||o.hasDesc)k(s)}};i.send(n)}};(function(){var e=i[0];var a=false;var t=false;setInterval(function(){if(a!=t){if(a)e.classList.add("over");else e.classList.remove("over");t=a}},30);function s(e){e.preventDefault();a=true;return false}function o(){a=false;return false}function r(e){e.preventDefault();e.stopPropagation();var t=e.dataTransfer.files;_(t);a=false;return false}function n(){a=false}e.addEventListener("dragover",s,false);e.addEventListener("dragleave",o,false);e.addEventListener("drop",r,false);e.addEventListener("dragend",n,false)})();e(".afile",i).attr("multiple","true").on("change",function(e){e.preventDefault();_(this.files);$(this).val(null)})}else{e(".afile",i).on("change",function(a){a.preventDefault();var t=[];p.show();h.css("width","5%");var s={};if(o.csrfToken)s[o.csrfTokenName]=o.csrfToken;e.ajax({type:"POST",url:o.uploadUrl,data:s,files:e(this),iframe:true,processData:false,dataType:"json"}).done(function(e){g(e["id"],e["preview"],e["name"],e["description"],e["rank"]);t.push(e["id"]);h.css("width","100%");p.hide();if(o.hasName||o.hasDesc)k(t)})})}e(".save-changes",d).click(function(a){a.preventDefault();e.post(o.updateUrl,e("input, textarea",f).serialize()+r,function(a){var t=a.length;for(var s=0;s<t;s++){var r=a[s];var l=n[r.id];e("img",l).attr("src",r["src"]);if(o.hasName)e(".caption h5",l).text(r["name"]);if(o.hasDesc)e(".caption p",l).text(r["description"])}d.modal("hide");e(".photo.selected",c).each(function(){e(".photo-select",this).prop("checked",false)}).removeClass("selected");e(".select_all",i).prop("checked",false);w()},"json")});e(".edit_selected",i).click(function(a){a.preventDefault();var t=[];e(".photo.selected",c).each(function(){t.push(e(this).data("id"))});k(t);return false});e(".remove_selected",i).click(function(a){a.preventDefault();var t=[];e(".photo.selected",c).each(function(){t.push(e(this).data("id"))});D(t)});e(".select_all",i).change(function(){if(e(this).prop("checked")){e(".photo",c).each(function(){e(".photo-select",this).prop("checked",true)}).addClass("selected")}else{e(".photo.selected",c).each(function(){e(".photo-select",this).prop("checked",false)}).removeClass("selected")}w()});for(var N=0,L=o.photos.length;N<L;N++){var C=o.photos[N];g(C["id"],C["preview"],C["name"],C["description"],C["rank"])}}e.fn.galleryManager=function(e){if(this.length){this.each(function(){t(this,e)})}}})(jQuery);