Refactoring: nodeElement renders itself with its contents
[fnpeditor.git] / modules / documentToolbar / documentToolbar.js
1 define(['libs/jquery-1.9.1.min', 'libs/underscore-min', 'libs/text!./template.html'], function($, _, template) {
2
3 'use strict';
4
5 return function(sandbox) {
6     
7     var view = {
8         node: $(_.template(template)()),
9         setup: function() {
10             var view = this;
11             
12             this.node.find('button').click(function(e) {
13                 e.stopPropagation();
14                 var btn = $(e.currentTarget),
15                     btnName = btn.attr('data-name'),
16                     meta = btn.attr('data-meta'),
17                     params = {},
18                     command = btnName;
19
20                 if(btn.attr('data-btn-type') === 'toggle') {
21                     command = 'toggle-' + command;
22                     btn.toggleClass('active');
23                     params.toggle = btn.hasClass('active');
24                 }
25
26                 if(btnName === 'new-node') {
27                     command = 'newNodeRequested';
28                     params.wlxmlTag = view.getOption('newTag-tag');
29                     params.wlxmlClass = view.getOption('newTag-class');
30                     if(meta) {
31                         var split = meta.split('/');
32                         params.wlxmlTag = split[0];
33                         params.wlxmlClass = split[1];
34                     }
35                 } else {
36                     params.meta = meta;
37                 }
38
39                 sandbox.publish('command', command, params);
40             });
41         },
42         getOption: function(option) {
43             return this.node.find('.rng-module-documentToolbar-toolbarOption[data-option=' + option +']').val();
44         }
45     };
46     
47     view.setup();
48
49     return {
50         start: function() { sandbox.publish('ready'); },
51         getView: function() { return view.node; },
52         getOption: function(option) { return view.getOption(option); }
53     };
54 };
55
56 });