1 define(['libs/jquery', 'libs/underscore', 'modules/documentToolbar/actionView', 'libs/text!./template.html'], function($, _, actionView, template) {
6 return function(sandbox) {
15 node: $(_.template(template)()),
16 getOption: function(option) {
17 return this.node.find('.rng-module-documentToolbar-toolbarOption[data-option=' + option +']').val();
19 addAction: function(group, actionDescription) {
20 var action = sandbox.createAction(actionDescription.actionName, actionDescription.actionConfig),
22 addedActions.push(action);
23 view = actionView.create(action);
25 _.pairs(contextParams).forEach(function(pair) {
28 action.updateContextParam(name, value);
31 group.append(view.dom);
32 view.on('actionExecuted', function(action, ret) {
33 sandbox.publish('actionExecuted', action, ret);
35 _.pairs(contextDefer).forEach(function(pair) {
39 refreshContextParam(what);
43 view.on('mousedown', function() {
47 view.on('hover', function() {
48 sandbox.publish('actionHovered', action);
50 view.on('leave', function() {
51 sandbox.publish('actionOff', action);
54 addActionsGroup: function() {
56 div.addClass('rng-module-documentToolbar-toolbarGroup');
57 this.node.append(div);
62 var setContextParam = function(what, ctx) {
63 contextParams[what] = ctx;
65 contextDefer[what] = true;
67 refreshContextParam(what);
71 var refreshContextParam = function(what) {
72 addedActions.forEach(function(action) {
73 action.updateContextParam(what, contextParams[what]);
77 sandbox.registerKeyHandler('keydown', function(e) {
78 if(e.keyCode === 17) {
79 addedActions.forEach(function(action) {
80 action.updateKeyParam('ctrl', true);
84 sandbox.registerKeyHandler('keyup', function(e) {
85 if(e.keyCode === 17) {
86 addedActions.forEach(function(action) {
87 action.updateKeyParam('ctrl', false);
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: {}};
101 view.addAction(group, actionDescription);
104 sandbox.publish('ready');
106 getView: function() { return view.node; },
107 setDocumentFragment: function(fragment) {
109 document = fragment.document;
110 document.on('operationEnd', function() {
111 setContextParam('document', document);
114 setContextParam('fragment', fragment);
117 setCanvas: function(_canvas) {
118 setContextParam('canvas', _canvas);
121 canvas.on('changed', function() {
122 setContextParam('canvas', _canvas);
126 getOption: function(option) { return view.getOption(option); }