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