Commits

Bogdan Savluk committed 1784521

code refactoring, small optimisations

Comments (0)

Files changed (4)

GalleryController.php

                 $resp[] = array(
                     'id' => $model->id,
                     'rank' => $model->rank,
-                    'name' => (string)$model->name, //todo: something wrong with model - it returns null, but it must return an empty string
+                    'name' => (string)$model->name,
                     'description' => (string)$model->description,
                     'preview' => $model->getPreview(),
                 );

GalleryManager.php

             $opts['csrfToken'] = Yii::app()->request->csrfToken;
         }
         $opts = CJavaScript::encode($opts);
-        $src = "$('#{$this->id}').galleryManager({$opts});";
-        $cs->registerScript('galleryManager#' . $this->id, $src);
+        $cs->registerScript('galleryManager#' . $this->id, "$('#{$this->id}').galleryManager({$opts});");
         $model = new GalleryPhoto();
 
         $cls = "GalleryEditor ";

assets/jquery.galleryManager.js

             arrangeUrl:'',
             photos:[]
         };
-
         //Extending options:
         var opts = $.extend({}, this.defaults, options);
         //code
+        var csrfParams = opts.csrfToken ? '&' + opts.csrfTokenName + '=' + opts.csrfToken : '';
+
         var $gallery = $(el);
         opts.wId = $gallery.attr('id');
 
                 .replace(/</g, '&lt;')
                 .replace(/>/g, '&gt;');
         }
-        function photoEditorTemplate(id, src, name, description) {
-            return '<div class="photo-editor">' +
+
+        function createEditorElement(id, src, name, description) {
+            var html = '<div class="photo-editor">' +
                 '<div class="preview"><img src="' + htmlEscape(src) + '" alt=""/></div>' +
                 '<div>' +
                 (opts.hasName
                     : '') +
                 '</div>' +
                 '</div>';
+            return $(html);
         }
 
-        function photoTemplate(id, src, name, description, rank) {
+        function createPhotoElement(id, src, name, description, rank) {
             var res = '<div id="' + opts.wId + '-' + id + '" class="photo">' +
                 '<div class="image-preview"><img src="' + htmlEscape(src) + '"/></div><div class="caption">';
             if (opts.hasName)res += '<h5>' + htmlEscape(name) + '</h5>';
                     : '') +
                 '<span data-photo-id="' + id + '" class="deletePhoto btn btn-danger"><i class="icon-remove icon-white"></i></span>' +
                 '</div><input type="checkbox" class="photo-select"/></div>';
-            return res;
+            return $(res);
         }
 
         function deleteClick(e) {
             $.ajax({
                 type:'POST',
                 url:opts.deleteUrl,
-                data:'id=' + id + (opts.csrfToken ? '&' + opts.csrfTokenName + '=' + opts.csrfToken : ''),
+                data:'id=' + id + csrfParams,
                 success:function (t) {
                     if (t == 'OK') $('#' + opts.wId + '-' + id).remove();
                     else alert(t);
             var src = $('img', photo[0]).attr('src');
             var name = $('.caption h5', photo[0]).text();
             var description = $('.caption p', photo[0]).text();
-            $editorForm.html(photoEditorTemplate(id, src, name, description));
+            $editorForm.empty();
+            $editorForm.append(createEditorElement(id, src, name, description));
             $editorModal.modal('show');
             return false;
         }
         });
 
         $('.images', $sorter).sortable().disableSelection().bind("sortstop", function () {
-            $.post(opts.arrangeUrl, $('input', $sorter).serialize() + '&ajax=true' + (opts.csrfToken ? '&' + opts.csrfTokenName + '=' + opts.csrfToken : ''), function () {
+            $.post(opts.arrangeUrl, $('input', $sorter).serialize() + '&ajax=true' + csrfParams, function () {
                 // order saved!
             }, 'json');
         });
                 e.preventDefault();
                 var filesCount = this.files.length;
                 var uploadedCount = 0;
-                $editorForm.html('');
+                $editorForm.empty();
 
                 for (var i = 0; i < filesCount; i++) {
                     var fd = new FormData();
                         uploadedCount++;
                         if (this.status == 200) {
                             var resp = JSON.parse(this.response);
-                            var newOne = $(photoTemplate(resp['id'], resp['preview'], resp['name'], resp['description'], resp['rank']));
+                            var newOne = createPhotoElement(resp['id'], resp['preview'], resp['name'], resp['description'], resp['rank']);
 
                             bindPhotoEvents(newOne);
 
                             $images.append(newOne);
                             if (opts.hasName || opts.hasDesc)
-                                $editorForm.append($(photoEditorTemplate(resp['id'], resp['preview'], resp['name'], resp['description'])));
+                                $editorForm.append(createEditorElement(resp['id'], resp['preview'], resp['name'], resp['description']));
                         }
                         if (uploadedCount === filesCount && (opts.hasName || opts.hasDesc)) $editorModal.modal('show');
                     };
             $('.afile', $gallery).on('change', function (e) {
 
                 e.preventDefault();
-                $editorForm.html('');
+                $editorForm.empty();
 
                 $.ajax(
                     opts.uploadUrl,
                         iframe:true,
                         dataType:"json"
                     }).done(function (resp) {
-                        var newOne = $(photoTemplate(resp['id'], resp['preview'], resp['name'], resp['description'], resp['rank']));
+                        var newOne = createPhotoElement(resp['id'], resp['preview'], resp['name'], resp['description'], resp['rank']);
                         bindPhotoEvents(newOne);
                         $images.append(newOne);
                         if (opts.hasName || opts.hasDesc)
-                            $editorForm.append($(photoEditorTemplate(resp['id'], resp['preview'], resp['name'], resp['description'])));
+                            $editorForm.append(createEditorElement(resp['id'], resp['preview'], resp['name'], resp['description']));
 
                         if (opts.hasName || opts.hasDesc) $editorModal.modal('show');
                     });
 
         $('.save-changes', $editorModal).click(function (e) {
             e.preventDefault();
-            $.post(opts.updateUrl, $('input, textarea', $editorForm).serialize() + '&ajax=true' + (opts.csrfToken ? '&' + opts.csrfTokenName + '=' + opts.csrfToken : ''), function (data) {
+            $.post(opts.updateUrl, $('input, textarea', $editorForm).serialize() + '&ajax=true' + csrfParams, function (data) {
                 var count = data.length;
                 for (var key = 0; key < count; key++) {
                     var p = data[key];
         $('.edit_selected', $gallery).click(function (e) {
             e.preventDefault();
             var cc = 0;
-            var form = $editorForm.html('');
+            var form = $editorForm.empty();
             $('.photo.selected', $sorter).each(function () {
                 cc++;
                 var photo = $(this),
                     src = $('img', photo[0]).attr('src'),
                     name = $('.caption h5', photo[0]).text(),
                     description = $('.caption p', photo[0]).text();
-                form.append(photoEditorTemplate(id, src, name, description));
+                form.append(createEditorElement(id, src, name, description));
             });
             if (cc > 0)$editorModal.modal('show');
             return false;
                 $.ajax({
                     type:'POST',
                     url:opts.deleteUrl,
-                    data:'id=' + id + (opts.csrfToken ? '&' + opts.csrfTokenName + '=' + opts.csrfToken : ''),
+                    data:'id=' + id + csrfParams,
                     success:function (t) {
                         if (t == 'OK') $('#' + opts.wId + '-' + id).remove();
                         else alert(t);
 
         for (var i in opts.photos) {
             var resp = opts.photos[i];
-            var newOne = $(photoTemplate(resp['id'], resp['preview'], resp['name'], resp['description'], resp['rank'])).data('data', resp);
+            var newOne = createPhotoElement(resp['id'], resp['preview'], resp['name'], resp['description'], resp['rank']).data('data', resp);
 
             bindPhotoEvents(newOne);
             $images.append(newOne);

assets/jquery.galleryManager.min.js

-(function(a){function b(b,c){function j(a){return(a+"").replace(/&/g,"&amp;").replace(/"/g,"&quot;").replace(/'/g,"&#39;").replace(/</g,"&lt;").replace(/>/g,"&gt;")}function k(a,b,c,e){return'<div class="photo-editor"><div class="preview"><img src="'+j(b)+'" alt=""/></div>'+"<div>"+(d.hasName?'<label for="photo_name_'+a+'">'+d.nameLabel+":</label>"+'<input type="text" name="photo['+a+'][name]" class="input-xlarge" value="'+j(c)+'" id="photo_name_'+a+'"/>':"")+(d.hasDesc?'<label for="photo_description_'+a+'">'+d.descriptionLabel+":</label>"+'<textarea name="photo['+a+'][description]" rows="3" cols="40" class="input-xlarge" id="photo_description_'+a+'">'+j(e)+"</textarea>":"")+"</div>"+"</div>"}function l(a,b,c,e,f){var g='<div id="'+d.wId+"-"+a+'" class="photo">'+'<div class="image-preview"><img src="'+j(b)+'"/></div><div class="caption">';return d.hasName&&(g+="<h5>"+j(c)+"</h5>"),d.hasDesc&&(g+="<p>"+j(e)+"</p>"),g+='</div><input type="hidden" name="order['+a+']" value="'+f+'"/><div class="actions">'+(d.hasName||d.hasDesc?'<span data-photo-id="'+a+'" class="editPhoto btn btn-primary"><i class="icon-edit icon-white"></i></span> ':"")+'<span data-photo-id="'+a+'" class="deletePhoto btn btn-danger"><i class="icon-remove icon-white"></i></span>'+'</div><input type="checkbox" class="photo-select"/></div>',g}function m(b){b.preventDefault();var c=a(this).data("photo-id");return a.ajax({type:"POST",url:d.deleteUrl,data:"id="+c+(d.csrfToken?"&"+d.csrfTokenName+"="+d.csrfToken:""),success:function(b){b=="OK"?a("#"+d.wId+"-"+c).remove():alert(b)}}),!1}function n(b){b.preventDefault();var c=a(this).data("photo-id"),d=a(this).parents(".photo"),e=a("img",d[0]).attr("src"),f=a(".caption h5",d[0]).text(),g=a(".caption p",d[0]).text();return i.html(k(c,e,f,g)),h.modal("show"),!1}function o(){var b=a(".photo.selected",f).length;a(".select_all",e).prop("checked",a(".photo",f).length==b),b==0?a(".edit_selected, .remove_selected",e).addClass("disabled"):a(".edit_selected, .remove_selected",e).removeClass("disabled")}function p(){var b=a(this);b.is(":checked")?b.parent().addClass("selected"):b.parent().removeClass("selected"),o()}function q(b){a(".deletePhoto",b).click(m),a(".editPhoto",b).click(n),a(".photo-select",b).change(p)}this.defaults={csrfToken:null,csrfTokenName:null,nameLabel:"Name",descriptionLabel:"Description",hasName:!0,hasDesc:!0,uploadUrl:"",deleteUrl:"",updateUrl:"",arrangeUrl:"",photos:[]};var d=a.extend({},this.defaults,c),e=a(b);d.wId=e.attr("id");var f=a(".sorter",e),g=a(".images",f),h=a(".editor-modal",e),i=a(".form",h);a(".photo",e).each(function(){q(this)}),a(".images",f).sortable().disableSelection().bind("sortstop",function(){a.post(d.arrangeUrl,a("input",f).serialize()+"&ajax=true"+(d.csrfToken?"&"+d.csrfTokenName+"="+d.csrfToken:""),function(){},"json")}),typeof window.FormData=="function"?a(".afile",e).attr("multiple","true").on("change",function(b){b.preventDefault();var c=this.files.length,e=0;i.html("");for(var f=0;f<c;f++){var j=new FormData;j.append(this.name,this.files[f]),d.csrfToken&&j.append(d.csrfTokenName,d.csrfToken);var m=new XMLHttpRequest;m.open("POST",d.uploadUrl,!0),m.onload=function(){e++;if(this.status==200){var b=JSON.parse(this.response),f=a(l(b.id,b.preview,b.name,b.description,b.rank));q(f),g.append(f),(d.hasName||d.hasDesc)&&i.append(a(k(b.id,b.preview,b.name,b.description)))}e===c&&(d.hasName||d.hasDesc)&&h.modal("show")},m.send(j)}}):a(".afile",e).on("change",function(b){b.preventDefault(),i.html(""),a.ajax(d.uploadUrl,{data:d.csrfToken?d.csrfTokenName+"="+d.csrfToken:"",files:a(this),iframe:!0,dataType:"json"}).done(function(b){var c=a(l(b.id,b.preview,b.name,b.description,b.rank));q(c),g.append(c),(d.hasName||d.hasDesc)&&i.append(a(k(b.id,b.preview,b.name,b.description))),(d.hasName||d.hasDesc)&&h.modal("show")})}),a(".save-changes",h).click(function(b){b.preventDefault(),a.post(d.updateUrl,a("input, textarea",i).serialize()+"&ajax=true"+(d.csrfToken?"&"+d.csrfTokenName+"="+d.csrfToken:""),function(b){var c=b.length;for(var g=0;g<c;g++){var i=b[g],j=a("#"+d.wId+"-"+i.id);a("img",j).attr("src",i.src),d.hasName&&a(".caption h5",j).text(i.name),d.hasDesc&&a(".caption p",j).text(i.description)}h.modal("hide"),a(".photo.selected",f).each(function(){a(".photo-select",this).prop("checked",!1)}).removeClass("selected"),a(".select_all",e).prop("checked",!1),o()},"json")}),a(".edit_selected",e).click(function(b){b.preventDefault();var c=0,e=i.html("");return a(".photo.selected",f).each(function(){c++;var b=a(this),f=b.attr("id").substr((d.wId+"-").length),g=a("img",b[0]).attr("src"),h=a(".caption h5",b[0]).text(),i=a(".caption p",b[0]).text();e.append(k(f,g,h,i))}),c>0&&h.modal("show"),!1}),a(".remove_selected",e).click(function(b){b.preventDefault(),a(".photo.selected",f).each(function(){var b=a(this).attr("id").substr((d.wId+"-").length);a.ajax({type:"POST",url:d.deleteUrl,data:"id="+b+(d.csrfToken?"&"+d.csrfTokenName+"="+d.csrfToken:""),success:function(c){c=="OK"?a("#"+d.wId+"-"+b).remove():alert(c)}})})}),a(".select_all",e).change(function(){a(this).prop("checked")?a(".photo",f).each(function(){a(".photo-select",this).prop("checked",!0)}).addClass("selected"):a(".photo.selected",f).each(function(){a(".photo-select",this).prop("checked",!1)}).removeClass("selected"),o()});for(var r in d.photos){var s=d.photos[r],t=a(l(s.id,s.preview,s.name,s.description,s.rank)).data("data",s);q(t),g.append(t)}}a.fn.galleryManager=function(a){this.length&&this.each(function(){b(this,a)})}})(jQuery);
+(function(a){function b(b,c){function k(a){return(a+"").replace(/&/g,"&amp;").replace(/"/g,"&quot;").replace(/'/g,"&#39;").replace(/</g,"&lt;").replace(/>/g,"&gt;")}function l(b,c,e,f){var g='<div class="photo-editor"><div class="preview"><img src="'+k(c)+'" alt=""/></div>'+"<div>"+(d.hasName?'<label for="photo_name_'+b+'">'+d.nameLabel+":</label>"+'<input type="text" name="photo['+b+'][name]" class="input-xlarge" value="'+k(e)+'" id="photo_name_'+b+'"/>':"")+(d.hasDesc?'<label for="photo_description_'+b+'">'+d.descriptionLabel+":</label>"+'<textarea name="photo['+b+'][description]" rows="3" cols="40" class="input-xlarge" id="photo_description_'+b+'">'+k(f)+"</textarea>":"")+"</div>"+"</div>";return a(g)}function m(b,c,e,f,g){var h='<div id="'+d.wId+"-"+b+'" class="photo">'+'<div class="image-preview"><img src="'+k(c)+'"/></div><div class="caption">';return d.hasName&&(h+="<h5>"+k(e)+"</h5>"),d.hasDesc&&(h+="<p>"+k(f)+"</p>"),h+='</div><input type="hidden" name="order['+b+']" value="'+g+'"/><div class="actions">'+(d.hasName||d.hasDesc?'<span data-photo-id="'+b+'" class="editPhoto btn btn-primary"><i class="icon-edit icon-white"></i></span> ':"")+'<span data-photo-id="'+b+'" class="deletePhoto btn btn-danger"><i class="icon-remove icon-white"></i></span>'+'</div><input type="checkbox" class="photo-select"/></div>',a(h)}function n(b){b.preventDefault();var c=a(this).data("photo-id");return a.ajax({type:"POST",url:d.deleteUrl,data:"id="+c+e,success:function(b){b=="OK"?a("#"+d.wId+"-"+c).remove():alert(b)}}),!1}function o(b){b.preventDefault();var c=a(this).data("photo-id"),d=a(this).parents(".photo"),e=a("img",d[0]).attr("src"),f=a(".caption h5",d[0]).text(),g=a(".caption p",d[0]).text();return j.empty(),j.append(l(c,e,f,g)),i.modal("show"),!1}function p(){var b=a(".photo.selected",g).length;a(".select_all",f).prop("checked",a(".photo",g).length==b),b==0?a(".edit_selected, .remove_selected",f).addClass("disabled"):a(".edit_selected, .remove_selected",f).removeClass("disabled")}function q(){var b=a(this);b.is(":checked")?b.parent().addClass("selected"):b.parent().removeClass("selected"),p()}function r(b){a(".deletePhoto",b).click(n),a(".editPhoto",b).click(o),a(".photo-select",b).change(q)}this.defaults={csrfToken:null,csrfTokenName:null,nameLabel:"Name",descriptionLabel:"Description",hasName:!0,hasDesc:!0,uploadUrl:"",deleteUrl:"",updateUrl:"",arrangeUrl:"",photos:[]};var d=a.extend({},this.defaults,c),e=d.csrfToken?"&"+d.csrfTokenName+"="+d.csrfToken:"",f=a(b);d.wId=f.attr("id");var g=a(".sorter",f),h=a(".images",g),i=a(".editor-modal",f),j=a(".form",i);a(".photo",f).each(function(){r(this)}),a(".images",g).sortable().disableSelection().bind("sortstop",function(){a.post(d.arrangeUrl,a("input",g).serialize()+"&ajax=true"+e,function(){},"json")}),typeof window.FormData=="function"?a(".afile",f).attr("multiple","true").on("change",function(a){a.preventDefault();var b=this.files.length,c=0;j.empty();for(var e=0;e<b;e++){var f=new FormData;f.append(this.name,this.files[e]),d.csrfToken&&f.append(d.csrfTokenName,d.csrfToken);var g=new XMLHttpRequest;g.open("POST",d.uploadUrl,!0),g.onload=function(){c++;if(this.status==200){var a=JSON.parse(this.response),e=m(a.id,a.preview,a.name,a.description,a.rank);r(e),h.append(e),(d.hasName||d.hasDesc)&&j.append(l(a.id,a.preview,a.name,a.description))}c===b&&(d.hasName||d.hasDesc)&&i.modal("show")},g.send(f)}}):a(".afile",f).on("change",function(b){b.preventDefault(),j.empty(),a.ajax(d.uploadUrl,{data:d.csrfToken?d.csrfTokenName+"="+d.csrfToken:"",files:a(this),iframe:!0,dataType:"json"}).done(function(a){var b=m(a.id,a.preview,a.name,a.description,a.rank);r(b),h.append(b),(d.hasName||d.hasDesc)&&j.append(l(a.id,a.preview,a.name,a.description)),(d.hasName||d.hasDesc)&&i.modal("show")})}),a(".save-changes",i).click(function(b){b.preventDefault(),a.post(d.updateUrl,a("input, textarea",j).serialize()+"&ajax=true"+e,function(b){var c=b.length;for(var e=0;e<c;e++){var h=b[e],j=a("#"+d.wId+"-"+h.id);a("img",j).attr("src",h.src),d.hasName&&a(".caption h5",j).text(h.name),d.hasDesc&&a(".caption p",j).text(h.description)}i.modal("hide"),a(".photo.selected",g).each(function(){a(".photo-select",this).prop("checked",!1)}).removeClass("selected"),a(".select_all",f).prop("checked",!1),p()},"json")}),a(".edit_selected",f).click(function(b){b.preventDefault();var c=0,e=j.empty();return a(".photo.selected",g).each(function(){c++;var b=a(this),f=b.attr("id").substr((d.wId+"-").length),g=a("img",b[0]).attr("src"),h=a(".caption h5",b[0]).text(),i=a(".caption p",b[0]).text();e.append(l(f,g,h,i))}),c>0&&i.modal("show"),!1}),a(".remove_selected",f).click(function(b){b.preventDefault(),a(".photo.selected",g).each(function(){var b=a(this).attr("id").substr((d.wId+"-").length);a.ajax({type:"POST",url:d.deleteUrl,data:"id="+b+e,success:function(c){c=="OK"?a("#"+d.wId+"-"+b).remove():alert(c)}})})}),a(".select_all",f).change(function(){a(this).prop("checked")?a(".photo",g).each(function(){a(".photo-select",this).prop("checked",!0)}).addClass("selected"):a(".photo.selected",g).each(function(){a(".photo-select",this).prop("checked",!1)}).removeClass("selected"),p()});for(var s in d.photos){var t=d.photos[s],u=m(t.id,t.preview,t.name,t.description,t.rank).data("data",t);r(u),h.append(u)}}a.fn.galleryManager=function(a){this.length&&this.each(function(){b(this,a)})}})(jQuery);
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.