check download url for duplicated when refreshing covers
[redakcja.git] / redakcja / static / js / wiki_img / view_editor_objects.js
1 (function($){
2
3     function ObjectsPerspective(options){
4
5         var old_callback = options.callback;
6
7         options.callback = function(){
8             var self = this;
9
10             self.$tag_name = $('#objects-editor .tag-name');
11             self.$toolbar = $('#objects-editor .toolbar');
12             self.$scrolled = $('#objects-editor .scrolled');
13             self.$objects_list = $('#objects-editor .objects-list');
14
15             self.x1 = null;
16             self.x2 = null;
17             self.y1 = null;
18             self.y2 = null;
19
20             if (!CurrentDocument.readonly) {
21                 self.ias = $('#objects-editor img.area-selectable').imgAreaSelect({ handles: true, onSelectEnd: self._fillCoords(self), instance: true });
22                 $('#objects-editor .add').click(self._addObject(self));
23
24                 $('.delete', self.$objects_list).live('click', function() {
25                     $(this).prev().trigger('click');
26                     if (window.confirm("Czy na pewno chcesz usunąć ten obiekt?")) {
27                         $(this).prev().remove();
28                         $(this).remove();
29                         self._refreshLayout();
30                     }
31                     self._resetSelection();
32                     return false;
33                 });
34             }
35
36             $('.image-object', self.$objects_list).live('click', function(){
37                 $('.active', self.$objects_list).removeClass('active');
38                 $(this).addClass('active');
39                 var coords = $(this).data('coords');
40                 if (coords) {
41                     self.ias.setSelection.apply(self.ias, coords);
42                     self.ias.setOptions({ show: true });
43                 }
44                 else {
45                     self._resetSelection();
46                 }
47             });
48
49             old_callback.call(this);
50         };
51
52         $.wiki.Perspective.call(this, options);
53     };
54
55     ObjectsPerspective.prototype = new $.wiki.Perspective();
56
57     ObjectsPerspective.prototype.freezeState = function(){
58
59     };
60
61     ObjectsPerspective.prototype._resetSelection = function() {
62         var self = this;
63         self.x1 = self.x2 = self.y1 = self.y2 = null;
64         self.ias.setOptions({ hide: true });
65     }
66
67     ObjectsPerspective.prototype._refreshLayout = function() {
68         this.$scrolled.css({top: this.$toolbar.height()});
69     };
70
71     ObjectsPerspective.prototype._push = function(name, x1, y1, x2, y2) {
72         var $e = $('<span class="image-object"></span>')
73         $e.text(name);
74         if (x1 !== null)
75             $e.data('coords', [x1, y1, x2, y2]);
76         this.$objects_list.append($e);
77         this.$objects_list.append('<span class="delete">(x) </span>');
78         this._refreshLayout();
79     }
80
81
82     ObjectsPerspective.prototype._addObject = function(self) {
83         return function() {
84             outputs = [];
85             chunks = self.$tag_name.val().split(',');
86             for (i in chunks) {
87                 item = chunks[i].trim();
88                 if (item == '')
89                     continue;
90                 outputs.push(item.trim());
91             }
92             output = outputs.join(', ');
93
94             self._push(output, self.x1, self.y1, self.x2, self.y2);
95             self._resetSelection();
96         }
97     }
98
99     ObjectsPerspective.prototype._fillCoords = function(self) {
100         return function(img, selection) {
101             $('.active', self.$objects_list).removeClass('active');
102             if (selection.x1 != selection.x2 && selection.y1 != selection.y2) {
103                 self.x1 = selection.x1;
104                 self.x2 = selection.x2;
105                 self.y1 = selection.y1;
106                 self.y2 = selection.y2;
107             }
108             else {
109                 self.x1 = self.x2 = self.y1 = self.y2 = null;
110             }
111         }
112     }
113
114     ObjectsPerspective.prototype.onEnter = function(success, failure){
115         var self = this;
116         this.$objects_list.children().remove();
117
118         $.each(this.doc.getImageItems('object'), function(i, e) {
119             self._push.apply(self, e);
120         });
121
122         if (this.x1 !== null)
123             this.ias.setOptions({enable: true, show: true});
124         else
125             this.ias.setOptions({enable: true});
126
127         $.wiki.Perspective.prototype.onEnter.call(this);
128
129     };
130
131     ObjectsPerspective.prototype.onExit = function(success, failure){
132         var self = this;
133         var objects = [];
134         this.$objects_list.children(".image-object").each(function(i, e) {
135             var args = $(e).data('coords');
136             if (!args)
137                 args = [null, null, null, null];
138             args.unshift($(e).text());
139             objects.push(args);
140         })
141         self.doc.setImageItems('object', objects);
142
143         this.ias.setOptions({disable: true, hide: true});
144
145     };
146
147     $.wiki.ObjectsPerspective = ObjectsPerspective;
148
149 })(jQuery);