Dodanie ButtonToolbarView.
[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     
27     this.element.html(render_template(this.template, this));
28     
29     $('.buttontoolbarview-tab', this.element).bind('click.buttontoolbarview', function() {
30       var groupIndex = $(this).attr('ui:groupindex');
31       $('.buttontoolbarview-group', this.element).each(function() {
32         if ($(this).attr('ui:groupindex') == groupIndex) {
33           $(this).show();
34         } else {
35           $(this).hide();
36         }
37       });
38     });
39     
40     var self = this;
41     $('.buttontoolbarview-button', this.element).bind('click.buttontoolbarview', function() {
42       var groupIndex = parseInt($(this).attr('ui:groupindex'), 10);
43       var buttonIndex = parseInt($(this).attr('ui:buttonindex'), 10);
44       var button = self.get('buttons')[groupIndex].buttons[buttonIndex];
45       var scriptletId = button.scriptlet_id;
46       var params = eval('(' + button.params + ')'); // To nie powinno być potrzebne
47       console.log('Executing', scriptletId, 'with params', params);
48       scriptletCenter[scriptletId](self.parent, params);
49     });
50   },
51   
52   dispose: function() {
53     $('.buttontoolbarview-tab', this.element).unbind('click.buttontoolbarview');
54     $('.buttontoolbarview-button', this.element).unbind('click.buttontoolbarview');
55     this._super();
56   }
57 });
58