summaryrefslogtreecommitdiff
path: root/lib/gallery.in_place_edit.js
blob: c10400e3cb7f117061cdfabbcb016c8e001e48ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
(function($) {
   $.widget("ui.gallery_in_place_edit",  {
     _init: function() {
       var self = this;
       this.element.click(function(event) {
         event.preventDefault();
         self._show(event.currentTarget);
         return false;
       });
     },

     _show: function(target) {
       if ($(this).data("gallery_in_place_edit") == true) {
         return;
       }
       $(this).data("gallery_in_place_edit", true);
       var self = this;
       var tag_width = $(target).width();
       $(self).data("tag_width", tag_width);

       var form = $("#g-in-place-edit-form");
       if (form.length > 0) {
         self._cancel();
       }

       $.get(self.options.form_url.replace("__ID__", $(target).attr('rel')), function(data) {
         var parent = $(target).parent();
         parent.children().hide();
         parent.append(data);
         parent.find("form :text")
           .width(tag_width)
           .focus();
         $(".g-short-form").gallery_short_form();
         parent.find("form .g-cancel").click(function(event) {
           self._cancel();
           event.preventDefault();
           return false;
         });
         self._ajaxify_edit();
       });

     },

     _cancel: function() {
       var parent = $("#g-in-place-edit-form").parent();
       $(parent).find("form").remove();
       $(parent).children().show();
       $("#g-in-place-edit-message").remove();
       $(this).data("gallery_in_place_edit", false);
     },

     _ajaxify_edit: function() {
       var self = this;
       var form = $("#g-in-place-edit-form");
       $(form).ajaxForm({
         dataType: "json",
         success: function(data) {
           if (data.result == "success") {
             window.location.reload();
           } else {
             var parent = $(form).parent();
             $(form).replaceWith(data.form);
             var width = $(self).data("tag_width");
             $(parent).find("form :text")
               .width(width)
               .focus();
             $(".g-short-form").gallery_short_form();
             $(parent).find("form .g-cancel").click(function(event) {
               self._cancel();
               event.preventDefault();
               return false;
             });
             self._ajaxify_edit();
           }
         }
       });
     }
   });

   $.extend($.ui.gallery_in_place_edit,  {
     defaults: {}
   });
})(jQuery);