82b6dca8fc27c4c766e358b48f73d40dc165e9e1
[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         contextDefer = {},
11         duringClick = false,
12         document, canvas;
13
14     var view = {
15         node: $(_.template(template)()),
16         getOption: function(option) {
17             return this.node.find('.rng-module-documentToolbar-toolbarOption[data-option=' + option +']').val();
18         },
19         addAction: function(group, actionDescription) {
20             var action = sandbox.createAction(actionDescription.actionName, actionDescription.actionConfig),
21                 view;
22             addedActions.push(action);
23             view = actionView.create(action);
24             
25             _.pairs(contextParams).forEach(function(pair) {
26                 var name = pair[0],
27                     value = pair[1];
28                 action.updateContextParam(name, value);
29             });
30
31             group.append(view.dom);
32             view.on('actionExecuted', function(action, ret) {
33                 sandbox.publish('actionExecuted', action, ret);
34                 duringClick = false;
35                 _.pairs(contextDefer).forEach(function(pair) {
36                     var what = pair[0],
37                         deferred = pair[1];
38                     if(deferred) {
39                         refreshContextParam(what);
40                     }
41                 });
42             });
43             view.on('mousedown', function() {
44                 duringClick = true;
45             });
46
47             view.on('hover', function() {
48                 sandbox.publish('actionHovered', action);
49             });
50             view.on('leave', function() {
51                 sandbox.publish('actionOff', action);
52             });
53         },
54         addActionsGroup: function() {
55             var div = $('<div>');
56             div.addClass('rng-module-documentToolbar-toolbarGroup');
57             this.node.append(div);
58             return div;
59         }
60     };
61     
62     var setContextParam = function(what, ctx) {
63         contextParams[what] = ctx;
64         if(duringClick) {
65             contextDefer[what] = true;
66         } else {
67             refreshContextParam(what);
68         }
69     };
70
71     var refreshContextParam = function(what) {
72         addedActions.forEach(function(action) {
73             action.updateContextParam(what, contextParams[what]);
74         });
75     };
76
77     sandbox.registerKeyHandler('keydown', function(e) {
78         if(e.keyCode === 17) {
79             addedActions.forEach(function(action) {
80                 action.updateKeyParam('ctrl', true);
81             });
82         }
83     });
84     sandbox.registerKeyHandler('keyup', function(e) {
85         if(e.keyCode === 17) {
86             addedActions.forEach(function(action) {
87                 action.updateKeyParam('ctrl', false);
88             });
89         }
90     });
91
92     return {
93         start: function() {
94             var config = sandbox.getConfig().toolbar || {};
95             config.forEach(function(actionsGroup) {
96                 var group = view.addActionsGroup();
97                 actionsGroup.forEach(function(actionDescription) {
98                     if(typeof actionDescription === 'string') {
99                         actionDescription = {actionName: actionDescription, actionConfig: {}};
100                     }
101                     view.addAction(group, actionDescription);
102                 });
103             });
104             sandbox.publish('ready');
105         },
106         getView: function() { return view.node; },
107         setDocumentFragment: function(fragment) {
108             if(!document) {
109                 document = fragment.document;
110                 document.on('operationEnd', function() {
111                     setContextParam('document', document);
112                 });
113             }
114             setContextParam('fragment', fragment);
115
116         },
117         setCanvas: function(_canvas) {
118             setContextParam('canvas', _canvas);
119             if(!canvas) {
120                 canvas = _canvas;
121                 canvas.on('changed', function() {
122                     setContextParam('canvas', _canvas);
123                 });
124             }
125         },
126         getOption: function(option) { return view.getOption(option); }
127     };
128 };
129
130 });