From 325fe554eae88b8d98310c78cfbbbabc9af11689 Mon Sep 17 00:00:00 2001 From: Vitaliy Filippov Date: Wed, 12 Aug 2015 01:03:09 +0300 Subject: [PATCH] Move all plugins to separate files --- build.sh | 6 + nicAdvancedButton.js | 110 +++++++ nicButtonTips.js | 32 ++ nicCodeButton.js | 29 ++ nicColors.js | 58 ++++ nicEdit.js => nicCore.js | 676 --------------------------------------- nicFloatingPanel.js | 26 ++ nicFontSelect.js | 135 ++++++++ nicPane.js | 60 ++++ nicSanitize.js | 1 + nicTable.js | 86 +++++ nicXHTML.js | 140 ++++++++ 12 files changed, 683 insertions(+), 676 deletions(-) create mode 100644 build.sh create mode 100644 nicAdvancedButton.js create mode 100644 nicButtonTips.js create mode 100644 nicCodeButton.js create mode 100644 nicColors.js rename nicEdit.js => nicCore.js (56%) create mode 100644 nicFloatingPanel.js create mode 100644 nicFontSelect.js create mode 100644 nicPane.js create mode 100644 nicTable.js create mode 100644 nicXHTML.js diff --git a/build.sh b/build.sh new file mode 100644 index 0000000..1ee4f1f --- /dev/null +++ b/build.sh @@ -0,0 +1,6 @@ +#!/bin/sh + +cat nicRu.js nicCore.js nicPane.js nicAdvancedButton.js nicButtonTips.js \ + nicFontSelect.js nicColors.js nicXHTML.js nicCodeButton.js nicTable.js nicFloatingPanel.js \ + nicSanitize.js sanitize-html.js > nicEdit.js +yui-compressor nicEdit.js > nicEdit.min.js diff --git a/nicAdvancedButton.js b/nicAdvancedButton.js new file mode 100644 index 0000000..0fed261 --- /dev/null +++ b/nicAdvancedButton.js @@ -0,0 +1,110 @@ +/** nicAdvancedButton */ + +var nicEditorAdvancedButton = nicEditorButton.extend({ + + init : function() { + this.ne.addEvent('selected',this.removePane.closure(this)).addEvent('blur',this.removePane.closure(this)); + }, + + mouseClick : function() { + if(!this.isDisabled) { + if(this.pane && this.pane.pane) { + this.removePane(); + } else { + this.pane = new nicEditorPane(this.contain,this.ne,{width : (this.width || '270px'), backgroundColor : '#fff'},this); + this.addPane(); + this.ne.selectedInstance.saveRng(); + } + } + }, + + addForm : function(f,elm) { + this.form = new bkElement('form').setAttributes({className: 'niceabf'}).addEvent('submit',this.submit.closureListener(this)); + this.pane.append(this.form); + this.inputs = {}; + if (!document._nicCss) { + document._nicCss = document.createElement('style'); + document._nicCss.appendChild(document.createTextNode( + '.niceabf table { border-collapse: collapse; }\n'+ + '.niceabf td { padding: 2px 5px 2px 0; }\n'+ + '.niceabf td.h { vertical-align: top; padding-top: 4px; white-space: nowrap; }\n'+ + '.niceabf h2 { font-size: 14px; font-weight: bold; padding: 0; margin: 0; }\n'+ + '.niceabf input, .niceabf select { text-transform: none; font-weight: normal; height: auto; padding: 1px; vertical-align: middle; font-size: 13px; border: 1px solid #ccc; }\n'+ + '.niceabf textarea { border: 1px solid #ccc; }\n'+ + '.niceabf input.button { background-color: #efefef; color: black; margin: 3px 0; }\n' + )); + document.getElementsByTagName('head')[0].appendChild(document._nicCss); + } + var tab, tr, td; + + for(itm in f) { + var field = f[itm]; + var val = ''; + if(elm) { + val = elm.getAttribute(itm); + } + if(!val) { + val = field['value'] || ''; + } + var type = f[itm].type; + + if(type == 'title') { + new bkElement('h2').setContent(field.txt).appendTo(this.form); + } else { + if (!tab) { + tab = new bkElement('table').appendTo(this.form); + } + tr = new bkElement('tr').appendTo(tab); + if(field.txt) { + td = new bkElement('td').setAttributes({className: 'h'}).appendTo(tr); + new bkElement('label').setAttributes({htmlFor: itm}).setContent(field.txt).appendTo(td); + } + td = new bkElement('td').appendTo(tr); + if(!field.txt) { + td.setAttributes({colspan: 2}); + } + + switch(type) { + case 'text': + this.inputs[itm] = new bkElement('input').setAttributes({id: itm, value: val, type: 'text'}).setStyle(field.style).appendTo(td); + break; + case 'select': + this.inputs[itm] = new bkElement('select').setAttributes({id: itm}).appendTo(td); + for(opt in field.options) { + var o = new bkElement('option').setAttributes({value: opt, selected: (opt == val) ? 'selected' : ''}).setContent(field.options[opt]).appendTo(this.inputs[itm]); + } + break; + case 'content': + this.inputs[itm] = new bkElement('textarea').setAttributes({id: itm}).setStyle(field.style).appendTo(td); + this.inputs[itm].value = val; + break; + case 'container': + this.inputs[itm] = td; + break; + } + } + } + new bkElement('input').setAttributes({type: 'submit', value: __('Submit'), className: 'button'}).appendTo(this.form); + this.form.onsubmit = bkLib.cancelEvent; + }, + + submit : function() { }, + + findElm : function(tag,attr,val) { + var list = this.ne.selectedInstance.getElm().getElementsByTagName(tag); + 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' }); - } -}); diff --git a/nicFloatingPanel.js b/nicFloatingPanel.js new file mode 100644 index 0000000..19a268d --- /dev/null +++ b/nicFloatingPanel.js @@ -0,0 +1,26 @@ +/** 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' }); + } +}); + diff --git a/nicFontSelect.js b/nicFontSelect.js new file mode 100644 index 0000000..03ea23d --- /dev/null +++ b/nicFontSelect.js @@ -0,0 +1,135 @@ +/** nicSelect */ + +/* START CONFIG */ +var nicSelectOptions = { + buttons : { + 'fontSize' : {name : __('Select Font Size'), type : 'nicEditorFontSizeSelect', command : 'fontsize'}, + 'fontFamily' : {name : __('Select Font Family'), type : 'nicEditorFontFamilySelect', command : 'fontname'}, + 'fontFormat' : {name : __('Select Font Format'), type : 'nicEditorFontFormatSelect', command : 'formatBlock'} + } +}; +/* END CONFIG */ +var nicEditorSelect = bkClass.extend({ + + construct : function(e,buttonName,options,nicEditor) { + this.options = options.buttons[buttonName]; + this.elm = e; + this.ne = nicEditor; + this.name = buttonName; + this.selOptions = new Array(); + + this.margin = new bkElement('div').setStyle({'float' : 'left', margin : '2px 1px 0 1px'}).appendTo(this.elm); + this.contain = new bkElement('div').setStyle({width: '90px', height : '20px', cursor : 'pointer', overflow: 'hidden'}).addClass('selectContain').addEvent('click',this.toggle.closure(this)).appendTo(this.margin); + this.items = new bkElement('div').setStyle({overflow : 'hidden', zoom : 1, border: '1px solid #ccc', paddingLeft : '3px', backgroundColor : '#fff'}).appendTo(this.contain); + this.control = new bkElement('div').setStyle({overflow : 'hidden', 'float' : 'right', height: '18px', width : '16px'}).addClass('selectControl').setStyle(this.ne.getIcon('arrow',options)).appendTo(this.items); + this.txt = new bkElement('div').setStyle({overflow : 'hidden', 'float' : 'left', width : '66px', height : '14px', marginTop : '1px', fontFamily : 'sans-serif', textAlign : 'center', fontSize : '12px'}).addClass('selectTxt').appendTo(this.items); + + if(!window.opera) { + this.contain.onmousedown = this.control.onmousedown = this.txt.onmousedown = bkLib.cancelEvent; + } + + this.margin.noSelect(); + + this.ne.addEvent('selected', this.enable.closure(this)).addEvent('blur', this.disable.closure(this)); + + this.disable(); + this.init(); + }, + + disable : function() { + this.isDisabled = true; + this.close(); + this.contain.setStyle({opacity : 0.6}); + }, + + enable : function(t) { + this.isDisabled = false; + this.close(); + this.contain.setStyle({opacity : 1}); + }, + + setDisplay : function(txt) { + this.txt.setContent(txt); + }, + + toggle : function() { + if(!this.isDisabled) { + (this.pane) ? this.close() : this.open(); + } + }, + + open : function() { + this.pane = new nicEditorPane(this.items,this.ne,{width : '88px', padding: '0px', borderTop : 0, borderLeft : '1px solid #ccc', borderRight : '1px solid #ccc', borderBottom : '0px', backgroundColor : '#fff'}); + + 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); + diff --git a/nicPane.js b/nicPane.js new file mode 100644 index 0000000..f646e7e --- /dev/null +++ b/nicPane.js @@ -0,0 +1,60 @@ +/** nicPane */ + +/* START CONFIG */ +var nicPaneOptions = { }; +/* END CONFIG */ + +var nicEditorPane = bkClass.extend({ + construct : function(elm,nicEditor,options,openButton) { + this.ne = nicEditor; + this.elm = elm; + this.pos = elm.pos(); + + this.contain = new bkElement('div').setStyle({zIndex: '999', overflow: 'hidden', position: 'absolute', left: this.pos[0]+'px', top : (this.pos[1]+elm.offsetHeight)+'px'}) + this.pane = new bkElement('div').setStyle({fontSize: '12px', border: '1px solid #ccc', overflow: 'hidden', padding: '4px', textAlign: 'left', backgroundColor : '#ffffc9'}).addClass('pane').setStyle(options).appendTo(this.contain); + + if(openButton && !openButton.options.noClose) { + this.close = new bkElement('div').setStyle({float: 'right', height: '16px', width: '16px', cursor: 'pointer'}).setStyle(this.ne.getIcon('close',nicPaneOptions)).addEvent('mousedown',openButton.removePane.closure(this)).appendTo(this.pane); + } + + this.contain.noSelect().appendTo(document.body); + + this.position(); + this.init(); + }, + + init : function() { }, + + position : function() { + if(this.ne.nicPanel) { + var panelElm = this.ne.nicPanel.elm; + var panelPos = panelElm.pos(); + var newLeft = panelPos[0]+parseInt(panelElm.getStyle('width'))-(parseInt(this.pane.getStyle('width'))+8); + if(newLeft < this.pos[0]) { + this.contain.setStyle({left : newLeft+'px'}); + } + } + }, + + toggle : function() { + this.isVisible = !this.isVisible; + this.contain.setStyle({display : ((this.isVisible) ? 'block' : 'none')}); + }, + + remove : function() { + if(this.contain) { + this.contain.remove(); + this.contain = null; + } + }, + + append : function(c) { + c.appendTo(this.pane); + }, + + setContent : function(c) { + this.pane.setContent(c); + } + +}); + diff --git a/nicSanitize.js b/nicSanitize.js index 4191e85..b006349 100644 --- a/nicSanitize.js +++ b/nicSanitize.js @@ -28,3 +28,4 @@ var nicSanitize = bkClass.extend({ }); nicEditors.registerPlugin(nicSanitize); + diff --git a/nicTable.js b/nicTable.js new file mode 100644 index 0000000..003e87a --- /dev/null +++ b/nicTable.js @@ -0,0 +1,86 @@ +/** 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); + diff --git a/nicXHTML.js b/nicXHTML.js new file mode 100644 index 0000000..06903db --- /dev/null +++ b/nicXHTML.js @@ -0,0 +1,140 @@ +/** 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); +