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