(function (factory) {
if (typeof define === 'function' && define.amd) {
define(['jquery'], factory);
} else if (typeof module === 'object' && module.exports) {
module.exports = factory(require('jquery'));
} else {
factory(window.jQuery);
}
}(function ($) {
'use strict';
var func = (function () {
var eq = function (itemA) {
return function (itemB) {
return itemA === itemB;
};
};
var eq2 = function (itemA, itemB) {
return itemA === itemB;
};
var peq2 = function (propName) {
return function (itemA, itemB) {
return itemA[propName] === itemB[propName];
};
};
var ok = function () {
return true;
};
var fail = function () {
return false;
};
var not = function (f) {
return function () {
return !f.apply(f, arguments);
};
};
var and = function (fA, fB) {
return function (item) {
return fA(item) && fB(item);
};
};
var self = function (a) {
return a;
};
var invoke = function (obj, method) {
return function () {
return obj[method].apply(obj, arguments);
};
};
var idCounter = 0;
var uniqueId = function (prefix) {
var id = ++idCounter + '';
return prefix ? prefix + id : id;
};
var rect2bnd = function (rect) {
var $document = $(document);
return {
top: rect.top + $document.scrollTop(),
left: rect.left + $document.scrollLeft(),
width: rect.right - rect.left,
height: rect.bottom - rect.top
};
};
var invertObject = function (obj) {
var inverted = {};
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
inverted[obj[key]] = key;
}
}
return inverted;
};
var namespaceToCamel = function (namespace, prefix) {
prefix = prefix || '';
return prefix + namespace.split('.').map(function (name) {
return name.substring(0, 1).toUpperCase() + name.substring(1);
}).join('');
};
var debounce = function (func, wait, immediate) {
var timeout;
return function () {
var context = this, args = arguments;
var later = function () {
timeout = null;
if (!immediate) {
func.apply(context, args);
}
};
var callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) func.apply(context, args);
};
};
return {
eq: eq,
eq2: eq2,
peq2: peq2,
ok: ok,
fail: fail,
self: self,
not: not,
and: and,
invoke: invoke,
uniqueId: uniqueId,
rect2bnd: rect2bnd,
invertObject: invertObject,
namespaceToCamel: namespaceToCamel,
debounce: debounce
};
})();
var list = (function () {
var head = function (array) {
return array[0];
};
var last = function (array) {
return array[array.length - 1];
};
var initial = function (array) {
return array.slice(0, array.length - 1);
};
var tail = function (array) {
return array.slice(1);
};
var find = function (array, pred) {
for (var idx = 0, len = array.length; idx < len; idx ++) {
var item = array[idx];
if (pred(item)) return item;
}
};
var all = function (array, pred) {
for (var idx = 0, len = array.length; idx < len; idx ++) {
if (!pred(array[idx])) return false;
}
return true;
};
var indexOf = function (array, item) {
return $.inArray(item, array);
};
var contains = function (array, item) {
return indexOf(array, item) !== -1;
};
var sum = function (array, fn) {
fn = fn || func.self;
return array.reduce(function (memo, v) {
return memo + fn(v);
}, 0);
};
var from = function (collection) {
var result = [], idx = -1, length = collection.length;
while (++idx < length) {
result[idx] = collection[idx];
}
return result;
};
var isEmpty = function (array) {
return !array || !array.length;
};
var clusterBy = function (array, fn) {
if (!array.length) { return []; }
var aTail = tail(array);
return aTail.reduce(function (memo, v) {
var aLast = last(memo);
if (fn(last(aLast), v)) {
aLast[aLast.length] = v;
} else {
memo[memo.length] = [v];
}
return memo;
}, [[head(array)]]);
};
var compact = function (array) {
var aResult = [];
for (var idx = 0, len = array.length; idx < len; idx ++) {
if (array[idx]) { aResult.push(array[idx]); }
}
return aResult;
};
var unique = function (array) {
var results = [];
for (var idx = 0, len = array.length; idx < len; idx ++) {
if (!contains(results, array[idx])) results.push(array[idx]);
}
return results;
};
var next = function (array, item) {
var idx = indexOf(array, item);
if (idx === -1) { return null; }
return array[idx + 1];
};
var prev = function (array, item) {
var idx = indexOf(array, item);
if (idx === -1) { return null; }
return array[idx - 1];
};
return { head: head, last: last, initial: initial, tail: tail,
prev: prev, next: next, find: find, contains: contains,
all: all, sum: sum, from: from, isEmpty: isEmpty,
clusterBy: clusterBy, compact: compact, unique: unique };
})();
var isSupportAmd = typeof define === 'function' && define.amd;
var isFontInstalled = function (fontName) {
var testFontName = fontName === 'Comic Sans MS' ? 'Courier New' : 'Comic Sans MS';
var $tester = $('
').css({
position: 'absolute',
left: '-9999px',
top: '-9999px',
fontSize: '200px'
}).text('mmmmmmmmmwwwwwww').appendTo(document.body);
var originalWidth = $tester.css('fontFamily', testFontName).width();
var width = $tester.css('fontFamily', fontName + ',' + testFontName).width();
$tester.remove();
return originalWidth !== width;
};
var userAgent = navigator.userAgent;
var isMSIE = /MSIE|Trident/i.test(userAgent);
var browserVersion;
if (isMSIE) {
var matches = /MSIE (\d+[.]\d+)/.exec(userAgent);
if (matches) browserVersion = parseFloat(matches[1]);
matches = /Trident\/.*rv:([0-9]{1,}[\.0-9]{0,})/.exec(userAgent);
if (matches) browserVersion = parseFloat(matches[1]);
}
var isEdge = /Edge\/\d+/.test(userAgent);
var hasCodeMirror = !!window.CodeMirror;
if (!hasCodeMirror && isSupportAmd && typeof require !== 'undefined') {
if (typeof require.resolve !== 'undefined') {
try {
require.resolve('codemirror');
hasCodeMirror = true;
} catch (e) {
// Do nothing.
}
} else if (typeof eval('require').specified !== 'undefined') {
hasCodeMirror = eval('require').specified('codemirror');
}
}
var agent = {
isMac: navigator.appVersion.indexOf('Mac') > -1,
isMSIE: isMSIE,
isEdge: isEdge,
isFF: !isEdge && /firefox/i.test(userAgent),
isPhantom: /PhantomJS/i.test(userAgent),
isWebkit: !isEdge && /webkit/i.test(userAgent),
isChrome: !isEdge && /chrome/i.test(userAgent),
isSafari: !isEdge && /safari/i.test(userAgent),
browserVersion: browserVersion,
jqueryVersion: parseFloat($.fn.jquery),
isSupportAmd: isSupportAmd,
hasCodeMirror: hasCodeMirror,
isFontInstalled: isFontInstalled,
isW3CRangeSupport: !!document.createRange
};
var NBSP_CHAR = String.fromCharCode(160);
var ZERO_WIDTH_NBSP_CHAR = '\ufeff';
var dom = (function () {
var isEditable = function (node) {
return node && $(node).hasClass('wysiwyg-editable');
};
var isControlSizing = function (node) {
return node && $(node).hasClass('wysiwyg-control-sizing');
};
var makePredByNodeName = function (nodeName) {
nodeName = nodeName.toUpperCase();
return function (node) {
return node && node.nodeName.toUpperCase() === nodeName;
};
};
var isText = function (node) {
return node && node.nodeType === 3;
};
var isElement = function (node) {
return node && node.nodeType === 1;
};
var isVoid = function (node) {
return node && /^BR|^IMG|^HR|^IFRAME|^BUTTON/.test(node.nodeName.toUpperCase());
};
var isPara = function (node) {
if (isEditable(node)) return false;
return node && /^DIV|^P|^LI|^H[1-7]/.test(node.nodeName.toUpperCase());
};
var isHeading = function (node) {
return node && /^H[1-7]/.test(node.nodeName.toUpperCase());
};
var isPre = makePredByNodeName('PRE');
var isLi = makePredByNodeName('LI');
var isPurePara = function (node) {
return isPara(node) && !isLi(node);
};
var isTable = makePredByNodeName('TABLE');
var isData = makePredByNodeName('DATA');
var isInline = function (node) {
return !isBodyContainer(node) &&
!isList(node) &&
!isHr(node) &&
!isPara(node) &&
!isTable(node) &&
!isBlockquote(node) &&
!isData(node);
};
var isList = function (node) {
return node && /^UL|^OL/.test(node.nodeName.toUpperCase());
};
var isHr = makePredByNodeName('HR');
var isCell = function (node) {
return node && /^TD|^TH/.test(node.nodeName.toUpperCase());
};
var isBlockquote = makePredByNodeName('BLOCKQUOTE');
var isBodyContainer = function (node) {
return isCell(node) || isBlockquote(node) || isEditable(node);
};
var isAnchor = makePredByNodeName('A');
var isParaInline = function (node) {
return isInline(node) && !!ancestor(node, isPara);
};
var isBodyInline = function (node) {
return isInline(node) && !ancestor(node, isPara);
};
var isBody = makePredByNodeName('BODY');
var isClosestSibling = function (nodeA, nodeB) {
return nodeA.nextSibling === nodeB ||
nodeA.previousSibling === nodeB;
};
var withClosestSiblings = function (node, pred) {
pred = pred || func.ok;
var siblings = [];
if (node.previousSibling && pred(node.previousSibling)) {
siblings.push(node.previousSibling);
}
siblings.push(node);
if (node.nextSibling && pred(node.nextSibling)) {
siblings.push(node.nextSibling);
}
return siblings;
};
var blankHTML = agent.isMSIE && agent.browserVersion < 11 ? ' ' : '
';
var nodeLength = function (node) {
if (isText(node)) return node.nodeValue.length;
if (node) return node.childNodes.length;
return 0;
};
var isEmpty = function (node) {
var len = nodeLength(node);
if (len === 0) {
return true;
} else if (!isText(node) && len === 1 && node.innerHTML === blankHTML) {
return true;
} else if (list.all(node.childNodes, isText) && node.innerHTML === '') {
return true;
}
return false;
};
var paddingBlankHTML = function (node) {
if (!isVoid(node) && !nodeLength(node)) {
node.innerHTML = blankHTML;
}
};
var ancestor = function (node, pred) {
while (node) {
if (pred(node)) { return node; }
if (isEditable(node)) { break; }
node = node.parentNode;
}
return null;
};
var singleChildAncestor = function (node, pred) {
node = node.parentNode;
while (node) {
if (nodeLength(node) !== 1) { break; }
if (pred(node)) { return node; }
if (isEditable(node)) { break; }
node = node.parentNode;
}
return null;
};
var listAncestor = function (node, pred) {
pred = pred || func.fail;
var ancestors = [];
ancestor(node, function (el) {
if (!isEditable(el)) ancestors.push(el);
return pred(el);
});
return ancestors;
};
var lastAncestor = function (node, pred) {
var ancestors = listAncestor(node);
return list.last(ancestors.filter(pred));
};
var commonAncestor = function (nodeA, nodeB) {
var ancestors = listAncestor(nodeA);
for (var n = nodeB; n; n = n.parentNode) {
if ($.inArray(n, ancestors) > -1) { return n; }
}
return null;
};
var listPrev = function (node, pred) {
pred = pred || func.fail;
var nodes = [];
while (node) {
if (pred(node)) { break; }
nodes.push(node);
node = node.previousSibling;
}
return nodes;
};
var listNext = function (node, pred) {
pred = pred || func.fail;
var nodes = [];
while (node) {
if (pred(node)) { break; }
nodes.push(node);
node = node.nextSibling;
}
return nodes;
};
var listDescendant = function (node, pred) {
var descendants = [];
pred = pred || func.ok;
(function fnWalk(current) {
if (node !== current && pred(current)) descendants.push(current);
for (var idx = 0, len = current.childNodes.length; idx < len; idx++) {
fnWalk(current.childNodes[idx]);
}
})(node);
return descendants;
};
var wrap = function (node, wrapperName) {
var parent = node.parentNode;
var wrapper = $('<' + wrapperName + '>')[0];
parent.insertBefore(wrapper, node);
wrapper.appendChild(node);
return wrapper;
};
var insertAfter = function (node, preceding) {
var next = preceding.nextSibling;
var parent = preceding.parentNode;
if (next) {
parent.insertBefore(node, next);
} else {
parent.appendChild(node);
}
return node;
};
var appendChildNodes = function (node, aChild) {
$.each(aChild, function (idx, child) {
node.appendChild(child);
});
return node;
};
var isLeftEdgePoint = function (point) {
return point.offset === 0;
};
var isRightEdgePoint = function (point) {
return point.offset === nodeLength(point.node);
};
var isEdgePoint = function (point) {
return isLeftEdgePoint(point) || isRightEdgePoint(point);
};
var isLeftEdgeOf = function (node, ancestor) {
while (node && node !== ancestor) {
if (position(node) !== 0) return false;
node = node.parentNode;
}
return true;
};
var isRightEdgeOf = function (node, ancestor) {
if (!ancestor) return false;
while (node && node !== ancestor) {
if (position(node) !== nodeLength(node.parentNode) - 1) return false;
node = node.parentNode;
}
return true;
};
var isLeftEdgePointOf = function (point, ancestor) {
return isLeftEdgePoint(point) && isLeftEdgeOf(point.node, ancestor);
};
var isRightEdgePointOf = function (point, ancestor) {
return isRightEdgePoint(point) && isRightEdgeOf(point.node, ancestor);
};
var position = function (node) {
var offset = 0;
while ((node = node.previousSibling)) {
offset += 1;
}
return offset;
};
var hasChildren = function (node) {
return !!(node && node.childNodes && node.childNodes.length);
};
var prevPoint = function (point, isSkipInnerOffset) {
var node, offset;
if (point.offset === 0) {
if (isEditable(point.node)) return null;
node = point.node.parentNode;
offset = position(point.node);
} else if (hasChildren(point.node)) {
node = point.node.childNodes[point.offset - 1];
offset = nodeLength(node);
} else {
node = point.node;
offset = isSkipInnerOffset ? 0 : point.offset - 1;
}
return {
node: node,
offset: offset
};
};
var nextPoint = function (point, isSkipInnerOffset) {
var node, offset;
if (nodeLength(point.node) === point.offset) {
if (isEditable(point.node)) return null;
node = point.node.parentNode;
offset = position(point.node) + 1;
} else if (hasChildren(point.node)) {
node = point.node.childNodes[point.offset];
offset = 0;
} else {
node = point.node;
offset = isSkipInnerOffset ? nodeLength(point.node) : point.offset + 1;
}
return {
node: node,
offset: offset
};
};
var isSamePoint = function (pointA, pointB) {
return pointA.node === pointB.node && pointA.offset === pointB.offset;
};
var isVisiblePoint = function (point) {
if (isText(point.node) || !hasChildren(point.node) || isEmpty(point.node)) return true;
var leftNode = point.node.childNodes[point.offset - 1];
var rightNode = point.node.childNodes[point.offset];
if ((!leftNode || isVoid(leftNode)) && (!rightNode || isVoid(rightNode))) return true;
return false;
};
var prevPointUntil = function (point, pred) {
while (point) {
if (pred(point)) return point;
point = prevPoint(point);
}
return null;
};
var nextPointUntil = function (point, pred) {
while (point) {
if (pred(point)) return point;
point = nextPoint(point);
}
return null;
};
var isCharPoint = function (point) {
if (!isText(point.node)) return false;
var ch = point.node.nodeValue.charAt(point.offset - 1);
return ch && (ch !== ' ' && ch !== NBSP_CHAR);
};
var walkPoint = function (startPoint, endPoint, handler, isSkipInnerOffset) {
var point = startPoint;
while (point) {
handler(point);
if (isSamePoint(point, endPoint)) break;
var isSkipOffset = isSkipInnerOffset &&
startPoint.node !== point.node &&
endPoint.node !== point.node;
point = nextPoint(point, isSkipOffset);
}
};
var makeOffsetPath = function (ancestor, node) {
var ancestors = listAncestor(node, func.eq(ancestor));
return ancestors.map(position).reverse();
};
var fromOffsetPath = function (ancestor, offsets) {
var current = ancestor;
for (var i = 0, len = offsets.length; i < len; i++) {
if (current.childNodes.length <= offsets[i]) {
current = current.childNodes[current.childNodes.length - 1];
} else {
current = current.childNodes[offsets[i]];
}
}
return current;
};
var splitNode = function (point, options) {
var isSkipPaddingBlankHTML = options && options.isSkipPaddingBlankHTML;
var isNotSplitEdgePoint = options && options.isNotSplitEdgePoint;
if (isEdgePoint(point) && (isText(point.node) || isNotSplitEdgePoint)) {
if (isLeftEdgePoint(point)) {
return point.node;
} else if (isRightEdgePoint(point)) {
return point.node.nextSibling;
}
}
if (isText(point.node)) {
return point.node.splitText(point.offset);
} else {
var childNode = point.node.childNodes[point.offset];
var clone = insertAfter(point.node.cloneNode(false), point.node);
appendChildNodes(clone, listNext(childNode));
if (!isSkipPaddingBlankHTML) {
paddingBlankHTML(point.node);
paddingBlankHTML(clone);
}
return clone;
}
};
var splitTree = function (root, point, options) {
var ancestors = listAncestor(point.node, func.eq(root));
if (!ancestors.length) {
return null;
} else if (ancestors.length === 1) {
return splitNode(point, options);
}
return ancestors.reduce(function (node, parent) {
if (node === point.node) {
node = splitNode(point, options);
}
return splitNode({
node: parent,
offset: node ? dom.position(node) : nodeLength(parent)
}, options);
});
};
var splitPoint = function (point, isInline) {
var pred = isInline ? isPara : isBodyContainer;
var ancestors = listAncestor(point.node, pred);
var topAncestor = list.last(ancestors) || point.node;
var splitRoot;
var container;
if (pred(topAncestor)) {
splitRoot = ancestors[ancestors.length - 2];
container = topAncestor;
} else {
splitRoot = topAncestor;
container = splitRoot.parentNode;
}
var pivot = splitRoot && splitTree(splitRoot, point, {
isSkipPaddingBlankHTML: isInline,
isNotSplitEdgePoint: isInline
});
if (!pivot && container === point.node) {
pivot = point.node.childNodes[point.offset];
}
return {
rightNode: pivot,
container: container
};
};
var create = function (nodeName) {
return document.createElement(nodeName);
};
var createText = function (text) {
return document.createTextNode(text);
};
var remove = function (node, isRemoveChild) {
if (!node || !node.parentNode) { return; }
if (node.removeNode) { return node.removeNode(isRemoveChild); }
var parent = node.parentNode;
if (!isRemoveChild) {
var nodes = [];
var i;
var len;
for (i = 0, len = node.childNodes.length; i < len; i++) {
nodes.push(node.childNodes[i]);
}
for (i = 0, len = nodes.length; i < len; i++) {
parent.insertBefore(nodes[i], node);
}
}
parent.removeChild(node);
};
var removeWhile = function (node, pred) {
while (node) {
if (isEditable(node) || !pred(node)) break;
var parent = node.parentNode;
remove(node);
node = parent;
}
};
var replace = function (node, nodeName) {
if (node.nodeName.toUpperCase() === nodeName.toUpperCase()) return node;
var newNode = create(nodeName);
if (node.style.cssText) {
newNode.style.cssText = node.style.cssText;
}
appendChildNodes(newNode, list.from(node.childNodes));
insertAfter(newNode, node);
remove(node);
return newNode;
};
var isTextarea = makePredByNodeName('TEXTAREA');
var value = function ($node, stripLinebreaks) {
var val = isTextarea($node[0]) ? $node.val() : $node.html();
if (stripLinebreaks) return val.replace(/[\n\r]/g, '');
return val;
};
var html = function ($node, isNewlineOnBlock) {
var markup = value($node);
if (isNewlineOnBlock) {
var regexTag = /<(\/?)(\b(?!!)[^>\s]*)(.*?)(\s*\/?>)/g;
markup = markup.replace(regexTag, function (match, endSlash, name) {
name = name.toUpperCase();
var isEndOfInlineContainer = /^DIV|^TD|^TH|^P|^LI|^H[1-7]/.test(name) && !!endSlash;
var isBlockNode = /^BLOCKQUOTE|^TABLE|^TBODY|^TR|^HR|^UL|^OL/.test(name);
return match + ((isEndOfInlineContainer || isBlockNode) ? '\n' : '');
});
markup = $.trim(markup);
}
return markup;
};
var posFromPlaceholder = function (placeholder) {
var $placeholder = $(placeholder);
var pos = $placeholder.offset();
var height = $placeholder.outerHeight(true);
return {
left: pos.left,
top: pos.top + height
};
};
var attachEvents = function ($node, events) {
Object.keys(events).forEach(function (key) {
$node.on(key, events[key]);
});
};
var detachEvents = function ($node, events) {
Object.keys(events).forEach(function (key) {
$node.off(key, events[key]);
});
};
return {
NBSP_CHAR: NBSP_CHAR,
ZERO_WIDTH_NBSP_CHAR: ZERO_WIDTH_NBSP_CHAR,
blank: blankHTML,
emptyPara: '
' + blankHTML + '
',
makePredByNodeName: makePredByNodeName,
isEditable: isEditable,
isControlSizing: isControlSizing,
isText: isText,
isElement: isElement,
isVoid: isVoid,
isPara: isPara,
isPurePara: isPurePara,
isHeading: isHeading,
isInline: isInline,
isBlock: func.not(isInline),
isBodyInline: isBodyInline,
isBody: isBody,
isParaInline: isParaInline,
isPre: isPre,
isList: isList,
isTable: isTable,
isData: isData,
isCell: isCell,
isBlockquote: isBlockquote,
isBodyContainer: isBodyContainer,
isAnchor: isAnchor,
isDiv: makePredByNodeName('DIV'),
isLi: isLi,
isBR: makePredByNodeName('BR'),
isSpan: makePredByNodeName('SPAN'),
isB: makePredByNodeName('B'),
isU: makePredByNodeName('U'),
isS: makePredByNodeName('S'),
isI: makePredByNodeName('I'),
isImg: makePredByNodeName('IMG'),
isTextarea: isTextarea,
isEmpty: isEmpty,
isEmptyAnchor: func.and(isAnchor, isEmpty),
isClosestSibling: isClosestSibling,
withClosestSiblings: withClosestSiblings,
nodeLength: nodeLength,
isLeftEdgePoint: isLeftEdgePoint,
isRightEdgePoint: isRightEdgePoint,
isEdgePoint: isEdgePoint,
isLeftEdgeOf: isLeftEdgeOf,
isRightEdgeOf: isRightEdgeOf,
isLeftEdgePointOf: isLeftEdgePointOf,
isRightEdgePointOf: isRightEdgePointOf,
prevPoint: prevPoint,
nextPoint: nextPoint,
isSamePoint: isSamePoint,
isVisiblePoint: isVisiblePoint,
prevPointUntil: prevPointUntil,
nextPointUntil: nextPointUntil,
isCharPoint: isCharPoint,
walkPoint: walkPoint,
ancestor: ancestor,
singleChildAncestor: singleChildAncestor,
listAncestor: listAncestor,
lastAncestor: lastAncestor,
listNext: listNext,
listPrev: listPrev,
listDescendant: listDescendant,
commonAncestor: commonAncestor,
wrap: wrap,
insertAfter: insertAfter,
appendChildNodes: appendChildNodes,
position: position,
hasChildren: hasChildren,
makeOffsetPath: makeOffsetPath,
fromOffsetPath: fromOffsetPath,
splitTree: splitTree,
splitPoint: splitPoint,
create: create,
createText: createText,
remove: remove,
removeWhile: removeWhile,
replace: replace,
html: html,
value: value,
posFromPlaceholder: posFromPlaceholder,
attachEvents: attachEvents,
detachEvents: detachEvents
};
})();
var Context = function ($note, options) {
var self = this;
var ui = $.wysiwyg.ui;
this.memos = {};
this.modules = {};
this.layoutInfo = {};
this.options = options;
this.initialize = function () {
this.layoutInfo = ui.createLayout($note, options);
this._initialize();
$note.hide();
return this;
};
this.destroy = function () {
this._destroy();
$note.removeData('wysiwyg');
ui.removeLayout($note, this.layoutInfo);
};
this.reset = function () {
var disabled = self.isDisabled();
this.code(dom.emptyPara);
this._destroy();
this._initialize();
if (disabled) self.disable();
};
this._initialize = function () {
var buttons = $.extend({}, this.options.buttons);
Object.keys(buttons).forEach(function (key) {
self.memo('button.' + key, buttons[key]);
});
var modules = $.extend({}, this.options.modules, $.wysiwyg.plugins || {});
Object.keys(modules).forEach(function (key) {
self.module(key, modules[key], true);
});
Object.keys(this.modules).forEach(function (key) {
self.initializeModule(key);
});
};
this._destroy = function () {
Object.keys(this.modules).reverse().forEach(function (key) {
self.removeModule(key);
});
Object.keys(this.memos).forEach(function (key) {
self.removeMemo(key);
});
};
this.code = function (html) {
var isActivated = this.invoke('codeview.isActivated');
if (html === undefined) {
this.invoke('codeview.sync');
return isActivated ? this.layoutInfo.codable.val() : this.layoutInfo.editable.html();
} else {
if (isActivated) {
this.layoutInfo.codable.val(html);
} else {
this.layoutInfo.editable.html(html);
}
$note.val(html);
this.triggerEvent('change', html);
}
};
this.isDisabled = function () {
return this.layoutInfo.editable.attr('contenteditable') === 'false';
};
this.enable = function () {
this.layoutInfo.editable.attr('contenteditable', true);
this.invoke('toolbar.activate', true);
};
this.disable = function () {
if (this.invoke('codeview.isActivated')) this.invoke('codeview.deactivate');
this.layoutInfo.editable.attr('contenteditable', false);
this.invoke('toolbar.deactivate', true);
};
this.triggerEvent = function () {
var namespace = list.head(arguments);
var args = list.tail(list.from(arguments));
var callback = this.options.callbacks[func.namespaceToCamel(namespace, 'on')];
if (callback) callback.apply($note[0], args);
$note.trigger('wysiwyg.' + namespace, args);
};
this.initializeModule = function (key) {
var module = this.modules[key];
module.shouldInitialize = module.shouldInitialize || func.ok;
if (!module.shouldInitialize()) return;
if (module.initialize) module.initialize();
if (module.events) dom.attachEvents($note, module.events);
};
this.module = function (key, ModuleClass, withoutIntialize) {
if (arguments.length === 1) return this.modules[key];
this.modules[key] = new ModuleClass(this);
if (!withoutIntialize) this.initializeModule(key);
};
this.removeModule = function (key) {
var module = this.modules[key];
if (module.shouldInitialize()) {
if (module.events) dom.detachEvents($note, module.events);
if (module.destroy) module.destroy();
}
delete this.modules[key];
};
this.memo = function (key, obj) {
if (arguments.length === 1) return this.memos[key];
this.memos[key] = obj;
};
this.removeMemo = function (key) {
if (this.memos[key] && this.memos[key].destroy) this.memos[key].destroy();
delete this.memos[key];
};
this.createInvokeHandler = function (namespace, value) {
return function (event) {
event.preventDefault();
self.invoke(namespace, value || $(event.target).closest('[data-value]').data('value'));
};
};
this.invoke = function () {
var namespace = list.head(arguments);
var args = list.tail(list.from(arguments));
var splits = namespace.split('.');
var hasSeparator = splits.length > 1;
var moduleName = hasSeparator && list.head(splits);
var methodName = hasSeparator ? list.last(splits) : list.head(splits);
var module = this.modules[moduleName || 'editor'];
if (!moduleName && this[methodName]) {
return this[methodName].apply(this, args);
} else if (module && module[methodName] && module.shouldInitialize()) {
return module[methodName].apply(module, args);
}
};
return this.initialize();
};
$.fn.extend({
wysiwyg: function () {
var type = $.type(list.head(arguments));
var isExternalAPICalled = type === 'string';
var hasInitOptions = type === 'object';
var options = hasInitOptions ? list.head(arguments) : {};
options = $.extend({}, $.wysiwyg.options, options);
options.langInfo = $.extend(true, {}, $.wysiwyg.lang['en-US'], $.wysiwyg.lang[options.lang]);
options.icons = $.extend(true, {}, $.wysiwyg.options.icons, options.icons);
this.each(function (idx, note) {
var $note = $(note);
if (!$note.data('wysiwyg')) {
var context = new Context($note, options);
$note.data('wysiwyg', context);
$note.data('wysiwyg').triggerEvent('init', context.layoutInfo);
}
});
var $note = this.first();
if ($note.length) {
var context = $note.data('wysiwyg');
if (isExternalAPICalled) {
return context.invoke.apply(context, list.from(arguments));
} else if (options.focus) {
context.invoke('editor.focus');
}
}
return this;
}
});
var Renderer = function (markup, children, options, callback) {
this.render = function ($parent) {
var $node = $(markup);
if (options && options.contents) $node.html(options.contents);
if (options && options.className) $node.addClass(options.className);
if (options && options.data) {
$.each(options.data, function (k, v) {
$node.attr('data-' + k, v);
});
}
if (options && options.click) $node.on('click', options.click);
if (children) {
var $container = $node.find('.wysiwyg-children-container');
children.forEach(function (child) {
child.render($container.length ? $container : $node);
});
}
if (callback) callback($node, options);
if (options && options.callback) options.callback($node);
if ($parent) $parent.append($node);
return $node;
};
};
var renderer = {
create: function (markup, callback) {
return function () {
var children = $.isArray(arguments[0]) ? arguments[0] : [];
var options = typeof arguments[1] === 'object' ? arguments[1] : arguments[0];
if (options && options.children) {
children = options.children;
}
return new Renderer(markup, children, options, callback);
};
}
};
var editor = renderer.create('
');
var toolbar = renderer.create('
');
var editingArea = renderer.create('
');
var codable = renderer.create('
');
var editable = renderer.create('
');
var statusbar = renderer.create([
'
'
].join(''));
var airEditor = renderer.create('
');
var airEditable = renderer.create('
');
var buttonGroup = renderer.create('
');
var button = renderer.create('