/** * NicEdit - Micro Inline WYSIWYG * Copyright 2007-2008 Brian Kirchoff, http://nicedit.com/ * Copyright 2012-2015 Vitaliy Filippov, http://yourcmc.ru/wiki/nicEdit * Version: 2015-08-12 * * NicEdit is distributed under the terms of the MIT license * Do not remove this copyright message */ var bkExtend = function(){ var args = arguments; if (args.length == 1) args = [this, args[0]]; for (var prop in args[1]) args[0][prop] = args[1][prop]; return args[0]; }; function bkClass() { } bkClass.prototype.construct = function() {}; bkClass.extend = function(def) { var classDef = function() { if (arguments[0] !== bkClass) { return this.construct.apply(this, arguments); } }; var proto = new this(bkClass); bkExtend(proto,def); classDef.prototype = proto; classDef.extend = this.extend; return classDef; }; var bkElement = bkClass.extend({ construct : function(elm,d) { if(typeof(elm) == "string") { elm = (d || document).createElement(elm); } elm = $BK(elm); return elm; }, appendTo : function(elm) { elm.appendChild(this); return this; }, appendBefore : function(elm) { elm.parentNode.insertBefore(this,elm); return this; }, addEvent : function(type, fn) { bkLib.addEvent(this,type,fn); return this; }, removeAllEvents : function() { bkLib.removeAllEvents(this); return this; }, setContent : function(c) { this.innerHTML = c; return this; }, pos : function() { var curleft = curtop = 0; var o = obj = this; if (obj.offsetParent) { do { curleft += obj.offsetLeft; curtop += obj.offsetTop; } while (obj = obj.offsetParent); } var b = (!window.opera) ? parseInt(this.getStyle('border-width') || this.style.border) || 0 : 0; return [curleft+b,curtop+b]; }, noSelect : function() { bkLib.noSelect(this); return this; }, parentTag : function(t) { var elm = this; do { if(elm && elm.nodeName && elm.nodeName.toUpperCase() == t) { return elm; } elm = elm.parentNode; } while(elm); return false; }, hasClass : function(cls) { return this.className.match(new RegExp('(\\s|^)nicEdit-'+cls+'(\\s|$)')); }, addClass : function(cls) { if (!this.hasClass(cls)) { this.className += " nicEdit-"+cls }; return this; }, removeClass : function(cls) { if (this.hasClass(cls)) { this.className = this.className.replace(new RegExp('(\\s|^)nicEdit-'+cls+'(\\s|$)'),' '); } return this; }, setStyle : function(st) { var elmStyle = this.style; for(var itm in st) { switch(itm) { case 'float': elmStyle['cssFloat'] = elmStyle['styleFloat'] = st[itm]; break; case 'opacity': elmStyle.opacity = st[itm]; elmStyle.filter = "alpha(opacity=" + Math.round(st[itm]*100) + ")"; break; case 'className': this.className = st[itm]; break; default: //if(document.compatMode || itm != "cursor") { // Nasty Workaround for IE 5.5 elmStyle[itm] = st[itm]; //} } } return this; }, getStyle : function( cssRule, d ) { var doc = (!d) ? document.defaultView : d; if(this.nodeType == 1) return (doc && doc.getComputedStyle) ? doc.getComputedStyle( this, null ).getPropertyValue(cssRule) : this.currentStyle[ bkLib.camelize(cssRule) ]; }, remove : function() { this.parentNode.removeChild(this); return this; }, setAttributes : function(at) { for(var itm in at) { this[itm] = at[itm]; } return this; } }); var bkLib = { isMSIE : (navigator.appVersion.indexOf("MSIE") != -1), addEvent : function(obj, type, fn) { obj._bkE = obj._bkE||[]; obj._bkE.push([type, fn]); (obj.addEventListener) ? obj.addEventListener( type, fn, false ) : obj.attachEvent("on"+type, fn); }, removeEvent : function(obj, type, fn) { (obj.removeEventListener) ? obj.removeEventListener( type, fn, false ) : obj.detachEvent("on"+type, fn); }, removeAllEvents : function(obj) { if (!obj._bkE) return; for (var i = 0; i < obj._bkE.length; i++) { bkLib.removeEvent(obj,obj._bkE[i][0],obj._bkE[i][1]); } obj._bkE = []; }, toArray : function(iterable) { var length = iterable.length, results = new Array(length); while (length--) { results[length] = iterable[length] }; return results; }, noSelect : function(element) { if(element.setAttribute && element.nodeName.toLowerCase() != 'input' && element.nodeName.toLowerCase() != 'textarea') { element.setAttribute('unselectable','on'); } for(var i=0;i'); } this.instanceDoc = document.defaultView; this.elm.addEvent('mousedown',this.selected.closureListener(this)).addEvent('keypress',this.keyDown.closureListener(this)).addEvent('focus',this.selected.closure(this)).addEvent('blur',this.blur.closure(this)).addEvent('keyup',this.selected.closure(this)); this.ne.fireEvent('add',this); }, remove : function() { this.saveContent(); if(this.copyElm || this.options.hasPanel) { this.editorContain.remove(); this.e.setStyle({'display' : 'block'}); this.ne.removePanel(); } this.elm.removeAllEvents(); this.disable(); this.ne.fireEvent('remove',this); }, disable : function() { this.elm.setAttribute('contentEditable','false'); }, getSel : function() { return (window.getSelection) ? window.getSelection() : document.selection; }, getRng : function() { var s = this.getSel(); if (!s) return; if (s.rangeCount > 0) { return s.getRangeAt(0); } else if (typeof s.createRange == 'undefined') { return document.createRange(); } return s.createRange(); }, selRng : function(rng,s) { if(window.getSelection) { s.removeAllRanges(); s.addRange(rng); } else { rng.select(); } }, selElm : function() { var r = this.getRng(); if(!r) { return; } if(r.startContainer) { var contain = r.startContainer; if(r.cloneContents().childNodes.length == 1) { for(var i=0;i'+((css) ? '' : '')+''+this.initialContent+''); fd.close(); this.frameDoc = fd; this.frameWin = $BK(this.elmFrame.contentWindow); this.frameContent = $BK(this.frameWin.document.body).setStyle(this.savedStyles); this.instanceDoc = this.frameWin.document.defaultView; this.heightUpdate(); this.frameDoc.addEvent('mousedown', this.selected.closureListener(this)).addEvent('keyup',this.heightUpdate.closureListener(this)).addEvent('keydown',this.keyDown.closureListener(this)).addEvent('keyup',this.selected.closure(this)); this.ne.fireEvent('add',this); }, getElm : function() { return this.frameContent; }, setContent : function(c) { this.content = c; this.ne.fireEvent('set',this); this.frameContent.innerHTML = this.content; this.heightUpdate(); }, getSel : function() { return (this.frameWin) ? this.frameWin.getSelection() : this.frameDoc.selection; }, heightUpdate : function() { this.elmFrame.style.height = Math.max(this.frameContent.offsetHeight,this.initialHeight)+'px'; }, nicCommand : function(cmd,args) { this.frameDoc.execCommand(cmd,false,args); setTimeout(this.heightUpdate.closure(this),100); } }); var nicEditorPanel = bkClass.extend({ construct : function(e,options,nicEditor) { this.elm = e; this.options = options; this.ne = nicEditor; this.panelButtons = new Array(); this.buttonList = bkExtend([],this.ne.options.buttonList); this.panelContain = new bkElement('DIV').setStyle({overflow : 'hidden', width : '100%', border : '1px solid #cccccc', backgroundColor : '#efefef'}).addClass('panelContain'); this.panelElm = new bkElement('DIV').setStyle({margin : '2px', marginTop : '0px', zoom : 1, overflow : 'hidden'}).addClass('panel').appendTo(this.panelContain); this.panelContain.appendTo(e); var opt = this.ne.options; var buttons = opt.buttons; for(button in buttons) { this.addButton(button,opt,true); } this.reorder(); e.noSelect(); }, addButton : function(buttonName,options,noOrder) { var button = options.buttons[buttonName]; var type = null; if (button['type']) { type = typeof(window[button['type']]) == undefined ? null : window[button['type']]; } else { type = nicEditorButton; } var hasButton = bkLib.inArray(this.buttonList,buttonName); if(type && (hasButton || this.ne.options.fullPanel)) { this.panelButtons.push(new type(this.panelElm,buttonName,options,this.ne)); if(!hasButton) { this.buttonList.push(buttonName); } } }, findButton : function(itm) { for(var i=0;i'+this.sel[itm]+''); } } }); var nicEditorFontFamilySelect = nicEditorSelect.extend({ sel : {'sans-serif':__('Sans-Serif'), 'serif':__('Serif'), 'fantasy':__('Fantasy'), 'monospace':__('Monospace'), 'cursive':__('Cursive'), 'georgia':'Georgia'}, init : function() { this.setDisplay(__('Font Family...')); for(itm in this.sel) { this.add(itm,''+this.sel[itm]+''); } } }); var nicEditorFontFormatSelect = nicEditorSelect.extend({ sel : {'p' : __('Paragraph'), 'pre' : __('Pre'), 'h6' : __('Heading 6'), 'h5' : __('Heading 5'), 'h4' : __('Heading 4'), 'h3' : __('Heading 3'), 'h2' : __('Heading 2'), 'h1' : __('Heading 1')}, init : function() { this.setDisplay(__('Font Format...')); for(itm in this.sel) { var tag = itm.toUpperCase(); this.add('<'+tag+'>','<'+itm+' style="padding: 0px; margin: 0px;">'+this.sel[itm]+''); } } }); nicEditors.registerPlugin(nicPlugin,nicSelectOptions); /** nicColors */ /* START CONFIG */ var nicColorOptions = { buttons : { 'forecolor' : {name : __('Change Text Color'), type : 'nicEditorColorButton', noClose : true}, 'bgcolor' : {name : __('Change Background Color'), type : 'nicEditorBgColorButton', noClose : true} } }; /* END CONFIG */ var nicEditorColorButton = nicEditorAdvancedButton.extend({ addPane : function() { var colorList = {0 : '00',1 : '33',2 : '66',3 :'99',4 : 'CC',5 : 'FF'}; var colorItems = new bkElement('DIV').setStyle({width: '270px'}); for(var r in colorList) { for(var b in colorList) { for(var g in colorList) { var colorCode = '#'+colorList[r]+colorList[g]+colorList[b]; var colorSquare = new bkElement('DIV').setStyle({'cursor' : 'pointer', 'height' : '15px', 'float' : 'left'}).appendTo(colorItems); var colorBorder = new bkElement('DIV').setStyle({border: '2px solid '+colorCode}).appendTo(colorSquare); var colorInner = new bkElement('DIV').setStyle({backgroundColor : colorCode, overflow : 'hidden', width : '11px', height : '11px'}).addEvent('click',this.colorSelect.closure(this,colorCode)).addEvent('mouseover',this.on.closure(this,colorBorder)).addEvent('mouseout',this.off.closure(this,colorBorder,colorCode)).appendTo(colorBorder); if(!window.opera) { colorSquare.onmousedown = colorInner.onmousedown = bkLib.cancelEvent; } } } } this.pane.append(colorItems.noSelect()); }, colorSelect : function(c) { this.ne.nicCommand('foreColor',c); this.removePane(); }, on : function(colorBorder) { colorBorder.setStyle({border : '2px solid #000'}); }, off : function(colorBorder,colorCode) { colorBorder.setStyle({border : '2px solid '+colorCode}); } }); var nicEditorBgColorButton = nicEditorColorButton.extend({ colorSelect : function(c) { this.ne.nicCommand('hiliteColor',c); this.removePane(); } }); nicEditors.registerPlugin(nicPlugin,nicColorOptions); /** nicXHTML */ var nicXHTML = bkClass.extend({ stripAttributes : ['_moz_dirty','_moz_resizing','_extended'], noShort : ['style','title','script','textarea','a'], cssReplace : {'font-weight:bold;' : 'strong', 'font-style:italic;' : 'em'}, sizes : {1 : 'xx-small', 2 : 'x-small', 3 : 'small', 4 : 'medium', 5 : 'large', 6 : 'x-large'}, construct : function(nicEditor) { this.ne = nicEditor; if(this.ne.options.xhtml) { nicEditor.addEvent('get',this.cleanup.closure(this)); } }, cleanup : function(ni) { var node = ni.getElm(); var xhtml = this.toXHTML(node); ni.content = xhtml; }, toXHTML : function(n,r,d) { var txt = ''; var attrTxt = ''; var cssTxt = ''; var nType = n.nodeType; var nName = n.nodeName.toLowerCase(); var nChild = n.hasChildNodes && n.hasChildNodes(); var extraNodes = new Array(); switch(nType) { case 1: var nAttributes = n.attributes; switch(nName) { case 'b': nName = 'strong'; break; case 'i': nName = 'em'; break; case 'font': nName = 'span'; break; } if(r) { for(var i=0;i'; } if(attrTxt == "" && nName == "span") { r = false; } if(r) { txt += '<'+nName; if(nName != 'br') { txt += attrTxt; } } } if(!nChild && !bkLib.inArray(this.noShort,attributeName)) { if(r) { txt += ' />'; } } else { if(r) { txt += '>'; } for(var i=0;i'; } for(var i=0;i'; } break; case 3: //if(n.nodeValue != '\n') { txt += n.nodeValue; //} break; } return txt; } }); nicEditors.registerPlugin(nicXHTML); /** nicCode */ /* START CONFIG */ var nicCodeOptions = { buttons : { 'xhtml' : {name : __('Edit HTML'), type : 'nicCodeButton'} } }; /* END CONFIG */ var nicCodeButton = nicEditorAdvancedButton.extend({ width : '350px', addPane : function() { this.addForm({ '' : {type : 'title', txt : __('Edit HTML')}, 'code' : {type : 'content', 'value' : this.ne.selectedInstance.getContent(), style : {width: '340px', height : '200px'}} }); }, submit : function(e) { var code = this.inputs['code'].value; this.ne.selectedInstance.setContent(code); this.removePane(); } }); nicEditors.registerPlugin(nicPlugin,nicCodeOptions); /** nicTable (c) Vitaliy Filippov */ /* START CONFIG */ var nicTableOptions = { buttons : { 'table' : {name : __('Add Table'), type : 'nicTableButton', tags : ['TABLE']} } }; /* END CONFIG */ var nicTableButton = nicEditorAdvancedButton.extend({ addPane: function() { this.t = this.ne.selectedInstance.selElm().parentTag('TABLE'); var r = 3, c = 3, h = '', b = 'yes'; if (this.t && (r = this.t.rows.length)) { c = this.t.rows[0].cells.length; if (this.t.rows[0].cells[c-1].nodeName == 'TH') h += 'top'; if (this.t.rows[r-1].cells[0].nodeName == 'TH') h += 'left'; if (this.t.className.indexOf('bordered') < 0) b = 'no'; } this.addForm({ '': {type: 'title', txt: __('Add/Edit Table')}, 'cols': {type: 'text', txt: __('Columns'), value: c, style: {width: '50px'}}, 'rows': {type: 'text', txt: __('Rows'), value: r, style: {width: '50px'}}, 'header': {type: 'select', txt: __('Headers'), value: h, options: {'':__('None'), left:__('Left'), top:__('Top'), topleft:__('Top and Left')}}, 'bordered': {type: 'select', txt: __('Borders'), value: b, options: {'no':__('No'), 'yes':__('Yes')}} },this.t); }, submit: function(e) { var r = parseInt(this.inputs['rows'].value); var c = parseInt(this.inputs['cols'].value); var cl = this.inputs['bordered'].value == 'no' ? '' : 'bordered'; var i, j; if(!this.t) { var tmp = 'javascript:nicImTemp();', h = ''; for (i = 0; i < r; i++) { h += ''+(new Array(c+1)).join('-')+''; } this.ne.nicCommand("insertHTML", ''+h+'
'); this.t = this.findElm('TABLE','title',tmp); } if(this.t) { this.t.className = cl; this.t.removeAttribute('title'); for (i = this.t.rows.length-1; i >= r; i--) { this.t.deleteRow(r); } for (i = this.t.rows.length; i < r; i++) { this.t.insertRow(i).innerHTML = (new Array(c+1)).join('-'); } if (this.t.rows.length && this.t.rows[0].cells.length != c) { for (i = 0; i < r; i++) { for (j = this.t.rows[i].cells.length; j < c; j++) { this.t.rows[i].insertCell(j).innerHTML = '-'; } for (j = this.t.rows[i].cells.length-1; j >= c; j--) { $BK(this.t.rows[i].cells[c]).remove(); } } } if (this.t.rows.length) { var ht = this.inputs['header'].value; var repl = function(node, name) { var nn = document.createElement(name ? 'th' : 'td'); nn.innerHTML = node.innerHTML; node.parentNode.insertBefore(nn, node); node.parentNode.removeChild(node); }; repl(this.t.rows[0].cells[0], ht != ''); j = ht == 'top' || ht == 'topleft'; for (i = 1; i < c; i++) { repl(this.t.rows[0].cells[i], j); } j = ht == 'left' || ht == 'topleft'; for (i = 1; i < r; i++) { repl(this.t.rows[i].cells[0], j); } } } } }); nicEditors.registerPlugin(nicPlugin,nicTableOptions); /** nicFloatingPanel */ nicEditor = nicEditor.extend({ floatingPanel: function() { this.floating = new bkElement('DIV').setStyle({display: 'inline-block', position: 'absolute', left: '-1000px', top: '-1000px', zIndex: 100}).appendTo(document.body); this.addEvent('focus', this.reposition.closure(this)).addEvent('blur', this.hide.closure(this)); bkLib.addEvent(window, 'scroll', this.reposition.closure(this)); this.setPanel(this.floating); }, reposition: function() { var e = this.selectedInstance; if (!e || !(e = e.elm)) return; var h = this.floating.offsetHeight; var p = e.pos(); var d = document; d = window.pageYOffset || d.body.scrollTop || d.documentElement.scrollTop; var top = p[1]-h; this.floating.setStyle({ top: (top < d ? d : top)+'px', left: p[0]+'px' }); }, hide: function() { this.floating.setStyle({ top: '-1000px', left: '-1000px' }); } });