Refaktoring funkcji związanych z przetwarzaniem XML po stronie klienta.
[redakcja.git] / platforma / static / js / views / editor.js
1 /*global View render_template panels */
2 var EditorView = View.extend({
3     _className: 'EditorView',
4     element: null,
5     model: null,
6     template: null,
7   
8     init: function(element, model, template) {
9         this._super(element, model, template);
10     
11         this.quickSaveButton = $('#action-quick-save', this.element).bind('click.editorview', this.quickSave.bind(this));
12         this.commitButton = $('#action-commit', this.element).bind('click.editorview', this.commit.bind(this));
13         this.updateButton = $('#action-update', this.element).bind('click.editorview', this.update.bind(this));
14         this.mergeButton = $('#action-merge', this.element).bind('click.editorview', this.merge.bind(this));
15     
16         this.model.addObserver(this, 'state', this.modelStateChanged.bind(this));
17         this.modelStateChanged('state', this.model.get('state'));        
18
19         this.splitView = new SplitView('#splitview', doc);
20         
21         // Inicjalizacja okien jQuery Modal
22         this.commitDialog = new CommitDialog( $('#commit-dialog') );                  
23         
24     
25         // $('#split-dialog').jqm({
26         //      modal: true,
27         //      onShow: $.fbind(self, self.loadSplitDialog)
28         //  }).
29         //  jqmAddClose('button.dialog-close-button');
30     
31         this.model.load();
32     },
33   
34     quickSave: function(event) {
35         this.model.saveDirtyContentModel();
36     },
37   
38     commit: function(event) 
39     {
40         this.commitDialog.show( this.doCommit.bind(this) )
41         
42     },
43   
44     doCommit: function(message) {
45         this.model.saveDirtyContentModel(message);
46     },
47   
48     update: function(event) {
49         this.model.update();
50     },
51   
52     merge: function(event) {
53         this.commitDialog.show( this.doMerge.bind(this) )        
54     },
55   
56     doMerge: function(message) {
57         this.model.merge(message);
58     },
59   
60     /*loadRelatedIssues: function(hash) {
61         var self = this;
62         var c = $('#commit-dialog-related-issues');
63         
64         $('#commit-dialog').data('context', hash.t);
65
66         $("div.loading-box", c).show();
67         $("div.fatal-error-box", c).hide();
68         $("div.container-box", c).hide();
69     
70         $.getJSON(c.attr('ui:ajax-src') + '?callback=?',
71             function(data, status)
72             {
73                 var fmt = '';
74                 $(data).each( function() {
75                     fmt += '<label><input type="checkbox" checked="checked"';
76                     fmt += ' value="' + this.id + '" />' + this.subject +'</label>\n';
77                 });
78                 $("div.container-box", c).html(fmt);
79                 $("div.loading-box", c).hide();
80                 $("div.container-box", c).show();
81             });
82     
83         hash.w.show();
84     }, */
85   
86     modelStateChanged: function(property, value) {
87         // Uaktualnia stan przycisków
88         if (value == 'dirty') {
89             this.quickSaveButton.attr('disabled', null);
90             this.commitButton.attr('disabled', null);
91             this.updateButton.attr('disabled', 'disabled');
92             this.mergeButton.attr('disabled', 'disabled');
93         } else if (value == 'synced' || value == 'unsynced') {
94             this.quickSaveButton.attr('disabled', 'disabled');
95             this.commitButton.attr('disabled', 'disabled');
96             this.updateButton.attr('disabled', null);
97             this.mergeButton.attr('disabled', null);
98             this.unfreeze();
99         } else if (value == 'empty') {
100             this.quickSaveButton.attr('disabled', 'disabled');
101             this.commitButton.attr('disabled', 'disabled');
102             this.updateButton.attr('disabled', 'disabled');
103             this.mergeButton.attr('disabled', 'disabled');
104         } else if (value == 'error') {
105             this.freeze(this.model.get('error'));
106             this.quickSaveButton.attr('disabled', 'disabled');
107             this.commitButton.attr('disabled', 'disabled');
108             this.updateButton.attr('disabled', 'disabled');
109             this.mergeButton.attr('disabled', 'disabled');
110             
111         }
112     },
113   
114     dispose: function() {
115         $('#action-quick-save', this.element).unbind('click.editorview');
116         $('#action-commit', this.element).unbind('click.editorview');
117         $('#action-update', this.element).unbind('click.editorview');
118         $('#action-merge', this.element).unbind('click.editorview');
119
120         this.model.removeObserver(this);
121         this._super();
122     }    
123 });
124
125
126 var AbstractDialog = Class.extend({
127     _className: 'AbstractDialog',
128
129     init: function($element, modal, overlay)
130     {
131         this.$window = $element;
132         this.$window.jqm({
133             modal: modal || true,
134             overlay: overlay || 80,
135             // toTop: true,
136             onShow: this.onShow.bind(this),
137             onHide: this.onHide.bind(this)
138         });
139
140         this.reset();        
141
142         $('.cancel-button', this.$window).click(this.cancel.bind(this));
143         $('.save-button', this.$window).click(this.accept.bind(this));
144     },
145
146     onShow: function(hash)
147     {
148         hash.w.show();
149     },
150
151     onHide: function(hash)
152     {
153         hash.w.hide();
154         hash.o.remove();
155     },
156
157     reset: function() {
158         this.acceptCallback = null;
159         this.cancelCallback = null;
160         this.errors = [];
161         
162         $('.error-messages-box', this.$window).html('').hide();
163
164         this.userData = {};
165     },
166
167     show: function(acall, ccall) {
168         this.acceptCallback = acall;
169         this.cancelCallback = ccall;
170
171         // do the show
172         this.$window.jqmShow();
173     },
174
175     cancel: function() {
176         this.$window.jqmHide();
177         if(this.cancelCallback) this.cancelCallback(this);
178         this.reset();
179     },
180
181     accept: function()
182     {
183         this.errors = [];
184         
185         if(!this.validate()) {
186             this.displayErrors();
187             return;
188         }
189
190         this.$window.jqmHide();
191
192         if(this.acceptCallback) 
193             this.acceptCallback(this);
194
195         this.reset();        
196     },
197
198     validate: function() {
199         return true;
200     },
201
202     displayErrors: function() {
203         var errorDiv = $('.error-messages-box', this.$window);
204         if(errorDiv.length > 0) {
205             var html = '';
206             $.each(this.errors, function() {
207                 html += '<li>' + this + '</li>';
208             });
209             errorDiv.html('<ul>' + html + '</ul>');
210             errorDiv.show();
211             console.log('Validation errors:', html);
212         }
213         else
214             throw this.errors;
215     }
216
217 });
218
219 var CommitDialog = AbstractDialog.extend({
220     _className: 'CommitDialog',
221
222     validate: function()
223     {
224         var message = $('textarea.commit-message', this.$window).val();
225
226         if( message.match(/^\s*$/) ) {
227             this.errors.push("Message can't be empty.");
228             return false;
229         }
230
231         // append refs
232         $('.related-issues-fields input:checked', this.$window).each(function() {
233             message += ' refs #' + $(this).val();
234         });
235
236         this.userData.message = message;
237         return this._super();
238      }
239  });
240
241