+/* global module */
+
module.exports = function(grunt) {
+ 'use strict';
grunt.initConfig({
requirejs: {
], function($, _, Backbone, documentElement, keyboard, utils, wlxmlListener) {
'use strict';
+/* global document:false, window:false */
-var TextHandler = function(canvas) {this.canvas = canvas; this.buffer = null};
+
+var TextHandler = function(canvas) {this.canvas = canvas; this.buffer = null;};
$.extend(TextHandler.prototype, {
handle: function(node, text) {
- //console.log('canvas text handler: ' + text);
this.setText(text, node);
- return;
- if(!this.node) {
- this.node = node;
- }
- if(this.node.sameNode(node)) {
- this._ping(text);
- } else {
- this.flush();
- this.node = node;
- this._ping(text);
- }
+ // return;
+ // if(!this.node) {
+ // this.node = node;
+ // }
+ // if(this.node.sameNode(node)) {
+ // this._ping(text);
+ // } else {
+ // this.flush();
+ // this.node = node;
+ // this._ping(text);
+ // }
},
_ping: _.throttle(function(text) {
this.buffer = text;
this.flush();
}, 1000),
flush: function() {
- if(this.buffer != null) {
+ if(this.buffer !== null) {
this.setText(this.buffer, this.node);
this.buffer = null;
}
});
-
+ /* globals MutationObserver */
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
if(documentElement.DocumentTextElement.isContentContainer(mutation.target)) {
observer.disconnect();
- if(mutation.target.data === '')
+ if(mutation.target.data === '') {
mutation.target.data = utils.unicode.ZWS;
+ }
else if(mutation.oldValue === utils.unicode.ZWS) {
mutation.target.data = mutation.target.data.replace(utils.unicode.ZWS, '');
canvas._moveCaretToTextElement(canvas.getDocumentElement(mutation.target), 'end');
//textElement.data('wlxmlNode').setText(toSet);
//textElement.data('wlxmlNode').document.transform('setText', {node: textElement.data('wlxmlNode'), text: toSet});
- if(textElement.data('wlxmlNode').getText() != toSet) {
+ if(textElement.data('wlxmlNode').getText() !== toSet) {
canvas.textHandler.handle(textElement.data('wlxmlNode'), toSet);
}
}
this.wrapper.on('mouseover', '[document-node-element], [document-text-element]', function(e) {
var el = canvas.getDocumentElement(e.currentTarget);
- if(!el)
+ if(!el) {
return;
+ }
e.stopPropagation();
- if(el instanceof documentElement.DocumentTextElement)
+ if(el instanceof documentElement.DocumentTextElement) {
el = el.parent();
+ }
el.toggleLabel(true);
});
this.wrapper.on('mouseout', '[document-node-element], [document-text-element]', function(e) {
var el = canvas.getDocumentElement(e.currentTarget);
- if(!el)
+ if(!el) {
return;
+ }
e.stopPropagation();
- if(el instanceof documentElement.DocumentTextElement)
+ if(el instanceof documentElement.DocumentTextElement) {
el = el.parent();
+ }
el.toggleLabel(false);
});
},
doc: function() {
- if(this.d === null)
+ if(this.d === null) {
return null;
+ }
return documentElement.DocumentNodeElement.fromHTMLElement(this.d.get(0), this); //{wlxmlTag: this.d.prop('tagName')};
},
},
getDocumentElement: function(from) {
+ /* globals HTMLElement, Text */
if(from instanceof HTMLElement || from instanceof Text) {
return documentElement.DocumentElement.fromHTMLElement(from, this);
}
params = _.extend({caretTo: 'end'}, params);
var findFirstDirectTextChild = function(e, nodeToLand) {
var byBrowser = this.getCursor().getPosition().element;
- if(byBrowser && byBrowser.parent().sameNode(nodeToLand))
+ if(byBrowser && byBrowser.parent().sameNode(nodeToLand)) {
return byBrowser;
+ }
var children = e.children();
for(var i = 0; i < children.length; i++) {
- if(children[i] instanceof documentElement.DocumentTextElement)
+ if(children[i] instanceof documentElement.DocumentTextElement) {
return children[i];
+ }
}
return null;
}.bind(this);
this.wrapper.find('.current-text-element').removeClass('current-text-element');
element.dom().addClass('current-text-element');
} else {
- this.wrapper.find('.current-node-element').removeClass('current-node-element')
+ this.wrapper.find('.current-node-element').removeClass('current-node-element');
element._container().addClass('current-node-element');
this.publisher('currentElementChanged', element);
}
currentTextElement = this.getCurrentTextElement(),
currentNodeElement = this.getCurrentNodeElement();
- if(currentTextElement && !(currentTextElement.sameNode(textElementToLand)))
+ if(currentTextElement && !(currentTextElement.sameNode(textElementToLand))) {
this.wrapper.find('.current-text-element').removeClass('current-text-element');
+ }
if(textElementToLand) {
_markAsCurrent(textElementToLand);
- if(params.caretTo || !textElementToLand.sameNode(this.getCursor().getPosition().element))
+ if(params.caretTo || !textElementToLand.sameNode(this.getCursor().getPosition().element)) {
this._moveCaretToTextElement(textElementToLand, params.caretTo); // as method on element?
- if(!(textElementToLand.sameNode(currentTextElement)))
+ }
+ if(!(textElementToLand.sameNode(currentTextElement))) {
this.publisher('currentTextElementSet', textElementToLand.data('wlxmlNode'));
+ }
} else {
document.getSelection().removeAllRanges();
}
}
var collapseArg = true;
- if(where === 'end')
+ if(where === 'end') {
collapseArg = false;
+ }
range.collapse(collapseArg);
var selection = document.getSelection();
},
setCursorPosition: function(position) {
- if(position.element)
+ if(position.element) {
this._moveCaretToTextElement(position.element, position.offset);
+ }
}
});
return this.getSelectionBoundry('focus');
},
getSelectionBoundry: function(which) {
+ /* globals window */
var selection = window.getSelection(),
anchorElement = this.canvas.getDocumentElement(selection.anchorNode),
focusElement = this.canvas.getDocumentElement(selection.focusNode);
- if((!anchorElement) || (anchorElement instanceof documentElement.DocumentNodeElement) || (!focusElement) || focusElement instanceof documentElement.DocumentNodeElement)
+ if((!anchorElement) || (anchorElement instanceof documentElement.DocumentNodeElement) || (!focusElement) || focusElement instanceof documentElement.DocumentNodeElement) {
return {};
+ }
if(which === 'anchor') {
return {
if(anchorFirst) {
if(which === 'start') {
element = anchorElement;
- offset = selection.anchorOffset
+ offset = selection.anchorOffset;
}
else if(which === 'end') {
- element = focusElement,
- offset = selection.focusOffset
+ element = focusElement;
+ offset = selection.focusOffset;
}
} else {
if(which === 'start') {
- element = focusElement,
- offset = selection.focusOffset
+ element = focusElement;
+ offset = selection.focusOffset;
}
else if(which === 'end') {
element = anchorElement;
- offset = selection.anchorOffset
+ offset = selection.anchorOffset;
}
}
} else {
// TODO: Handle order via https://developer.mozilla.org/en-US/docs/Web/API/Node.compareDocumentPosition
if(which === 'start') {
element = anchorElement;
- offset = selection.anchorOffset
+ offset = selection.anchorOffset;
} else {
element = focusElement;
- offset = selection.focusOffset
+ offset = selection.focusOffset;
}
}
offset: offset,
offsetAtBeginning: offset === 0,
offsetAtEnd: nodeLen === offset
- }
+ };
}
-})
+});
return {
fromXMLDocument: function(wlxmlDocument, publisher) {
], function($, _, utils, widgets, wlxmlManagers) {
'use strict';
+/* global Node:false, document:false */
// DocumentElement represents a text or an element node from WLXML document rendered inside Canvas
var DocumentElement = function(htmlElement, canvas) {
- if(arguments.length === 0)
+ if(arguments.length === 0) {
return;
+ }
this.canvas = canvas;
this._setupDOMHandler(htmlElement);
-}
+};
var elementTypeFromWlxmlNode = function(wlxmlNode) {
return wlxmlNode.nodeType === Node.TEXT_NODE ? DocumentTextElement : DocumentNodeElement;
-}
+};
$.extend(DocumentElement, {
create: function(node, canvas) {
fromHTMLElement: function(htmlElement, canvas) {
var $element = $(htmlElement);
- if(htmlElement.nodeType === Node.ELEMENT_NODE && $element.attr('document-node-element') !== undefined)
+ if(htmlElement.nodeType === Node.ELEMENT_NODE && $element.attr('document-node-element') !== undefined) {
return DocumentNodeElement.fromHTMLElement(htmlElement, canvas);
- if($element.attr('document-text-element') !== undefined || (htmlElement.nodeType === Node.TEXT_NODE && $element.parent().attr('document-text-element') !== undefined))
+ }
+ if($element.attr('document-text-element') !== undefined || (htmlElement.nodeType === Node.TEXT_NODE && $element.parent().attr('document-text-element') !== undefined)) {
return DocumentTextElement.fromHTMLElement(htmlElement, canvas);
+ }
return undefined;
}
});
data: function() {
var dom = this.dom(),
args = Array.prototype.slice.call(arguments, 0);
- if(args.length === 2 && args[1] === undefined)
+ if(args.length === 2 && args[1] === undefined) {
return dom.removeData(args[0]);
+ }
return dom.data.apply(dom, arguments);
},
parent: function() {
var parents = this.$element.parents('[document-node-element]');
- if(parents.length)
+ if(parents.length) {
return DocumentElement.fromHTMLElement(parents[0], this.canvas);
+ }
return null;
},
getVerticallyFirstTextElement: function() {
var toret;
this.children().some(function(child) {
- if(!child.isVisible())
+ if(!child.isVisible()) {
return false; // continue
+ }
if(child instanceof DocumentTextElement) {
toret = child;
return true; // break
} else {
toret = child.getVerticallyFirstTextElement();
- if(toret)
+ if(toret) {
return true; // break
+ }
}
});
return toret;
exec: function(method) {
var manager = this.data('_wlxmlManager');
- if(manager[method])
+ if(manager[method]) {
return manager[method].apply(manager, Array.prototype.slice.call(arguments, 1));
+ }
}
});
element.setWlxml({tag: wlxmlNode.getTagName(), klass: wlxmlNode.getClass()});
- wlxmlNode.contents().forEach((function(node) {
+ wlxmlNode.contents().forEach(function(node) {
container.append(DocumentElement.create(node).dom());
- }).bind(this));
+ }.bind(this));
return dom;
}
return this;
},
append: function(params) {
- if(params.tag !== 'span')
+ if(params.tag !== 'span') {
this.data('orig-end', undefined);
+ }
return manipulate(this, params, 'append');
},
prepend: function(params) {
},
children: function() {
var toret = [];
- if(this instanceof DocumentTextElement)
+ if(this instanceof DocumentTextElement) {
return toret;
+ }
var elementContent = this._container().contents();
var element = this;
elementContent.each(function(idx) {
var childElement = DocumentElement.fromHTMLElement(this, element.canvas);
- if(childElement === undefined)
+ if(childElement === undefined) {
return true;
- if(idx === 0 && elementContent.length > 1 && elementContent[1].nodeType === Node.ELEMENT_NODE && (childElement instanceof DocumentTextElement) && $.trim($(this).text()) === '')
+ }
+ if(idx === 0 && elementContent.length > 1 && elementContent[1].nodeType === Node.ELEMENT_NODE && (childElement instanceof DocumentTextElement) && $.trim($(this).text()) === '') {
return true;
+ }
if(idx > 0 && childElement instanceof DocumentTextElement) {
- if(toret[toret.length-1] instanceof DocumentNodeElement && $.trim($(this).text()) === '')
+ if(toret[toret.length-1] instanceof DocumentNodeElement && $.trim($(this).text()) === '') {
return true;
+ }
}
toret.push(childElement);
});
return this._container().attr('wlxml-tag');
},
setWlxmlTag: function(tag) {
- if(tag === this.getWlxmlTag())
+ if(tag === this.getWlxmlTag()) {
return;
+ }
this._container().attr('wlxml-tag', tag);
- if(!this.__updatingWlxml)
+ if(!this.__updatingWlxml) {
this._updateWlxmlManager();
+ }
},
getWlxmlClass: function() {
var klass = this._container().attr('wlxml-class');
- if(klass)
+ if(klass) {
return klass.replace(/-/g, '.');
+ }
return undefined;
},
setWlxmlClass: function(klass) {
- if(klass === this.getWlxmlClass())
+ if(klass === this.getWlxmlClass()) {
return;
- if(klass)
+ }
+ if(klass) {
this._container().attr('wlxml-class', klass.replace(/\./g, '-'));
- else
+ }
+ else {
this._container().removeAttr('wlxml-class');
- if(!this.__updatingWlxml)
+ }
+ if(!this.__updatingWlxml) {
this._updateWlxmlManager();
+ }
},
setWlxml: function(params) {
this.__updatingWlxml = true;
- if(params.tag !== undefined)
+ if(params.tag !== undefined) {
this.setWlxmlTag(params.tag);
- if(params.klass !== undefined)
+ }
+ if(params.klass !== undefined) {
this.setWlxmlClass(params.klass);
+ }
this._updateWlxmlManager();
this.__updatingWlxml = false;
},
manager.setup();
},
is: function(what) {
- if(what === 'list' && _.contains(['list.items', 'list.items.enum'], this.getWlxmlClass()))
+ if(what === 'list' && _.contains(['list.items', 'list.items.enum'], this.getWlxmlClass())) {
return true;
+ }
return false;
},
toggleLabel: function(toggle) {
$.extend(DocumentTextElement.prototype, {
_setupDOMHandler: function(htmlElement) {
var $element = $(htmlElement);
- if(htmlElement.nodeType === Node.TEXT_NODE)
+ if(htmlElement.nodeType === Node.TEXT_NODE) {
this.$element = $element.parent();
- else
+ }
+ else {
this.$element = $element;
+ }
},
detach: function() {
this.dom().detach();
setText: function(text) {
this.dom().contents()[0].data = text;
},
- appendText: function(text) {
- this.dom().contents()[0].data += text;
- },
- prependText: function(text) {
- this.dom().contents()[0].data = text + this.dom().contents()[0].data;
- },
getText: function(options) {
options = _.extend({raw: false}, options || {});
var toret = this.dom().text();
return this.dom().contents()[0].data === utils.unicode.ZWS;
},
after: function(params) {
- if(params instanceof DocumentTextElement || params.text)
+ if(params instanceof DocumentTextElement || params.text) {
return false;
+ }
var element;
if(params instanceof DocumentNodeElement) {
element = params;
return element;
},
before: function(params) {
- if(params instanceof DocumentTextElement || params.text)
+ if(params instanceof DocumentTextElement || params.text) {
return false;
+ }
var element;
if(params instanceof DocumentNodeElement) {
element = params;
divide: function(params) {
var myText = this.getText();
- if(params.offset === myText.length)
+ if(params.offset === myText.length) {
return this.after(params);
- if(params.offset === 0)
+ }
+ if(params.offset === 0) {
return this.before(params);
+ }
var lhsText = myText.substr(0, params.offset),
rhsText = myText.substr(params.offset),
};
var handles = function(handler, event) {
- if(handler.key === event.which)
+ if(handler.key === event.which) {
return true;
- if(handler.keys && handler.keys.indexOf(event.which) !== -1)
+ }
+ if(handler.keys && handler.keys.indexOf(event.which) !== -1) {
return true;
+ }
return false;
};
event.preventDefault();
var cursor = canvas.getCursor(),
position = cursor.getPosition(),
- element = position.element;
+ element = position.element,
+ added;
if(Object.keys(cursor.getPosition()).length === 0) {
var currentElement = canvas.getCurrentNodeElement();
if(currentElement) {
- var added = currentElement.data('wlxmlNode').after({
+ added = currentElement.data('wlxmlNode').after({
tag: currentElement.getWlxmlTag() || 'div',
attrs: {'class': currentElement.getWlxmlClass() || 'p'}
});
if(element instanceof documentElement.DocumentTextElement) {
element = element.parent();
}
- var added = element.data('wlxmlNode').after(
+ added = element.data('wlxmlNode').after(
{tagName: element.getWlxmlTag() || 'div', attrs: {'class': element.getWlxmlClass() || 'p'}}
);
added.append({text: ''});
gotoOptions = {};
} else {
goto = result.second;
- gotoOptions = {caretTo: 'start'};
+ gotoOptions = {caretTo: 'start'};
}
canvas.setCurrentElement(utils.findCanvasElement(goto), gotoOptions);
direction = 'below';
caretTo = 'start';
}
+ /* globals window */
element = canvas.getDocumentElement(utils.nearestInDocumentOrder('[document-text-element]:visible', direction, window.getSelection().focusNode));
}
canvas.setCurrentElement(element, {caretTo: caretTo});
var selectsWholeTextElement = function(cursor) {
- if(cursor.isSelecting() && cursor.getSelectionStart().offsetAtBeginning && cursor.getSelectionEnd().offsetAtEnd)
+ if(cursor.isSelecting() && cursor.getSelectionStart().offsetAtBeginning && cursor.getSelectionEnd().offsetAtEnd) {
return true;
+ }
return false;
-}
+};
handlers.push({key: KEYS.X,
keydown: function(event, canvas) {
- if(event.ctrlKey && selectsWholeTextElement(canvas.getCursor()))
+ if(event.ctrlKey && selectsWholeTextElement(canvas.getCursor())) {
event.preventDefault();
+ }
}
});
var willDeleteWholeText = function() {
return element.getText().length === 1 || selectsWholeTextElement(cursor);
- }
+ };
if(willDeleteWholeText()) {
event.preventDefault();
});
commands.register('remove-node', function(canvas) {
- var cursor = canvas.getCursor(),
- selectionStart = cursor.getSelectionStart(),
- selectionEnd = cursor.getSelectionEnd(),
- parent1 = selectionStart.element.parent() || undefined,
- parent2 = selectionEnd.element.parent() || undefined;
-
-// canvas.wlxmlDocument.transform('detach2', {node:canvas.getCurrentNodeElement().data('wlxmlNode')});
canvas.getCurrentNodeElement().data('wlxmlNode').transform('smartxml.detach');
-
});
commands.register('unwrap-node', function(canvas) {
});
commands.register('list', function(canvas, params) {
+ void(params);
var cursor = canvas.getCursor(),
selectionStart = cursor.getSelectionStart(),
selectionEnd = cursor.getSelectionEnd(),
commands.register('newNodeRequested', function(canvas, params) {
var cursor = canvas.getCursor(),
selectionStart = cursor.getSelectionStart(),
- selectionEnd = cursor.getSelectionEnd();
+ selectionEnd = cursor.getSelectionEnd(),
+ wlxmlNode, caretTo, wrapper, wrapperCanvasElement;
if(cursor.isSelecting()) {
if(cursor.isSelectingSiblings()) {
if(cursor.isSelectingWithinElement()) {
- var wlxmlNode = selectionStart.element.data('wlxmlNode'),
- caretTo = selectionStart.offset < selectionEnd.offset ? 'start' : 'end',
+ wlxmlNode = selectionStart.element.data('wlxmlNode');
+ caretTo = selectionStart.offset < selectionEnd.offset ? 'start' : 'end';
//wrapper = wlxmlNode.wrapWith({tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}, start: selectionStart.offset, end: selectionEnd.offset}),
//wrapper = wlxmlNode.transform('smartxml.wrapWith', {tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}, start: selectionStart.offset, end: selectionEnd.offset})
- wrapper = wlxmlNode.wrapWith({tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}, start: selectionStart.offset, end: selectionEnd.offset});
- ;
- var wrapperCanvasElement = utils.findCanvasElement(wrapper);
+ wrapper = wlxmlNode.wrapWith({tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}, start: selectionStart.offset, end: selectionEnd.offset});
+ wrapperCanvasElement = utils.findCanvasElement(wrapper);
canvas.setCurrentElement(wrapperCanvasElement.children()[0], {caretTo: caretTo});
}
else {
- var wlxmlNode = selectionStart.element.data('wlxmlNode').parent(),
- caretTo = selectionStart.element.sameNode(cursor.getSelectionAnchor().element) ? 'end' : 'start';
+ wlxmlNode = selectionStart.element.data('wlxmlNode').parent();
+ caretTo = selectionStart.element.sameNode(cursor.getSelectionAnchor().element) ? 'end' : 'start';
// var wrapper = wlxmlNode.wrapText({
// _with: {tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}},
// offsetEnd: selectionEnd.offset,
// textNodeIdx: [wlxmlNode.indexOf(selectionStart.element.data('wlxmlNode')), wlxmlNode.indexOf(selectionEnd.element.data('wlxmlNode'))] //parent.childIndex(selectionEnd.element)]
// }),
- var wrapper = wlxmlNode.transform('smartxml.wrapText', {
+ wrapper = wlxmlNode.transform('smartxml.wrapText', {
_with: {tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}},
offsetStart: selectionStart.offset,
offsetEnd: selectionEnd.offset,
textNodeIdx: [wlxmlNode.indexOf(selectionStart.element.data('wlxmlNode')), wlxmlNode.indexOf(selectionEnd.element.data('wlxmlNode'))] //parent.childIndex(selectionEnd.element)]
- }),
+ });
wrapperCanvasElement = utils.findCanvasElement(wrapper);
canvas.setCurrentElement(wrapperCanvasElement.children()[caretTo === 0 ? 0 : wrapperCanvasElement.children().length - 1], {caretTo: caretTo});
}
}
}
} else if(canvas.getCurrentNodeElement()) {
- var node = canvas.getCurrentNodeElement().data('wlxmlNode'),
+ wlxmlNode = canvas.getCurrentNodeElement().data('wlxmlNode');
// wrapper = node.wrapWith({tagName: params.wlxmlTag, attrs: {klass: params.wlxmlClass}});
- wrapper = node.transform('smartxml.wrapWith', {tagName: params.wlxmlTag, attrs: {klass: params.wlxmlClass}});
+ wrapper = wlxmlNode.transform('smartxml.wrapWith', {tagName: params.wlxmlTag, attrs: {klass: params.wlxmlClass}});
canvas.setCurrentElement(utils.findCanvasElement(wrapper));
}
});
commands.register('footnote', function(canvas, params) {
+ void(params);
var cursor = canvas.getCursor(),
position = cursor.getPosition(),
asideElement;
element = position.element,
nodeElement = element ? element.parent() : canvas.getCurrentNodeElement();
- if(!nodeElement || !(nodeElement.parent()))
+ if(!nodeElement || !(nodeElement.parent())) {
return;
+ }
var range = nodeElement.data('wlxmlNode').unwrapContent();