84361adf95c860f1be3531c02f9ca8f44111169e
[fnpeditor.git] / src / editor / modules / documentToolbar / documentToolbar.js
1 define(['libs/jquery', 'libs/underscore', 'utils/wlxml', 'libs/text!./template.html'], function($, _, wlxmlUtils, template) {
2
3 'use strict';
4 /* globals Node */
5
6 return function(sandbox) {
7     
8     var documentTemplates = sandbox.getBootstrappedData(),
9         currentNode;
10
11     var view = {
12         node: $(_.template(template)({tagNames: wlxmlUtils.wlxmlTagNames, classNames: wlxmlUtils.wlxmlClassNames, templates: documentTemplates})),
13         setup: function() {
14             var view = this;
15             
16             this.node.find('button').click(function(e) {
17                 e.stopPropagation();
18
19                 var btn = $(e.currentTarget),
20                     btnName = btn.attr('data-name'),
21                     meta = btn.attr('data-meta'),
22                     params = {},
23                     command = btnName;
24
25                 if(myHandlers[btnName]) {
26                     myHandlers[btnName](btn);
27                 } else {
28                     if(btn.attr('data-btn-type') === 'toggle') {
29                         command = 'toggle-' + command;
30                         btn.toggleClass('active');
31                         params.toggle = btn.hasClass('active');
32                     }
33
34                     if(btnName === 'new-node') {
35                         command = 'newNodeRequested';
36                         params.wlxmlTag = view.getOption('newTag-tag');
37                         params.wlxmlClass = view.getOption('newTag-class');
38                         if(meta) {
39                             var split = meta.split('/');
40                             params.wlxmlTag = split[0];
41                             params.wlxmlClass = split[1];
42                         }
43                     } else {
44                         params.meta = meta;
45                     }
46
47                     if(command === 'undo' || command === 'redo') {
48                         params.callback = function(disable) {
49                             btn.attr('disabled', !disable);
50                         };
51                     }
52
53                     sandbox.publish('command', command, params);
54                 }
55             });
56         },
57         getOption: function(option) {
58             return this.node.find('.rng-module-documentToolbar-toolbarOption[data-option=' + option +']').val();
59         }
60     };
61     
62     var myHandlers = {
63         templatesBtn: function() {
64             if(currentNode && currentNode.nodeType === Node.ELEMENT_NODE) {
65                 var templateId = parseInt(view.node.find('[data-name=templates-select]').val(), 10);
66                 documentTemplates.forEach(function(template) {
67                     if(template.id === templateId) {
68                         var toAdd = currentNode.document.createDocumentNode(template.content);
69                         currentNode.after(toAdd);
70                     }
71                 });
72             }
73         }
74     };
75
76     view.setup();
77
78     return {
79         start: function() { sandbox.publish('ready'); },
80         getView: function() { return view.node; },
81         setNodeElement: function(node) {
82             currentNode = node;
83         },
84         getOption: function(option) { return view.getOption(option); }
85     };
86 };
87
88 });