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