Merge branch 'master' of stigma.nowoczesnapolska.org.pl:platforma
[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         $('#commit-dialog', this.element).
23         jqm({
24             modal: true,
25             onShow: this.loadRelatedIssues.bind(this)
26         });
27     
28         $('#commit-dialog-cancel-button', this.element).click(function() {
29             $('#commit-dialog-error-empty-message').hide();
30             $('#commit-dialog').jqmHide();
31         });
32         
33         $('#commit-dialog-save-button').click(function(event, data)
34         {
35             if ($('#commit-dialog-message').val().match(/^\s*$/)) {
36                 $('#commit-dialog-error-empty-message').fadeIn();
37             } else {
38                 $('#commit-dialog-error-empty-message').hide();
39                 $('#commit-dialog').jqmHide();
40
41                 var message = $('#commit-dialog-message').val();
42                 $('#commit-dialog-related-issues input:checked')
43                 .each(function() {
44                     message += ' refs #' + $(this).val();
45                 });
46                 
47                 var ctx = $('#commit-dialog').data('context');
48                 console.log("COMMIT APROVED", ctx);
49                 ctx.callback(message);
50             }
51             return false;
52         });
53         
54     
55         // $('#split-dialog').jqm({
56         //      modal: true,
57         //      onShow: $.fbind(self, self.loadSplitDialog)
58         //  }).
59         //  jqmAddClose('button.dialog-close-button');
60     
61         this.model.load();
62     },
63   
64     quickSave: function(event) {
65         this.model.saveDirtyContentModel();
66     },
67   
68     commit: function(event) {
69         $('#commit-dialog', this.element).jqmShow({
70             callback: this.doCommit.bind(this)
71             });
72     },
73   
74     doCommit: function(message) {
75         this.model.saveDirtyContentModel(message);
76     },
77   
78     update: function(event) {
79         this.model.update();
80     },
81   
82     merge: function(event) {
83         $('#commit-dialog', this.element).jqmShow({
84             callback: this.doMerge.bind(this)
85             });
86     },
87   
88     doMerge: function(message) {
89         this.model.merge(message);
90     },
91   
92     loadRelatedIssues: function(hash) {
93         var self = this;
94         var c = $('#commit-dialog-related-issues');
95         
96         $('#commit-dialog').data('context', hash.t);
97
98         $("div.loading-box", c).show();
99         $("div.fatal-error-box", c).hide();
100         $("div.container-box", c).hide();
101     
102         $.getJSON(c.attr('ui:ajax-src') + '?callback=?',
103             function(data, status)
104             {
105                 var fmt = '';
106                 $(data).each( function() {
107                     fmt += '<label><input type="checkbox" checked="checked"';
108                     fmt += ' value="' + this.id + '" />' + this.subject +'</label>\n';
109                 });
110                 $("div.container-box", c).html(fmt);
111                 $("div.loading-box", c).hide();
112                 $("div.container-box", c).show();
113             });
114     
115         hash.w.show();
116     },
117   
118     modelStateChanged: function(property, value) {
119         // Uaktualnia stan przycisków
120         if (value == 'dirty') {
121             this.quickSaveButton.attr('disabled', null);
122             this.commitButton.attr('disabled', null);
123             this.updateButton.attr('disabled', 'disabled');
124             this.mergeButton.attr('disabled', 'disabled');
125         } else if (value == 'synced' || value == 'unsynced') {
126             this.quickSaveButton.attr('disabled', 'disabled');
127             this.commitButton.attr('disabled', 'disabled');
128             this.updateButton.attr('disabled', null);
129             this.mergeButton.attr('disabled', null);
130             this.unfreeze();
131         } else if (value == 'empty') {
132             this.quickSaveButton.attr('disabled', 'disabled');
133             this.commitButton.attr('disabled', 'disabled');
134             this.updateButton.attr('disabled', 'disabled');
135             this.mergeButton.attr('disabled', 'disabled');
136         } else if (value == 'error') {
137             this.freeze(this.model.get('error'));
138             this.quickSaveButton.attr('disabled', 'disabled');
139             this.commitButton.attr('disabled', 'disabled');
140             this.updateButton.attr('disabled', 'disabled');
141             this.mergeButton.attr('disabled', 'disabled');
142             
143         }
144     },
145   
146     dispose: function() {
147         $('#action-quick-save', this.element).unbind('click.editorview');
148         $('#action-commit', this.element).unbind('click.editorview');
149         $('#action-update', this.element).unbind('click.editorview');
150         $('#action-merge', this.element).unbind('click.editorview');
151
152         this.model.removeObserver(this);
153         this._super();
154     }    
155 });