remove empty list
[fnpeditor.git] / src / editor / modules / documentToolbar / documentToolbar.js
1 define(['libs/jquery', 'libs/underscore', 'modules/documentToolbar/actionView', 'libs/text!./template.html'], function($, _, actionView, template) {
2
3 'use strict';
4
5
6 return function(sandbox) {
7     
8     var addedActions = [],
9         contextParams = {},
10         document, canvas;
11
12     var view = {
13         node: $(_.template(template)()),
14         getOption: function(option) {
15             return this.node.find('.rng-module-documentToolbar-toolbarOption[data-option=' + option +']').val();
16         },
17         addAction: function(group, actionDescription) {
18             var action = sandbox.createAction(actionDescription.actionName, actionDescription.actionConfig),
19                 view;
20             addedActions.push(action);
21             view = actionView.create(action);
22             
23             _.pairs(contextParams).forEach(function(pair) {
24                 var name = pair[0],
25                     value = pair[1];
26                 action.updateContextParam(name, value);
27             });
28
29             group.append(view.dom);
30             action.on('actionExecuted', function(ret) {
31                 sandbox.publish('actionExecuted', action, ret);
32             });
33
34             view.on('hover', function() {
35                 sandbox.publish('actionHovered', action);
36             });
37             view.on('leave', function() {
38                 sandbox.publish('actionOff', action);
39             });
40         },
41         addActionsGroup: function() {
42             var div = $('<div>');
43             div.addClass('rng-module-documentToolbar-toolbarGroup');
44             this.node.append(div);
45             return div;
46         }
47     };
48     
49     var setContextParam = function(what, ctx) {
50         contextParams[what] = ctx;
51         addedActions.forEach(function(action) {
52             action.updateContextParam(what, contextParams[what]);
53         });
54     };
55
56     sandbox.registerKeyHandler('keydown', function(e) {
57         if(e.keyCode === 17) {
58             addedActions.forEach(function(action) {
59                 action.updateKeyParam('ctrl', true);
60             });
61         }
62     });
63     sandbox.registerKeyHandler('keyup', function(e) {
64         if(e.keyCode === 17) {
65             addedActions.forEach(function(action) {
66                 action.updateKeyParam('ctrl', false);
67             });
68         }
69     });
70
71     return {
72         start: function() {
73             var config = sandbox.getConfig().toolbar || {};
74             config.forEach(function(actionsGroup) {
75                 var group = view.addActionsGroup();
76                 actionsGroup.forEach(function(actionDescription) {
77                     if(typeof actionDescription === 'string') {
78                         actionDescription = {actionName: actionDescription, actionConfig: {}};
79                     }
80                     view.addAction(group, actionDescription);
81                 });
82             });
83             sandbox.publish('ready');
84         },
85         getView: function() { return view.node; },
86         setDocumentFragment: function(fragment) {
87             if(!document) {
88                 document = fragment.document;
89                 document.on('operationEnd', function() {
90                     setContextParam('document', document);
91                 });
92                 setContextParam('document', document);
93             }
94             setContextParam('fragment', fragment);
95
96         },
97         setCanvas: function(_canvas) {
98             setContextParam('canvas', _canvas);
99             if(!canvas) {
100                 canvas = _canvas;
101                 canvas.on('changed', function() {
102                     setContextParam('canvas', _canvas);
103                 });
104             }
105         },
106         getOption: function(option) { return view.getOption(option); }
107     };
108 };
109
110 });