From 600f0113753edfca58e20936e4701a096d2b6d04 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Aleksander=20=C5=81ukasz?= Date: Tue, 1 Jul 2014 15:53:49 +0200 Subject: [PATCH 1/1] editor: canvas - context menu support --- .../modules/documentCanvas/canvas/canvas.js | 40 ++++++++++++++++--- .../documentCanvas/canvas/documentElement.js | 4 ++ .../modules/documentCanvas/documentCanvas.js | 2 +- 3 files changed, 39 insertions(+), 7 deletions(-) diff --git a/src/editor/modules/documentCanvas/canvas/canvas.js b/src/editor/modules/documentCanvas/canvas/canvas.js index 0259452..d3376e3 100644 --- a/src/editor/modules/documentCanvas/canvas/canvas.js +++ b/src/editor/modules/documentCanvas/canvas/canvas.js @@ -3,6 +3,7 @@ define([ 'libs/underscore', 'libs/backbone', 'fnpjs/logging/logging', +'views/menu/menu', 'modules/documentCanvas/canvas/documentElement', 'modules/documentCanvas/canvas/keyboard', 'modules/documentCanvas/canvas/utils', @@ -14,8 +15,8 @@ define([ 'modules/documentCanvas/canvas/selection', 'modules/documentCanvas/canvas/keyEvent', 'libs/text!./canvas.html' -], function($, _, Backbone, logging, documentElement, keyboard, utils, wlxmlListener, ElementsRegister, genericElement, nullElement, gutter, selection, keyEvent, canvasTemplate) { - +], function($, _, Backbone, logging, Menu, documentElement, keyboard, utils, wlxmlListener, ElementsRegister, genericElement, nullElement, gutter, selection, keyEvent, canvasTemplate) { + 'use strict'; /* global document:false, window:false, Node:false, gettext */ @@ -62,8 +63,9 @@ $.extend(TextHandler.prototype, { }); -var Canvas = function(wlxmlDocument, elements, metadata) { +var Canvas = function(wlxmlDocument, elements, metadata, sandbox) { this.metadata = metadata || {}; + this.sandbox = sandbox; this.elementsRegister = new ElementsRegister(documentElement.DocumentNodeElement, nullElement); elements = [ @@ -147,7 +149,6 @@ $.extend(Canvas.prototype, Backbone.Events, { this.rootWrapper.append(this.rootElement.dom); }, - triggerKeyEvent: function(keyEvent, selection) { selection = selection || this.getSelection(); if(selection && (selection.type === 'caret' || selection.type === 'textSelection') && selection.toDocumentFragment().isValid()) { @@ -155,6 +156,10 @@ $.extend(Canvas.prototype, Backbone.Events, { } }, + createAction: function(fqName, config) { + return this.sandbox.createAction(fqName, config); + }, + setupEventHandling: function() { var canvas = this; @@ -196,6 +201,17 @@ $.extend(Canvas.prototype, Backbone.Events, { } }); + this.rootWrapper.on('contextmenu', function(e) { + var el = canvas.getDocumentElement(e.target); + + if(!el) { + return; + } + + e.preventDefault(); + this.showContextMenu(el, {x: e.clientX, y: e.clientY}); + }.bind(this)); + this.rootWrapper.on('paste', function(e) { e.preventDefault(); @@ -454,6 +470,18 @@ $.extend(Canvas.prototype, Backbone.Events, { if(position.element) { this._moveCaretToTextElement(position.element, position.offset); } + }, + showContextMenu: function(element, coors) { + var menu = new Menu(); + + while(element) { + (element.contextMenuActions || []).forEach(menu.addAction.bind(menu)); + element = element.parent(); + } + if(menu.actions.length) { + menu.updateContextParam('fragment', this.getSelection().toDocumentFragment()); + this.sandbox.showContextMenu(menu, {x: coors.x, y: coors.y}); + } } }); @@ -585,8 +613,8 @@ $.extend(Cursor.prototype, { }); return { - fromXMLDocument: function(wlxmlDocument, elements, metadata) { - return new Canvas(wlxmlDocument, elements, metadata); + fromXMLDocument: function(wlxmlDocument, elements, metadata, sandbox) { + return new Canvas(wlxmlDocument, elements, metadata, sandbox); } }; diff --git a/src/editor/modules/documentCanvas/canvas/documentElement.js b/src/editor/modules/documentCanvas/canvas/documentElement.js index 859a80b..adeaafe 100644 --- a/src/editor/modules/documentCanvas/canvas/documentElement.js +++ b/src/editor/modules/documentCanvas/canvas/documentElement.js @@ -95,6 +95,7 @@ $.extend(DocumentElement.prototype, { var DocumentNodeElement = function(wlxmlNode, canvas) { DocumentElement.call(this, wlxmlNode, canvas); this.containers = []; + this.contextMenuActions = []; this.init(this.dom); }; @@ -146,6 +147,9 @@ $.extend(DocumentNodeElement.prototype, { this.containers.splice(idx, 1); } }, + addToContextMenu: function(actionFqName) { + this.contextMenuActions.push(this.canvas.createAction(actionFqName)); + }, handle: function(event) { var method = 'on' + event.type[0].toUpperCase() + event.type.substr(1), target; diff --git a/src/editor/modules/documentCanvas/documentCanvas.js b/src/editor/modules/documentCanvas/documentCanvas.js index a98fdb4..a440bb9 100644 --- a/src/editor/modules/documentCanvas/documentCanvas.js +++ b/src/editor/modules/documentCanvas/documentCanvas.js @@ -22,7 +22,7 @@ return function(sandbox) { var canvas = canvas3.fromXMLDocument(null, canvasElements, { user: sandbox.getConfig().user - }); + }, sandbox); var canvasWrapper = $(template); var shownAlready = false; var scrollbarPosition = 0, -- 2.20.1