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