7f03998547b901f7557da48524ae92220bcbf17b
[redakcja.git] / project / static / js / views / button_toolbar.js
1 /*globals View render_template scriptletCenter*/
2 var ButtonToolbarView = View.extend({
3     _className: 'ButtonToolbarView',
4     template: null,
5     buttons: null,
6   
7     init: function(element, model, parent, template) {
8         this._super(element, model, null);
9         this.parent = parent;
10         this.template = 'button-toolbar-view-template';
11     
12         this.model.addObserver(this, 'buttons', this.modelButtonsChanged.bind(this));
13         this.buttons = this.model.get('buttons');
14         this.model.load();
15         this.render();
16     },
17   
18     modelButtonsChanged: function(property, value) {
19         this.set('buttons', value);
20         this.render();
21     },
22   
23     render: function() {
24         $('.buttontoolbarview-tab', this.element).unbind('click.buttontoolbarview');
25         $('.buttontoolbarview-button', this.element).unbind('click.buttontoolbarview');
26         var self = this;
27     
28         this.element.html(render_template(this.template, this));
29     
30         $('.buttontoolbarview-tab', this.element).bind('click.buttontoolbarview', function() {
31             var groupIndex = $(this).attr('ui:groupindex');
32             $('.buttontoolbarview-group', self.element).each(function() {
33                 if ($(this).attr('ui:groupindex') == groupIndex) {
34                     $(this).show();
35                 } else {
36                     $(this).hide();
37                 }
38             });
39             $(self.element).trigger('resize');
40         });
41     
42         $('.buttontoolbarview-button', this.element).
43         bind('click.buttontoolbarview', this.buttonPressed.bind(this) );
44             
45         $(this.element).trigger('resize');
46     },
47
48     buttonPressed: function(event)
49     {
50         var self = this;
51         var target = event.target;
52         
53         var groupIndex = parseInt($(target).attr('ui:groupindex'), 10);
54         var buttonIndex = parseInt($(target).attr('ui:buttonindex'), 10);
55         var button = this.get('buttons')[groupIndex].buttons[buttonIndex];
56         var scriptletId = button.scriptlet_id;
57         var params = eval('(' + button.params + ')'); // To nie powinno być potrzebne
58
59         console.log('Executing', scriptletId, 'with params', params);
60         try {
61             self.parent.freeze('Wykonuję akcję...');
62             setTimeout(function() {
63                 scriptletCenter.scriptlets[scriptletId](self.parent, params);
64                 self.parent.unfreeze();
65             }, 10);
66         } catch(e) {
67             console.log("Scriptlet", scriptletId, "failed.", e);
68         }
69
70     },
71   
72     dispose: function() {
73         $('.buttontoolbarview-tab', this.element).unbind('click.buttontoolbarview');
74         $('.buttontoolbarview-button', this.element).unbind('click.buttontoolbarview');
75         this._super();
76     }
77 });
78