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