// $Id: bueditor.js,v 1.20.2.10 2009/04/12 23:54:02 ufku Exp $ //Global container var BUE = { 'preset': {push: function(arr) {this[arr[0]] = arr[1];}}, 'instances': [], 'popups': {}, 'dialog': {}, 'templates': {}, 'mode': (window.getSelection || document.getSelection) ? 1 : (document.selection && document.selection.createRange ? 2 : 0 ) }; //Get editor settings from Drupal.settings and process preset textareas. BUE.behavior = function(context) { if (Drupal.settings.BUE) { $.each(Drupal.settings.BUE.templates||{}, function (id, tpl) { BUE.templates[id] = BUE.templates[id] || $.extend({}, tpl); }); Drupal.settings.BUE.templates = {}; $.each(Drupal.settings.BUE.preset||{}, function (id, tplid) { BUE.preset[id] = BUE.preset[id] || tplid; }); Drupal.settings.BUE.preset = {}; } $.each(BUE.preset, function (tid, tplid) { BUE.processTextarea($('#'+ tid, context).get(0), tplid); }); }; //editor settle. BUE.initiate = function () { (Drupal.behaviors.BUE = BUE.behavior)(document); //set editor quickPop. var qp = BUE.quickPop = BUE.createPopup('bue-quickpop'); $(qp.rows[0]).hide(); qp.oldopen = qp.open; qp.open = function(content, effect) { qp.oldopen(null, content, effect); $(document).mousedown(qpToEnd); function qpToEnd() {$(document).mouseup(qpEnd);} function qpEnd() {$(document).unbind('mousedown', qpToEnd).unbind('mouseup', qpEnd); qp.close();} }; //set editor dialog BUE.dialog.popup = BUE.createPopup('bue-dialog'); BUE.dialog.popup.close = function (effect) {BUE.dialog.close(effect);} //fix enter key triggering button click on autocomplete fields. $('input.form-autocomplete').keydown(function(e) {return e.keyCode != 13}); }; //integrate editor template into textarea T BUE.processTextarea = function (T, tplid) { var T = typeof(T) == 'string' ? $('#'+ T).get(0) : T; if (!BUE.templates[tplid] || !T || !T.tagName || T.tagName != 'TEXTAREA' || $(T).is(':hidden')) return false; if (T.editor) return T.editor; var E = new BUE.instance(T, tplid); $(T).focus(function () { if (!(BUE.active == this.editor || BUE.dialog.editor)) { BUE.active.accesskeys(false); BUE.active = this.editor; BUE.active.accesskeys(true); } }); $.each(E.buttons, function(i) { var arr = this.id.split('-'); this.eindex = arr[1]; this.bid = arr[3]; this.bindex = i; this.onclick = function(){return BUE.buttonClick(this.eindex, this.bindex)}; }); if (E.index == 0) { BUE.active = E; } else if (BUE.active.textArea.disabled) { BUE.active.accesskeys(false); BUE.active = E; } else { E.accesskeys(false); } return E; }; //create an editor instance BUE.instance = function (T, tplid) { this.index = BUE.instances.length; this.textArea = T; this.textArea.editor = BUE.instances[this.index] = this; this.tpl = BUE.templates[tplid]; this.UI = $(BUE.theme(tplid).replace(/\%n/g, this.index)).insertBefore(T); this.buttons = $('input.bue', this.UI).get(); this.bindex = null; this.focus = function () { this.textArea.focus(); return this; }; this.getContent = function () { return BUE.processText(this.textArea.value); }; this.setContent = function (content) { var st = this.textArea.scrollTop; this.textArea.value = content; this.textArea.scrollTop = st; return this; }; this.getSelection = function () { var pos = this.posSelection(); return this.getContent().substring(pos.start, pos.end); }; this.replaceSelection = function (txt, cursor) { var txt = BUE.processText(txt); var pos = this.posSelection(); var content = this.getContent(); this.setContent(content.substr(0, pos.start) + txt + content.substr(pos.end)); var end = cursor == 'start' ? pos.start : pos.start+txt.length; var start = cursor == 'end' ? end : pos.start; this.makeSelection(start, end); return this; }; this.tagSelection = function (left, right, cursor) { var left = BUE.processText(left); var right = BUE.processText(right); var llen = left.length; var pos = this.posSelection(); var content = this.getContent(); this.setContent(content.substr(0, pos.start) + left + content.substring(pos.start, pos.end) + right + content.substr(pos.end)); var end = cursor=='start' ? pos.start+llen : pos.end+llen; var start = cursor=='end' ? end : pos.start+llen; this.makeSelection(start, end); return this; }; this.makeSelection = function (start, end) { if (end < start) end = start; BUE.selMake(this.textArea, start, end); if (BUE.dialog.esp) BUE.dialog.esp = {'start': start, 'end': end}; return this; }; this.posSelection = function () { return BUE.dialog.esp ? BUE.dialog.esp : BUE.selPos(this.textArea); }; this.buttonsDisabled = function (state, bindex) { for (var i=0; B = this.buttons[i]; i++) { B.disabled = i == bindex ? !state : state; } return this; }; this.accesskeys = function (state) { for (var i=0; B = this.buttons[i]; i++) { B.accessKey = state ? this.tpl.buttons[B.bid][3] : ''; } return this; }; }; //execute button's click event BUE.buttonClick = function (eindex, bindex) { try { var E = BUE.active = BUE.instances[eindex]; var domB = E.buttons[bindex]; var tplB = E.tpl.buttons[domB.bid]; var content = tplB[1]; E.bindex = bindex; BUE.dialog.close(); if (tplB[4]) {//execute button script. tplB[4](E); } else if (content) {//or insert content var arr = content.split('%TEXT%'); if (arr.length == 2) E.tagSelection(arr[0], arr[1]); else E.replaceSelection(arr.length == 1 ? content : arr.join(E.getSelection()), 'end'); } if (!$(domB).hasClass('stay-clicked')) E.focus(); } catch (e) {alert(e.name +': '+ e.message);} return false; }; //return html of editor template buttons BUE.theme = function (tplid) { if (!BUE.templates[tplid]) return ''; var ET = BUE.templates[tplid]; if (ET.html) return ET.html; ET.html = '