/* Redactor v7.2.0 Updated 20.11.2011 In English http://imperavi.com/ In Russian http://imperavi.ru/ Copyright (c) 2009-2012, Imperavi Ltd. Dual licensed under the MIT or GPL Version 2 licenses. Usage: $('#content').redactor(); */ var isCtrl = false; var redactorActive = false; var $table = false; var $tbody = false; var $thead = false; var $current_tr = false; var $current_td = false; var deviceAndroid = "android"; var uagent = navigator.userAgent.toLowerCase(); // detect iOS and Android function isiOS() { return ((navigator.platform.indexOf("iPhone") != -1) || (navigator.platform.indexOf("iPod") != -1) || (navigator.platform.indexOf("iPad") != -1)); } function detectAndroid() { return uagent.search(deviceAndroid) > -1 } function detectAndroidWebKit() { if (detectAndroid()) { return uagent.search('webkit') > -1 } return false; } // redactor (function($){ // Initialization $.fn.redactor = function(options) { if (isiOS() || detectAndroid() || detectAndroidWebKit()) return false; var obj = new Construct(this, options); obj.init(); return obj; }; // Options and variables function Construct(el, options) { var defaultOptions = { air : false, autosave : false, // false or url interval : 20, // seconds resize : true, visual : true, focus : false, autoclear : true, lang : 'en', toolbar : 'main', removeClasses : false, removeStyles : true, convertLinks : true, autoformat : true, clearOnInit : false, overlay : true, // modal overlay fileUploadCallback : false, // callback function imageUploadCallback : false, // callback function imageInsertCallback : function() {} }; var defaultPaths = { // Paths to various handlers paths : { // Editor css stylesheets : ['/assets/imperavi-rails/imperavi/wym.css'], // Toolbar toolbar : '/imperavi/toolbar/'+defaultOptions.toolbar+'.js', // Interface translations language : '/imperavi/language/'+defaultOptions.lang+'.js', // Typograf typograf : '/imperavi/typograf', // Dialogs // TODO Add dialogs sizes dialogs : { file : '/imperavi/file?r', fileEdit : '/imperavi/file_edit', image : '/imperavi/image?r', imageEdit : '/imperavi/image_edit', link : '/imperavi/link', table : '/imperavi/table', video : '/imperavi/video' }, // Images images : { upload : '/imperavi/images', download : '/imperavi/images/777', list : '/imperavi/images.json' }, // Files files : { upload : '/imperavi/files', download : '/imperavi/files/777', // /tests/file_download.php?file= remove : '/imperavi/files/777' // /tests/file_delete.php?delete= } } }; $.extend(defaultOptions, defaultPaths); this.opts = $.extend(defaultOptions,options); this.$el = $(el); }; // Functionality Construct.prototype = { init: function() { if (this.opts.air) this.opts.toolbar = 'air'; // include lang and toolbar this.include(); // sizes and id this.frameID = this.$el.attr('id'); this.width = this.$el.css('width'); this.height = this.$el.css('height'); // modal overlay if ($('#redactor_imp_modal_overlay').size() == 0) { this.overlay = $(''); $('body').prepend(this.overlay); } // create container this.box = $('
'); // air box if (this.opts.air) { this.air = $(''); } // create iframe this.frame = $(''); this.$el.hide(); // append box and frame $(this.box).insertAfter(this.$el).append(this.frame).append(this.$el); // toolbar if (this.opts.toolbar !== false) this.buildToolbar(); // resizer if (this.opts.resize) { this.resizer = $('
'); $(this.box).append(this.resizer); $(this.resizer).mousedown(function(e) { this.initResize(e) }.bind2(this)); } // enable this.enable(this.$el.val()); $(this.doc).click(function() { this.hideAllDropDown() }.bind2(this)); if (this.opts.autoclear) { $(this.doc).bind('paste', function(e) { setTimeout(function () { this.clean(); }.bind2(this), 200); }.bind2(this)); } // air enable this.enableAir(); // doc events $(this.doc).keydown(function(e) { if (e.ctrlKey || e.metaKey) isCtrl = true; if (e.keyCode == 9) { this.execCommand('indent', false); return false; } if (e.keyCode == 66 && isCtrl) { this.execCommand('bold', 'bold'); return false; } if (e.keyCode == 73 && isCtrl) { this.execCommand('italic', 'italic'); return false; } }.bind2(this)).keyup(function(e) { isCtrl = false; if (e.keyCode == 13) { $(this.doc).linkify(); return true; } this.syncCode(); }.bind2(this)); // autosave if (this.opts.autosave) { setInterval(function() { var html = this.getHtml(); $.post(this.opts.autosave, { data: html }); }.bind2(this), this.opts.interval*1000); } this.formSets(); // focus if (this.opts.focus) this.focus(); }, /* API */ setHtml: function(html) { this.doc.body.innerHTML = html; this.docObserve(); }, getHtml: function() { return this.doc.body.innerHTML; }, getCode: function(clear) { return this.$el.val(); }, focus: function() { if ($.browser.msie) $(this.frame).load(function() { $(this).get(0).contentWindow.focus(); }); else this.frame.get(0).contentWindow.focus(); }, typo: function() { var html = this.getHtml(); $.ajax({ url : this.opts.paths.typograf, type : 'post', data : 'redactor=' + escape(encodeURIComponent(html)), success: function(data) { this.setHtml(data); }.bind2(this) }); }, syncCode: function() { var html = this.getHtml(); html = this.tidyUp(html); html = html.replace(/\%7B/gi, '{'); html = html.replace(/\%7D/gi, '}'); html = html.replace(/
/gi, ''); html = html.replace(/
/gi, ''); this.$el.val(html); }, destroy: function() { var html = this.getCode(); $(this.box).after(this.$el) this.box.remove(); this.$el.val(html).show(); }, // Include include: function() { // lang $('head').append( $('') ); // toolbar // @tanraya if (this.opts.toolbar !== false) { $('head').append($('')); } }, // Enable enable: function(html) { this.doc = this.contentDocumentFrame(this.frame); // flash replace html = html.replace(/\/gi, '

'); if (html == '') { if (this.opts.autoformat === true) { html = $.browser.msie ? "

" : "

 

"; } } this.redactorWrite(this.getRedactorDoc(html)); if (this.opts.clearOnInit) this.clean(); this.designMode(); }, enableAir: function() { if (this.opts.air) { $('#imp_redactor_air_' + this.frameID).hide(); $(this.doc).bind('textselect', this.frameID, function(e) { var width = $('#imp_redactor_air_' + this.frameID).width(); var width_area = $(this.frame).width(); var diff = width_area - e.clientX; if (diff < width) e.clientX = e.clientX - width; $('#imp_redactor_air_' + this.frameID).css({ left: e.clientX + 'px', top: (e.clientY + 8) + 'px' }).show(); }.bind2(this)); $(this.doc).bind('textunselect', this.frameID, function() { $('#imp_redactor_air_' + this.frameID).hide(); }.bind2(this)); } }, redactorWrite: function(html) { this.doc.open(); this.doc.write(html); this.doc.close(); }, getRedactorDoc: function(html) { css = ''; $.each(this.opts.paths.stylesheets, $.proxy(function(i, stylesheet) { css += ''; }, this)); var frameHtml = '\n'; frameHtml += '' + css + ''; frameHtml += html; frameHtml += ''; return frameHtml; }, contentDocumentFrame: function(frame) { frame = frame.get(0); if (frame.contentDocument) return frame.contentDocument; else if (frame.contentWindow && frame.contentWindow.document) return frame.contentWindow.document; else if (frame.document) return frame.document; else return null; }, designMode: function() { if (this.doc) { this.doc.designMode = 'on'; this.frame.load(function() { this.enableObjects(); this.docObserve(); this.doc.designMode = 'on'; }.bind2(this)); } }, enableObjects: function() { if ($.browser.mozilla) { this.doc.execCommand("enableObjectResizing", false, "false"); this.doc.execCommand("enableInlineTableEditing", false, "false"); } }, // Observers docObserve: function() { var body = $(this.doc).find('body'); body.find('img').click(function(e) { this.imageEdit(e); }.bind2(this)); body.find('table').click(function(e) { this.tableObserver(e); }.bind2(this)); body.find('.redactor_file_link').click(function(e) { this.fileEdit(e); }.bind2(this)); }, // Format on submit form formSets: function() { var oldOnsubmit = null; var theForm = $(this.box).parents('form'); if (theForm.length == 0) return false; oldOnsubmit = theForm.get(0).onsubmit; if (typeof theForm.get(0).onsubmit != "function") { theForm.get(0).onsubmit = function() { if (this.opts.visual) { this.syncCode(); return true; } }.bind2(this) } else { theForm.get(0).onsubmit = function() { if (this.opts.visual) { this.syncCode(); return oldOnsubmit(); } }.bind2(this) } return true; }, // Exec execCommand: function(cmd, param) { if (this.opts.visual && this.doc) { try { this.frame.get(0).contentWindow.focus(); if (cmd == 'inserthtml' && $.browser.msie) { this.doc.selection.createRange().pasteHTML(param); } else { this.doc.execCommand(cmd, false, param); if (param == "blockquote" || param == 'pre') this.doc.body.appendChild(this.doc.createElement("BR")); } } catch (e) { } this.syncCode(); if (this.opts.air) $('#imp_redactor_air_' + this.frameID).hide(); } }, /************************************************************************************************************************** * Format and clean******************************************************************************************************** **************************************************************************************************************************/ clean: function() { var html = this.getHtml(); if ($.browser.mozilla) html = this.convertSpan(html); // strip tags html = html.replace(/<(?!\s*\/?(a|br|p|b|i|strong|em|table|tr|td|th|tbody|thead|tfoot|h2|h3|h4)\b)[^>]+>/ig,""); if (this.opts.removeStyles) html = html.replace(/ style=".*?"/g, ''); if (this.opts.removeClasses) html = html.replace(/ class=".*?"/g, ''); html = this.tidyUp(html); this.setHtml(html); this.paragraphise(); return html; }, tidyUp: function (html) { // lowercase if ($.browser.msie) { html = html.replace(/< *(\/ *)?(\w+)/g,function(w){return w.toLowerCase()}); html = html.replace(/ jQuery(.*?)=\"(.*?)\"/gi, ''); } if (this.opts.convertLinks) html = this.convertLinks(html); html = html.replace(/[\t]*/g, ''); html = html.replace(/[\r\n]*/g, ''); html = html.replace(/\n\s*\n/g, "\n"); html = html.replace(/^[\s\n]*/, ''); html = html.replace(/[\s\n]*$/, ''); var lb = '\r\n'; var btags = ["","","","","","","","","","","","","","
","
","","","","","","","","<\!--","","","","","","","",""]; for (i = 0; i < btags.length; ++i) { var bbb = btags[i]; html = html.replace(new RegExp(bbb,'gi'),lb+bbb); } // indenting html = html.replace(/
  • /g, "\t"); // empty tags var btags = ["
    ","
    ","
      ","
        ","
      1. ","
        ","","", "

         

        ", "

        ", "


        ", "
        "]; for (i = 0; i < btags.length; ++i) { var bbb = btags[i]; html = html.replace(new RegExp(bbb,'gi'), ""); } return html; }, convertLinks: function(html) { html = html.replace(/\([\w\W]*?)\<\/a\>/gi, "$4"); html = html.replace(/\http:\/\/([\w\W]*?)\<\/a\>/gi, "rttp:\/\/$4"); //var url1 = /(^|>|\s)(www\..+?\..+?)(\s|<|$)/g; var url2 = /(^|>|\s)(((https?|ftp):\/\/|mailto:).+?)(\s|<|$)/g; //html = html.replace(url1, '$1$2$3') html = html.replace(url2, '$1$2$5'); html = html.replace(/\([\w\W]*?)\<\/a\>/gi, "$4"); html = html.replace(/\rttp:\/\/([\w\W]*?)\<\/a\>/gi, "http://$4"); return html; }, convertSpan: function(html) { html = html.replace(/\([\w\W]*?)\<\/span\>/gi, "$2"); html = html.replace(/\([\w\W]*?)\<\/span\>/gi, "$2"); html = html.replace(/\([\w\W]*?)\<\/span\>/gi, "$2"); html = html.replace(/\([\w\W]*?)\<\/span\>/gi, "$2"); return html; }, /* Paragraphise */ paragraphise: function() { if (this.opts.autoformat === false) return true; if (this.opts.visual) { var theBody = this.doc.body; /* Remove all text nodes containing just whitespace */ for (var i = 0; i < theBody.childNodes.length; i++) { if (theBody.childNodes[i].nodeName.toLowerCase() == "#text" && theBody.childNodes[i].data.search(/^\s*$/) != -1) { theBody.removeChild(theBody.childNodes[i]); i--; } } var removedElements = new Array(); for (var i = 0; i < theBody.childNodes.length; i++) { if (theBody.childNodes[i].nodeName.isInlineName()) { removedElements.push(theBody.childNodes[i].cloneNode(true)); theBody.removeChild(theBody.childNodes[i]); i--; } else if (theBody.childNodes[i].nodeName.toLowerCase() == "br") { if (i + 1 < theBody.childNodes.length) { if (theBody.childNodes[i + 1].nodeName.toLowerCase() == "br") { while (i < theBody.childNodes.length && theBody.childNodes[i].nodeName.toLowerCase() == "br") { theBody.removeChild(theBody.childNodes[i]); } if (removedElements.length > 0) { this.insertNewParagraph(removedElements, theBody.childNodes[i]); removedElements = new Array(); } } else if (!theBody.childNodes[i + 1].nodeName.isInlineName()) theBody.removeChild(theBody.childNodes[i]); else if (removedElements.length > 0) { removedElements.push(theBody.childNodes[i].cloneNode(true)); theBody.removeChild(theBody.childNodes[i]); } else theBody.removeChild(theBody.childNodes[i]); i--; } else theBody.removeChild(theBody.childNodes[i]); } else if (removedElements.length > 0) { this.insertNewParagraph(removedElements, theBody.childNodes[i]); removedElements = new Array(); } } if (removedElements.length > 0) this.insertNewParagraph(removedElements); } return true; }, insertNewParagraph: function(elementArray, succeedingElement) { var theBody = this.doc.getElementsByTagName("body")[0]; var theParagraph = this.doc.createElement("p"); for (var i = 0; i < elementArray.length; i++) theParagraph.appendChild(elementArray[i]); if (typeof(succeedingElement) != "undefined") theBody.insertBefore(theParagraph, succeedingElement); else theBody.appendChild(theParagraph); return true; }, /* Selection */ get_selection: function () { if (this.frame.get(0).contentWindow.getSelection) return this.frame.get(0).contentWindow.getSelection(); else if (this.frame.get(0).contentWindow.document.selection) return this.frame.contentWindow.get(0).document.selection.createRange(); }, setCut: function() { this.execCommand('inserthtml', '
        '); }, /************************************************************************************************************************** * Toggle ***************************************************************************************************************** **************************************************************************************************************************/ toggle: function() { if (this.opts.visual) { this.addSelButton('html'); var html = this.getHtml(); html = this.tidyUp(html); html = html.replace(/\%7B/gi, '{'); html = html.replace(/\%7D/gi, '}'); // flash replace html = html.replace(/([\w\W]*?)\<\/p>/gi, "$3"); // files replace html = html.replace(/([\w\W]*?)\<\/a>/gi, "$6"); // cut replace html = html.replace(/
        /gi, ''); html = html.replace(/
        /gi, ''); this.frame.hide(); this.$el.val(html); this.$el.show().focus(); var height = this.$el.height(); this.opts.visual = false; } else { this.removeSelButton('html'); this.$el.hide(); var html = this.$el.val(); // cut replace html = html.replace(//gi, '
        '); // flash replace html = html.replace(/\/gi, '

        '); // files replace html = html.replace(/(.*?)<\/a>/gi, "$7"); this.opts.visual = true; this.setHtml(html); this.frame.show(); this.focus(); } }, /************************************************************************************************************************** * Video ****************************************************************************************************************** **************************************************************************************************************************/ showVideo: function() { redactorActive = this; this.modalInit(RLANG.video, this.opts.paths.dialogs.video, 600, 360, function() { $('#redactor_insert_video_area').focus(); }); }, insertVideo: function() { var data = $('#redactor_insert_video_area').val(); if (redactorActive.opts.visual) { // iframe video if (data.search('iframe')) {} // flash else data = '

        ' + data + '

        '; } redactorActive.execCommand('inserthtml', data); this.modalClose(); }, /************************************************************************************************************************** * File ******************************************************************************************************************* **************************************************************************************************************************/ showFile: function() { redactorActive = this; var handler = function() { // upload params var params = ''; if (this.opts.fileUploadCallback) params = this.opts.fileUploadCallback(); $('#redactor_file').dragupload( { url: this.opts.paths.files.upload + params, success: function(data) { this.fileUploadCallback(data); }.bind2(this) }); this.uploadInit('redactor_file', { auto: true, url: this.opts.paths.files.upload + params, success: function(data) { this.fileUploadCallback(data); }.bind2(this) }); }.bind2(this); redactorActive = this; this.modalInit(RLANG.file, this.opts.paths.dialogs.file, 500, 400, handler); }, fileUploadCallback: function(data) { redactorActive.frame.get(0).contentWindow.focus(); redactorActive.execCommand('inserthtml', data); this.modalClose(); this.docObserve(); }, fileEdit: function(e) { var el = e.target; var file_id = $(el).attr('rel'); var handler = function() { $('#file').val($(el).text()); $('#redactorFileDeleteBtn').click(function() { this.fileDelete(el, file_id); }.bind2(this)); $('#redactorFileDownloadBtn').click(function() { this.fileDownload(el, file_id); }.bind2(this)); }.bind2(this); redactorActive = this; this.modalInit(RLANG.file, this.opts.paths.dialogs.fileEdit, 400, 200, handler); }, fileDelete: function(el, file_id) { $(el).remove(); $.get(this.opts.paths.files.remove + file_id); redactorActive.frame.get(0).contentWindow.focus(); this.modalClose(); }, fileDownload: function(el, file_id) { top.location.href = this.opts.paths.files.download + file_id; }, /************************************************************************************************************************** * Table ****************************************************************************************************************** **************************************************************************************************************************/ showTable: function() { redactorActive = this; this.modalInit(RLANG.table, this.opts.paths.dialogs.table, 360, 200); }, insertTable: function() { var rows = $('#redactor_table_rows').val(); var columns = $('#redactor_table_columns').val(); var table_box = $('
        '); var tableid = Math.floor(Math.random() * 99999); var table = $('
        '); for (i = 0; i < rows; i++) { var row = $('') for (z = 0; z < columns; z++) { var column = $(' '); $(row).append(column); } $(table).append(row); } $(table_box).append(table); var html = $(table_box).html(); if ($.browser.msie) html += '

        '; else html += '

         

        '; redactorActive.execCommand('inserthtml', html); this.enableObjects(); this.docObserve(); this.modalClose(); $table = $(this.doc).find('body').find('#table' + tableid); }, tableObserver: function(e) { $table = $(e.target).parents('table'); $tbody = $(e.target).parents('tbody'); $thead = $($table).find('thead'); $current_td = $(e.target); $current_tr = $(e.target).parents('tr'); }, deleteTable: function() { $($table).remove(); $table = false; }, deleteRow: function() { $($current_tr).remove(); }, deleteColumn: function() { var index = $($current_td).attr('cellIndex'); $($table).find('tr').each(function() { $(this).find('td').eq(index).remove(); }); }, addHead: function() { if ($($table).find('thead').size() != 0) this.deleteHead(); else { var tr = $($table).find('tr').first().clone(); tr.find('td').html(' '); $thead = $(''); $thead.append(tr); $($table).prepend($thead); } }, deleteHead: function() { $($thead).remove(); $thead = false; }, insertRowAbove: function() { this.insertRow('before'); }, insertRowBelow: function() { this.insertRow('after'); }, insertColumnLeft: function() { this.insertColumn('before'); }, insertColumnRight: function() { this.insertColumn('after'); }, insertRow: function(type) { var new_tr = $($current_tr).clone(); new_tr.find('td').html(' '); if (type == 'after') $($current_tr).after(new_tr); else $($current_tr).before(new_tr); }, insertColumn: function(type) { var index = $($current_td).attr('cellIndex'); $($table).find('tr').each(function(i,s) { var current = $(s).find('td').eq(index); var td = current.clone(); td.html(' '); if (type == 'after') $(current).after(td); else $(current).before(td); }); }, /************************************************************************************************************************** * Image ****************************************************************************************************************** **************************************************************************************************************************/ imageEdit: function(e) { var handler = function() { var $el = $(e.target); var align_box = $('#redactor_form_image_align'); var alt = $('#redactor_file_alt'); // Download image $('#redactor_image_edit_src').attr('href', $el.attr('src')); // Remove image $('#redactor_image_edit_delete').click(function() { $el.remove(); this.modalClose(); }.bind2(this)); // Save images $('#redactorSaveBtn').click(function() { $el.attr('alt', alt.val()) .removeClass('img_left img_right img_none') .addClass('img_' + align_box.val()); this.modalClose(); }.bind2(this)); // Populate inputs alt.val($el.attr('alt')); align_box.val($el.css('float')); }.bind2(this); redactorActive = this; this.modalInit(RLANG.image, this.opts.paths.dialogs.imageEdit, 380, 290, handler); }, showImage: function() { this.spanid = Math.floor(Math.random() * 99999); if (jQuery.browser.msie) { this.execCommand('inserthtml', ''); } var handler = function() { if (this.opts.paths.images.list !== false) { $.getJSON(this.opts.paths.images.list, function(data) { $.each(data, function(key, val) { var img = $(''); img.click(function() { redactorActive.imageSetThumb($(this).attr('rel')); }); $('#redactor_image_box').append(img); }); }); } else { // 'pretty' code $('#redactor_tabs li').eq(0).remove(); $('#redactor_tabs a').eq(1).addClass('redactor_tabs_act'); $('#redactor_tabs1').hide(); $('#redactor_tabs2').show(); } // upload params $('#redactor_file').dragupload({ url : this.opts.paths.images.upload, success : function(data) { this.imageUploadCallback(data); }.bind2(this) }); this.uploadInit('redactor_file', { auto : true, url : this.opts.paths.images.upload, trigger : 'redactorUploadBtn', success : function(data) { this.imageUploadCallback(data); }.bind2(this)}); }.bind2(this); redactorActive = this; this.modalInit(RLANG.image, this.opts.paths.dialogs.image, 750, 600, handler); }, imageSetThumb: function(data) { this._imageSet(''); }, imageUploadCallback: function(data) { if ($('#redactor_file_link').val() != '') data = $('#redactor_file_link').val(); this._imageSet(data); }, _imageSet: function(html) { redactorActive.frame.get(0).contentWindow.focus(); if ($.browser.msie) { $(redactorActive.doc.getElementById('span' + redactorActive.spanid)).after(html); $(redactorActive.doc.getElementById('span' + redactorActive.spanid)).remove(); } else { redactorActive.execCommand('inserthtml', html); } this.modalClose(); this.docObserve(); }, /************************************************************************************************************************** * Link ****************************************************************************************************************** **************************************************************************************************************************/ showLink: function() { redactorActive = this; var handler = function() { var sel = this.get_selection(); if ($.browser.msie) { var temp = sel.htmlText.match(/href="(.*?)"/gi); if (temp != null) { temp = new String(temp); temp = temp.replace(/href="(.*?)"/gi, '$1'); } var text = sel.text; if (temp != null) var url = temp; else var url = ''; var title = ''; } else { if (sel.anchorNode.parentNode.tagName == 'A') { var url = sel.anchorNode.parentNode.href; var text = sel.anchorNode.parentNode.text; var title = sel.anchorNode.parentNode.title; if (sel.toString() == '') this.insert_link_node = sel.anchorNode.parentNode } else { var url = ''; var text = sel.toString(); var title = ''; } } $('#redactor_link_url').val(url).focus(); $('#redactor_link_text').val(text); $('#redactor_link_title').val(title); }.bind2(this); this.modalInit(RLANG.link, this.opts.paths.dialogs.link, 400, 300, handler); }, insertLink: function() { var value = $('#redactor_link_text').val(); if (value == '') return true; var title = $('#redactor_link_title').val(); if (title != '') title = ' title="' + $('#redactor_link_title').val() + '"'; // Email link? var mailto = $('#redactor_link_id_url').get(0).checked ? '' : 'mailto:'; var a = '' + value + ' '; if (this.insert_link_node) { $(this.insert_link_node).text(value); $(this.insert_link_node).attr('href', $('#redactor_link_url').val()); var title = $('#redactor_link_title').val(); if (title != '') $(this.insert_link_node).attr('title', title); this.modalClose(); } else { redactorActive.frame.get(0).contentWindow.focus(); redactorActive.execCommand('inserthtml', a); } this.modalClose(); }, /************************************************************************************************************************** * Modal ****************************************************************************************************************** **************************************************************************************************************************/ modalInit: function(title, url, width, height, handler, scroll) { if (this.opts.overlay) { $('#redactor_imp_modal_overlay') .show() .click(function() { this.modalClose(); }.bind2(this)); } if ($('#redactor_imp_modal').size() == 0) { this.modal = $(''); $('body').append(this.modal); } $('#redactor_imp_modal_close').click(function() { this.modalClose(); }.bind2(this)); var escClose = function(e) { if ( e.keyCode == 27) this.modalClose(); }.bind2(this) $(document).keyup(escClose); $(this.doc).keyup(escClose); $.ajax({ url: url, success: function(data) { // parse lang $.each(RLANG, function(i,s) { var re = new RegExp("%RLANG\." + i + "%","gi"); data = data.replace(re, s); }); $('#redactor_imp_modal_inner').html(data); $('#redactor_imp_modal_header').html(title); $('#redactor_imp_modal').css({ width : width + 'px', height : height ? height + 'px' : 'auto', marginTop : '-' + height/2 + 'px', marginLeft : '-' + width/2 + 'px' }).fadeIn('fast'); if (scroll === true) { $('#imp_redactor_table_box') .height(height-$('#redactor_imp_modal_header').outerHeight()-130) .css('overflow', 'auto'); } if (typeof(handler) == 'function') handler(); }.bind2(this) }); }, modalClose: function() { $('#redactor_imp_modal_close').unbind('click', function() { this.modalClose(); }.bind2(this)); $('#redactor_imp_modal').fadeOut('fast', function() { $('#redactor_imp_modal_inner').html(''); if (this.opts.overlay) { $('#redactor_imp_modal_overlay') .hide() .unbind('click', function() { this.modalClose(); }.bind2(this)); } var escClose = function(e) { if ( e.keyCode == 27) this.modalClose(); }.bind2(this) $(document).unbind('keyup', escClose); $(this.doc).unbind('keyup', escClose); }.bind2(this)); }, /************************************************************************************************************************** * Upload ***************************************************************************************************************** **************************************************************************************************************************/ uploadInit: function(element, options) { /* Options */ this.uploadOptions = { url : false, success : false, start : false, trigger : false, auto : false, input : false }; $.extend(this.uploadOptions, options); // Test input or form if ($('#' + element).get(0).tagName == 'INPUT') { this.uploadOptions.input = $('#' + element); this.element = $($('#' + element).get(0).form); } else { this.element = $('#' + element); } this.element_action = this.element.attr('action'); // Auto or trigger if (this.uploadOptions.auto) { $(this.uploadOptions.input).change(function() { this.element.submit(function(e) { return false; }); this.uploadSubmit(); }.bind2(this)); } else if (this.uploadOptions.trigger) { $('#' + this.uploadOptions.trigger).click(function() { this.uploadSubmit(); }.bind2(this)); } }, uploadSubmit : function() { this.uploadForm(this.element, this.uploadFrame()); }, uploadFrame : function() { this.id = 'f' + Math.floor(Math.random() * 99999); var d = document.createElement('div'); var iframe = ''; d.innerHTML = iframe; document.body.appendChild(d); // Start if (this.uploadOptions.start) this.uploadOptions.start(); $('#' + this.id).load(function () { this.uploadLoaded() }.bind2(this)); return this.id; }, uploadForm : function(f, name) { if (this.uploadOptions.input) { var formId = 'redactorUploadForm' + this.id; var fileId = 'redactorUploadFile' + this.id; this.form = $('
        '); var oldElement = this.uploadOptions.input; var newElement = $(oldElement).clone(); $(oldElement).attr('id', fileId) .before(newElement) .appendTo(this.form); $(this.form).css('position', 'absolute') .css('top', '-2000px') .css('left', '-2000px') .appendTo('body'); this.form.submit(); } else { f.attr('target', name) .attr('method', 'POST') .attr('enctype', 'multipart/form-data') .attr('action', this.uploadOptions.url); this.element.submit(); } }, uploadLoaded : function() { var i = $('#' + this.id), d; if (i.contentDocument) d = i.contentDocument; else if (i.contentWindow) d = i.contentWindow.document; else d = window.frames[this.id].document; if (d.location.href == "about:blank") return true; // Success if (this.uploadOptions.success) this.uploadOptions.success(d.body.innerHTML); this.element.attr('action', this.element_action) .attr('target', ''); }, /************************************************************************************************************************** * Toolbar ***************************************************************************************************************** **************************************************************************************************************************/ buildToolbar: function() { this.toolbar = $('
          '); if (this.opts.air) { $(this.air).append(this.toolbar); this.box.prepend(this.air); } else $(this.box).prepend(this.toolbar); $.each(RTOOLBAR, function (i, s) { if (s.name == 'separator') { var li = $('
        • '); $(this.toolbar).append(li); } else { var a = $(''); if (typeof(s.func) == 'undefined') a.click(function() { this.execCommand(s.exec, s.name); }.bind2(this)); else if (s.func != 'show') a.click(function(e) { this[s.func](e); }.bind2(this)); var li = $('
        • '); $(li).append(a); $(this.toolbar).append(li); // build dropdown box if (s.name == 'backcolor' || s.name == 'fontcolor' || typeof(s.dropdown) != 'undefined') { var ul = $(''); if ($.browser.msie) ul.css({ borderLeft: '1px solid #ddd', borderRight: '1px solid #ddd', borderBottom: '1px solid #ddd' }); } // build dropdown if (typeof(s.dropdown) != 'undefined') { $.each(s.dropdown, function (x, d) { if (typeof(d.style) == 'undefined') d.style = ''; if (d.name == 'separator') { var ul_li = $('
        • '); $(ul).append(ul_li); } else { var ul_li = $('
        • '); var ul_li_a = $('' + d.title + ''); $(ul_li).append(ul_li_a); $(ul).append(ul_li); if (typeof(d.func) == 'undefined') $(ul_li_a).click(function() { this.execCommand(d.exec, d.name); }.bind2(this)); else $(ul_li_a).click(function(e) { this[d.func](e); }.bind2(this)); } }.bind2(this) ); } else { a.mouseover(function() { this.hideAllDropDown() }.bind2(this)); } // observing dropdown if (s.name == 'backcolor' || s.name == 'fontcolor' || typeof(s.dropdown) != 'undefined') { $('#imp_redactor_toolbar_' + this.frameID).after(ul); this.hdlHideDropDown = function(e) { this.hideDropDown(e, ul, s.name) }.bind2(this); this.hdlShowDropDown = function(e) { this.showDropDown(e, ul, s.name) }.bind2(this); this.hdlShowerDropDown = function(e) { this.showerDropDown(e, ul, s.name) }.bind2(this); a.click(this.hdlShowDropDown).mouseover(this.hdlShowerDropDown); $(document).click(this.hdlHideDropDown); } } }.bind2(this) ); }, /************************************************************************************************************************** * DropDown *************************************************************************************************************** **************************************************************************************************************************/ showedDropDown: false, showDropDown: function(e, ul, name) { if (this.showedDropDown) { this.hideAllDropDown(); } else { this.showedDropDown = true; this.showingDropDown(e, ul, name); } }, showingDropDown: function(e, ul, name) { this.hideAllDropDown(); this.addSelButton(name); var left = $('#imp_redactor_toolbar_' + this.frameID + ' li.imp_li_btn_' + name).position().left; $(ul).css('left', left + 'px').show(); }, showerDropDown: function(e, ul, name) { if (this.showedDropDown) this.showingDropDown(e, ul, name); }, hideAllDropDown: function() { $('#imp_redactor_toolbar_' + this.frameID + ' li.imp_li_btn').removeClass('act'); $('ul.imp_redactor_drop_down' + this.frameID).hide(); }, hideDropDown: function(e, ul, name) { if (!$(e.target).parent().hasClass('act')) { this.showedDropDown = false; this.hideAllDropDown(); } $(document).unbind('click', this.hdlHideDropDown); $(this.doc).unbind('click', this.hdlHideDropDown); }, addSelButton: function(name) { var element = $('#imp_redactor_toolbar_' + this.frameID + ' li.imp_li_btn_' + name); element.addClass('act'); }, removeSelButton: function(name) { var element = $('#imp_redactor_toolbar_' + this.frameID + ' li.imp_li_btn_' + name); element.removeClass('act'); }, toggleSelButton: function(name) { $('#imp_redactor_toolbar_' + this.frameID + ' li.imp_li_btn_' + name).toggleClass('act'); }, /************************************************************************************************************************** * Resizer *************************************************************************************************************** **************************************************************************************************************************/ initResize: function(e) { if (e.preventDefault) e.preventDefault(); else e.returnValue = false; this.splitter = e.target; if (this.opts.visual) { this.element_resize = this.frame; this.element_resize.get(0).style.visibility = 'hidden'; this.element_resize_parent = this.$el; } else { this.element_resize = this.$el; this.element_resize_parent = this.frame; } this.stopResizeHdl = function (e) { this.stopResize(e) }.bind2(this); this.startResizeHdl = function (e) { this.startResize(e) }.bind2(this); this.resizeHdl = function (e) { this.resize(e) }.bind2(this); $(document).mousedown(this.startResizeHdl); $(document).mouseup(this.stopResizeHdl); $(this.splitter).mouseup(this.stopResizeHdl); this.null_point = false; this.h_new = false; this.h = this.element_resize.height(); }, startResize: function() { $(document).mousemove(this.resizeHdl); }, resize: function(e) { if (e.preventDefault) e.preventDefault(); else e.returnValue = false; var y = e.pageY; if (this.null_point == false) this.null_point = y; if (this.h_new == false) this.h_new = this.element_resize.height(); var s_new = (this.h_new + y - this.null_point) - 10; if (s_new <= 30) return true; if (s_new >= 0) { this.element_resize.get(0).style.height = s_new + 'px'; this.element_resize_parent.get(0).style.height = s_new + 'px'; } }, stopResize: function(e) { $(document).unbind('mousemove', this.resizeHdl); $(document).unbind('mousedown', this.startResizeHdl); $(document).unbind('mouseup', this.stopResizeHdl); $(this.splitter).unbind('mouseup', this.stopResizeHdl); this.element_resize.get(0).style.visibility = 'visible'; } }; String.prototype.isInlineName = function() { var inlineList = new Array("#text", "a", "em", "font", "span", "strong", "u"); var theName = this.toLowerCase(); for (var i = 0; i < inlineList.length; i++) { if (theName == inlineList[i]) return true } return false; }; // bind2 Function.prototype.bind2 = function(object) { var method = this; var oldArguments = $.makeArray(arguments).slice(1); return function (argument) { if (argument == new Object) { method = null; oldArguments = null; } else if (method == null) throw "Attempt to invoke destructed method reference."; else { var newArguments = $.makeArray(arguments); return method.apply(object, oldArguments.concat(newArguments)); } }; }; })(jQuery); // redactor_tabs function showRedactorTabs(el, index) { $('#redactor_tabs a').removeClass('redactor_tabs_act'); $(el).addClass('redactor_tabs_act'); $('.redactor_tabs').hide(); $('#redactor_tabs' + index).show(); }