33f95a6baefbb918c63805de391c842a43ca4c3c
[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)({wlxmlUtils: wlxmlUtils, 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                     _.extend(params, {ctrlKey: e.ctrlKey});
54
55                     sandbox.publish('command', command, params);
56                 }
57             });
58         },
59         getOption: function(option) {
60             return this.node.find('.rng-module-documentToolbar-toolbarOption[data-option=' + option +']').val();
61         }
62     };
63     
64     var myHandlers = {
65         templatesBtn: function() {
66             if(currentNode && currentNode.nodeType === Node.ELEMENT_NODE) {
67                 var templateId = parseInt(view.node.find('[data-name=templates-select]').val(), 10);
68                 documentTemplates.forEach(function(template) {
69                     if(template.id === templateId) {
70                         var toAdd = currentNode.document.createDocumentNode(template.content);
71                         currentNode.after(toAdd);
72                     }
73                 });
74             }
75         }
76     };
77
78     view.setup();
79
80     return {
81         start: function() { sandbox.publish('ready'); },
82         getView: function() { return view.node; },
83         setNodeElement: function(node) {
84             currentNode = node;
85         },
86         getOption: function(option) { return view.getOption(option); }
87     };
88 };
89
90 });