var Editor = Class.create(); Editor.prototype = { initialize: function(element) { this.element = element; }, getWikiElement: function() { return new WikiElement(this, this.getCaretPosition()); }, getCaretPosition: function() { if (this.element.selectionStart || this.element.selectionStart == '0') { return this.element.selectionStart; } else if (document.selection) { this.element.focus(); sel = document.selection.createRange(); var lstr = sel.text.length; sel.moveStart('character', -100000); return sel.text.length - lstr - 64; } }, setCaretPosition: function(position) { if (this.element.selectionStart || this.element.selectionStart == '0') { this.element.selectionStart = position; this.element.selectionEnd = position; } }, getEditorText: function() { return this.element.value; }, setEditorText: function(value) { this.element.value = value; }, replaceSelection: function(value) { if (this.element.selectionStart || this.element.selectionStart == '0') { var startPos = this.element.selectionStart; var endPos = this.element.selectionEnd; this.element.value = this.element.value.substring(0, startPos) + value + this.element.value.substring(endPos, this.element.value.length); this.element.selectionStart = startPos + value.length; this.element.selectionEnd = this.element.selectionStart; } else if (document.selection) { this.element.focus(); sel = document.selection.createRange(); sel.text = value; } else { this.element.value += value; } }, encloseSelection: function(startValue, endValue) { if (this.element.selectionStart || this.element.selectionStart == '0') { var startPos = this.element.selectionStart; var endPos = this.element.selectionEnd; var currValue = this.element.value; this.element.value = currValue.substring(0, startPos) + startValue + currValue.substring(startPos, endPos) + endValue + currValue.substring(endPos, currValue.length); this.element.selectionStart = startPos; this.element.selectionEnd = this.element.selectionStart; } else if (document.selection) { this.element.focus(); sel = document.selection.createRange(); sel.text = startValue + sel.text + endValue; } else { this.element.value = startValue + this.element.value + endValue; } } } var WikiElement = Class.create(); WikiElement.prototype = { initialize: function(element, position) { this.element = element; this.position = position; this.startPos = position; this.endPos = position; this.strEl = null; this.attrs = null; this.getFromPosition(); }, isSimpleText: function() { return this.strEl == null; }, isValid: function() { return !this.isSimpleText() && this.strEl.charAt(1) != '/'; }, isImage: function() { return this.getName() == "img" || this.getName() == "thumb"; }, isThumb: function() { return this.getName() == "thumb"; }, isLink: function() { var str = this.element.getEditorText(); return this.startPos > 0 && str.charAt(this.startPos - 1) == '['; }, isTextLink: function() { var str = this.element.getEditorText(); return this.endPos < str.length && str.charAt(this.endPos + 1) == ']'; }, hasCSS: function() { var wname = this.getName(); return this.isValid() && $A(["block", "span", "column", "tab", "blind", "note"]).indexOf(wname) > -1; }, getFromPosition: function() { var str = this.element.getEditorText(); var strLen = str.length; var reversed = 0; var first = true; while(this.startPos >= 0 && (str.charAt(this.startPos) != '[' || reversed > 0)) { if(!first && str.charAt(this.startPos) == ']') reversed++; this.startPos--; first = false; } if(this.startPos < 0 || reversed > 0) return; reversed = 0; while(this.endPos < strLen && (str.charAt(this.endPos) != ']' || reversed > 0)) { if(str.charAt(this.endPos) == '[') reversed++; this.endPos++; } if(this.endPos >= strLen || reversed > 0) return; this.strEl = str.substring(this.startPos, this.endPos + 1); }, getName: function() { if(this.isValid()) { var p = this.strEl.indexOf(' '), tmp = this.strEl.indexOf('('); if((tmp > 0 && p > tmp) || p < 0) p = tmp if(p > -1) return this.strEl.substring(1, p); return this.strEl.substring(1, this.strEl.length - 1); } return null; }, getContent: function() { var wname = this.getName(); if(this.hasCSS()) { var pos = this.element.getEditorText().indexOf("[/" + wname + "]", this.startPos); if(pos == -1) { pos = this.element.getEditorText().indexOf("[" + wname + "]", this.startPos); } if(pos > -1) { return this.element.getEditorText().substring(this.endPos + 1, pos); } return wname; } }, getExtraText: function() { var l = this.getName().length; return this.strEl.substring(l + 1, this.strEl.length - 1); }, setExtraText: function(value) { this.strEl = "[" + this.getName() + value + "]"; }, changeElementName: function(name) { if(this.isValid()) { var sp = this.strEl.indexOf(' '); if(sp < 0) sp = this.strEl.indexOf('('); if(sp > -1) this.strEl = "[" + name + this.strEl.substring(sp); else this.strEl = "[" + name + "]"; } }, getInsideText: function() { return this.strEl.substring(1, this.strEl.length -1); }, getImageLocation: function(repository) { var extra = this.getExtraText(); if(extra) { var p = extra.indexOf(":"); if(p > -1) extra = extra.substring(0, p); return extra.replace(/repository\((.*)\)/, repository + "$1"); } }, getOuterLink: function() { var str = this.element.getEditorText(), startStr = this.startPos - 1; while(startStr >= 0 && str.charAt(startStr) != '[') { startStr--; } return str.substring(startStr + 1, this.startPos - 1); }, setAttributesTo: function(anEl) { if(this.hasCSS()) { this.getAttributes(); $H(this.attrs).each(function(node) { if(node.value) { if(node.value == "notset") return; anEl.style[node.key.camelize()] = node.value; } }); } }, getAttributes: function() { if(this.hasCSS() && this.attrs == null) { this.attrs = new Array(); var sp = this.strEl.indexOf('('), ep = this.strEl.indexOf(')'); if(sp > -1 && ep > -1) { var strAttr = this.strEl.substring(sp + 1, ep).split(/,/); for(i = 0; i < strAttr.length; i++) { var strVal = strAttr[i].split(/:/); if(strVal.length == 2) { this.attrs[strVal[0]] = strVal[1]; } else { this.attrs[strVal[0]] = null; } } } } return this.attrs; }, getAttribute: function(key) { if(this.hasCSS() && this.getAttributes() != null) { return this.attrs[key]; } }, getAttributeStr: function(key, matching) { if(this.hasCSS() && this.getAttributes() != null) { var value = this.attrs[key]; if(value) { if(!matching) return value; else { matcher = value.match(matching); return matcher[1] ? matcher[1] : ""; } } else return ""; } }, setAttribute: function(key, value) { if(this.hasCSS() && this.getAttributes() != null) { this.attrs[key] = value; } }, render: function() { if(this.isValid()) { this.getAttributes(); var attrStr = "", sep = ""; $H(this.attrs).each(function(node) { if(node.value && (Object.isFunction(node.value) || node.value == "notset")) return; attrStr += sep + node.key; if(node.value) { attrStr += ":" + node.value; } sep = ","; }); this.element.setEditorText( this.element.getEditorText().substring(0, this.startPos) + "[" + this.getName() + (attrStr != null ? "(" + attrStr + ")" : "") + "]" + this.element.getEditorText().substring(this.endPos + 1)); this.element.setCaretPosition(this.position); } }, renderFlat: function() { if(this.isValid()) { this.element.setEditorText( this.element.getEditorText().substring(0, this.startPos) + this.strEl + this.element.getEditorText().substring(this.endPos + 1)); this.element.setCaretPosition(this.position); } } }