X-Git-Url: https://git.mdrn.pl/fnpeditor.git/blobdiff_plain/6757e2f4fe80bfdc197d4963c464bf2b3a47f66b..9e5d2dd07732d54da694d199c551b1818eeee3da:/src/editor/modules/documentToolbar/documentToolbar.js?ds=inline diff --git a/src/editor/modules/documentToolbar/documentToolbar.js b/src/editor/modules/documentToolbar/documentToolbar.js index 75fcc72..33f95a6 100644 --- a/src/editor/modules/documentToolbar/documentToolbar.js +++ b/src/editor/modules/documentToolbar/documentToolbar.js @@ -1,48 +1,59 @@ define(['libs/jquery', 'libs/underscore', 'utils/wlxml', 'libs/text!./template.html'], function($, _, wlxmlUtils, template) { 'use strict'; +/* globals Node */ return function(sandbox) { + var documentTemplates = sandbox.getBootstrappedData(), + currentNode; + var view = { - node: $(_.template(template)({tagNames: wlxmlUtils.wlxmlTagNames, classNames: wlxmlUtils.wlxmlClassNames})), + node: $(_.template(template)({wlxmlUtils: wlxmlUtils, templates: documentTemplates})), setup: function() { var view = this; this.node.find('button').click(function(e) { e.stopPropagation(); + var btn = $(e.currentTarget), btnName = btn.attr('data-name'), meta = btn.attr('data-meta'), params = {}, command = btnName; - if(btn.attr('data-btn-type') === 'toggle') { - command = 'toggle-' + command; - btn.toggleClass('active'); - params.toggle = btn.hasClass('active'); - } + if(myHandlers[btnName]) { + myHandlers[btnName](btn); + } else { + if(btn.attr('data-btn-type') === 'toggle') { + command = 'toggle-' + command; + btn.toggleClass('active'); + params.toggle = btn.hasClass('active'); + } - if(btnName === 'new-node') { - command = 'newNodeRequested'; - params.wlxmlTag = view.getOption('newTag-tag'); - params.wlxmlClass = view.getOption('newTag-class'); - if(meta) { - var split = meta.split('/'); - params.wlxmlTag = split[0]; - params.wlxmlClass = split[1]; + if(btnName === 'new-node') { + command = 'newNodeRequested'; + params.wlxmlTag = view.getOption('newTag-tag'); + params.wlxmlClass = view.getOption('newTag-class'); + if(meta) { + var split = meta.split('/'); + params.wlxmlTag = split[0]; + params.wlxmlClass = split[1]; + } + } else { + params.meta = meta; } - } else { - params.meta = meta; - } - if(command === 'undo' || command === 'redo') { - params.callback = function(disable) { - btn.attr('disabled', !disable); + if(command === 'undo' || command === 'redo') { + params.callback = function(disable) { + btn.attr('disabled', !disable); + }; } - } - sandbox.publish('command', command, params); + _.extend(params, {ctrlKey: e.ctrlKey}); + + sandbox.publish('command', command, params); + } }); }, getOption: function(option) { @@ -50,11 +61,28 @@ return function(sandbox) { } }; + var myHandlers = { + templatesBtn: function() { + if(currentNode && currentNode.nodeType === Node.ELEMENT_NODE) { + var templateId = parseInt(view.node.find('[data-name=templates-select]').val(), 10); + documentTemplates.forEach(function(template) { + if(template.id === templateId) { + var toAdd = currentNode.document.createDocumentNode(template.content); + currentNode.after(toAdd); + } + }); + } + } + }; + view.setup(); return { start: function() { sandbox.publish('ready'); }, getView: function() { return view.node; }, + setNodeElement: function(node) { + currentNode = node; + }, getOption: function(option) { return view.getOption(option); } }; };