Fixed unsanited message/user description in document.invoke_and_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.saveDirtyContentModel(message);
49   },
50   
51   update: function(event) {
52     this.model.update();
53   },
54   
55   merge: function(event) {
56     $('#commit-dialog', this.element).jqmShow({callback: this.doMerge.bind(this)});
57   },
58   
59   doMerge: function(message) {
60     this.model.merge(message);
61   },
62   
63   loadRelatedIssues: function(hash) {
64     var self = this;
65     var c = $('#commit-dialog-related-issues');
66
67     $('#commit-dialog-save-button').click(function(event, data)
68     {
69       if ($('#commit-dialog-message').val().match(/^\s*$/)) {
70         $('#commit-dialog-error-empty-message').fadeIn();
71       } else {
72         $('#commit-dialog-error-empty-message').hide();
73         $('#commit-dialog').jqmHide();
74
75         var message = $('#commit-dialog-message').val();
76         $('#commit-dialog-related-issues input:checked')
77           .each(function() { message += ' refs #' + $(this).val(); });
78         console.log("COMMIT APROVED", hash.t);
79         hash.t.callback(message);
80       }
81       return false;
82     });
83
84     $("div.loading-box", c).show();
85     $("div.fatal-error-box", c).hide();
86     $("div.container-box", c).hide();
87     
88     $.getJSON(c.attr('ui:ajax-src') + '?callback=?',
89     function(data, status)
90     {
91         var fmt = '';
92         $(data).each( function() {
93             fmt += '<label><input type="checkbox" checked="checked"';
94             fmt += ' value="' + this.id + '" />' + this.subject +'</label>\n';
95         });
96         $("div.container-box", c).html(fmt);
97         $("div.loading-box", c).hide();
98         $("div.container-box", c).show();        
99     });   
100     
101     hash.w.show();
102   },
103   
104   modelStateChanged: function(property, value) {
105     // Uaktualnia stan przycisków
106     if (value == 'dirty') {
107       this.quickSaveButton.attr('disabled', null);
108       this.commitButton.attr('disabled', null);
109       this.updateButton.attr('disabled', 'disabled');
110       this.mergeButton.attr('disabled', 'disabled');
111     } else if (value == 'synced') {
112       this.quickSaveButton.attr('disabled', 'disabled');
113       this.commitButton.attr('disabled', 'disabled');
114       this.updateButton.attr('disabled', null);
115       this.mergeButton.attr('disabled', null);      
116     } else if (value == 'empty') {
117       this.quickSaveButton.attr('disabled', 'disabled');
118       this.commitButton.attr('disabled', 'disabled');
119       this.updateButton.attr('disabled', 'disabled');
120       this.mergeButton.attr('disabled', 'disabled');
121     }
122   },
123   
124   dispose: function() {
125     $('#action-quick-save', this.element).unbind('click.editorview');
126     $('#action-commit', this.element).unbind('click.editorview');
127     $('#action-update', this.element).unbind('click.editorview');
128     $('#action-merge', this.element).unbind('click.editorview');
129
130     this.model.removeObserver(this);
131     this._super();
132   }
133 });