X-Git-Url: https://code.citadel.org/?a=blobdiff_plain;f=webcit%2Ftiny_mce%2Fplugins%2Ffullpage%2Feditor_plugin_src.js;h=23de7c5a1a258cfbe1358155fab84db3b3ca8caf;hb=b05f4eef4014db3885787ef15107cba93c932ac9;hp=8c618235591cf2d44958eb2aed6fe0d3a4bb64b1;hpb=932f4feaa7b2afe4553e5e669e2312614d3f6140;p=citadel.git diff --git a/webcit/tiny_mce/plugins/fullpage/editor_plugin_src.js b/webcit/tiny_mce/plugins/fullpage/editor_plugin_src.js old mode 100755 new mode 100644 index 8c6182355..23de7c5a1 --- a/webcit/tiny_mce/plugins/fullpage/editor_plugin_src.js +++ b/webcit/tiny_mce/plugins/fullpage/editor_plugin_src.js @@ -1,235 +1,405 @@ /** - * $Id: editor_plugin_src.js 232 2007-03-05 17:00:27Z spocke $ + * editor_plugin_src.js * - * @author Moxiecode - * @copyright Copyright © 2004-2007, Moxiecode Systems AB, All rights reserved. + * Copyright 2009, Moxiecode Systems AB + * Released under LGPL License. + * + * License: http://tinymce.moxiecode.com/license + * Contributing: http://tinymce.moxiecode.com/contributing */ -/* Import plugin specific language pack */ -tinyMCE.importPluginLanguagePack('fullpage'); - -var TinyMCE_FullPagePlugin = { - getInfo : function() { - return { - longname : 'Fullpage', - author : 'Moxiecode Systems AB', - authorurl : 'http://tinymce.moxiecode.com', - infourl : 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/fullpage', - version : tinyMCE.majorVersion + "." + tinyMCE.minorVersion - }; - }, - - getControlHTML : function(cn) { - switch (cn) { - case "fullpage": - return tinyMCE.getButtonHTML(cn, 'lang_fullpage_desc', '{$pluginurl}/images/fullpage.gif', 'mceFullPageProperties'); - } +(function() { + var each = tinymce.each, Node = tinymce.html.Node; + + tinymce.create('tinymce.plugins.FullPagePlugin', { + init : function(ed, url) { + var t = this; + + t.editor = ed; + + // Register commands + ed.addCommand('mceFullPageProperties', function() { + ed.windowManager.open({ + file : url + '/fullpage.htm', + width : 430 + parseInt(ed.getLang('fullpage.delta_width', 0)), + height : 495 + parseInt(ed.getLang('fullpage.delta_height', 0)), + inline : 1 + }, { + plugin_url : url, + data : t._htmlToData() + }); + }); + + // Register buttons + ed.addButton('fullpage', {title : 'fullpage.desc', cmd : 'mceFullPageProperties'}); + + ed.onBeforeSetContent.add(t._setContent, t); + ed.onGetContent.add(t._getContent, t); + }, + + getInfo : function() { + return { + longname : 'Fullpage', + author : 'Moxiecode Systems AB', + authorurl : 'http://tinymce.moxiecode.com', + infourl : 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/fullpage', + version : tinymce.majorVersion + "." + tinymce.minorVersion + }; + }, + + // Private plugin internal methods + + _htmlToData : function() { + var headerFragment = this._parseHeader(), data = {}, nodes, elm, matches, editor = this.editor; + + function getAttr(elm, name) { + var value = elm.attr(name); + + return value || ''; + }; + + // Default some values + data.fontface = editor.getParam("fullpage_default_fontface", ""); + data.fontsize = editor.getParam("fullpage_default_fontsize", ""); + + // Parse XML PI + elm = headerFragment.firstChild; + if (elm.type == 7) { + data.xml_pi = true; + matches = /encoding="([^"]+)"/.exec(elm.value); + if (matches) + data.docencoding = matches[1]; + } - return ""; - }, - - execCommand : function(editor_id, element, command, user_interface, value) { - // Handle commands - switch (command) { - case "mceFullPageProperties": - var template = new Array(); - - template['file'] = '../../plugins/fullpage/fullpage.htm'; - template['width'] = 430; - template['height'] = 485 + (tinyMCE.isOpera ? 5 : 0); - - template['width'] += tinyMCE.getLang('lang_fullpage_delta_width', 0); - template['height'] += tinyMCE.getLang('lang_fullpage_delta_height', 0); - - tinyMCE.openWindow(template, {editor_id : editor_id, inline : "yes"}); - return true; - - case "mceFullPageUpdate": - TinyMCE_FullPagePlugin._addToHead(tinyMCE.getInstanceById(editor_id)); - return true; - } - - // Pass to next handler in chain - return false; - }, - - cleanup : function(type, content, inst) { - switch (type) { - case "insert_to_editor": - var tmp = content.toLowerCase(); - var pos = tmp.indexOf('', pos); - pos2 = tmp.lastIndexOf(''); - inst.fullpageTopContent = content.substring(0, pos + 1); - content = content.substring(pos + 1, pos2); - // tinyMCE.debug(inst.fullpageTopContent, content); - } else { - if (!inst.fullpageTopContent) { - var docType = tinyMCE.getParam("fullpage_default_doctype", ''); - var enc = tinyMCE.getParam("fullpage_default_encoding", 'utf-8'); - var title = tinyMCE.getParam("fullpage_default_title", 'Untitled document'); - var lang = tinyMCE.getParam("fullpage_default_langcode", 'en'); - var pi = tinyMCE.getParam("fullpage_default_xml_pi", true); - var ff = tinyMCE.getParam("fullpage_default_font_family", ""); - var fz = tinyMCE.getParam("fullpage_default_font_size", ""); - var ds = tinyMCE.getParam("fullpage_default_style", ""); - var dtc = tinyMCE.getParam("fullpage_default_text_color", ""); - - // Xml encode it - title = title.replace(/&/g, '&'); - title = title.replace(/\"/g, '"'); - title = title.replace(//g, '>'); - - tmp = ''; - - // Make default chunk - if (pi) - tmp += '\n'; - - tmp += docType + '\n'; - tmp += '\n'; - tmp += '\n'; - tmp += '\t' + title + '\n'; - tmp += '\t\n'; - tmp += '\n'; - tmp += '"; + + // Parse title element + elm = headerFragment.getAll('title')[0]; + if (elm && elm.firstChild) { + data.metatitle = elm.firstChild.value; + } + + // Parse meta elements + each(headerFragment.getAll('meta'), function(meta) { + var name = meta.attr('name'), httpEquiv = meta.attr('http-equiv'), matches; + + if (name) + data['meta' + name.toLowerCase()] = meta.attr('content'); + else if (httpEquiv == "Content-Type") { + matches = /charset\s*=\s*(.*)\s*/gi.exec(meta.attr('content')); + + if (matches) + data.docencoding = matches[1]; + } + }); + + // Parse html attribs + elm = headerFragment.getAll('html')[0]; + if (elm) + data.langcode = getAttr(elm, 'lang') || getAttr(elm, 'xml:lang'); + + // Parse stylesheet + elm = headerFragment.getAll('link')[0]; + if (elm && elm.attr('rel') == 'stylesheet') + data.stylesheet = elm.attr('href'); + + // Parse body parts + elm = headerFragment.getAll('body')[0]; + if (elm) { + data.langdir = getAttr(elm, 'dir'); + data.style = getAttr(elm, 'style'); + data.visited_color = getAttr(elm, 'vlink'); + data.link_color = getAttr(elm, 'link'); + data.active_color = getAttr(elm, 'alink'); + } + + return data; + }, + + _dataToHtml : function(data) { + var headerFragment, headElement, html, elm, value, dom = this.editor.dom; + + function setAttr(elm, name, value) { + elm.attr(name, value ? value : undefined); + }; + + function addHeadNode(node) { + if (headElement.firstChild) + headElement.insert(node, headElement.firstChild); + else + headElement.append(node); + }; + + headerFragment = this._parseHeader(); + headElement = headerFragment.getAll('head')[0]; + if (!headElement) { + elm = headerFragment.getAll('html')[0]; + headElement = new Node('head', 1); + + if (elm.firstChild) + elm.insert(headElement, elm.firstChild, true); + else + elm.append(headElement); + } + + // Add/update/remove XML-PI + elm = headerFragment.firstChild; + if (data.xml_pi) { + value = 'version="1.0"'; + + if (data.docencoding) + value += ' encoding="' + data.docencoding + '"'; + + if (elm.type != 7) { + elm = new Node('xml', 7); + headerFragment.insert(elm, headerFragment.firstChild, true); } - this._addToHead(inst); + elm.value = value; + } else if (elm && elm.type == 7) + elm.remove(); - break; + // Add/update/remove doctype + elm = headerFragment.getAll('#doctype')[0]; + if (data.doctype) { + if (!elm) { + elm = new Node('#doctype', 10); - case "get_from_editor": - if (inst.fullpageTopContent && !tinyMCE.getParam("fullpage_hide_in_source_view", false)) { - content = content.replace(/(\s)?mce\_[a-z_]+\=[^\s>]+(\s|\>)/i, ''); // Remove internal stuff - content = inst.fullpageTopContent + content + "\n\n"; + if (data.xml_pi) + headerFragment.insert(elm, headerFragment.firstChild); + else + addHeadNode(elm); } - break; + elm.value = data.doctype.substring(9, data.doctype.length - 1); + } else if (elm) + elm.remove(); + + // Add/update/remove title + elm = headerFragment.getAll('title')[0]; + if (data.metatitle) { + if (!elm) { + elm = new Node('title', 1); + elm.append(new Node('#text', 3)).value = data.metatitle; + addHeadNode(elm); + } + } - case "submit_content": - if (inst.fullpageTopContent && tinyMCE.getParam("fullpage_hide_in_source_view", false)) - content = inst.fullpageTopContent + content + "\n\n"; + // Add meta encoding + if (data.docencoding) { + elm = null; + each(headerFragment.getAll('meta'), function(meta) { + if (meta.attr('http-equiv') == 'Content-Type') + elm = meta; + }); + + if (!elm) { + elm = new Node('meta', 1); + elm.attr('http-equiv', 'Content-Type'); + elm.shortEnded = true; + addHeadNode(elm); + } - break; - } + elm.attr('content', 'text/html; charset=' + data.docencoding); + } - // Pass through to next handler in chain - return content; - }, - - // Private plugin internal methods - - _addToHead : function(inst) { - var doc = inst.getDoc(); - var head = doc.getElementsByTagName("head")[0]; - var body = doc.body; - var h = inst.fullpageTopContent; - var e = doc.createElement("body"); - var nl, i, le, tmp; - - // Remove stuff we don't want - h = h.replace(/(\r|\n)/gi, ''); - h = h.replace(/<\?[^\>]*\>/gi, ''); - h = h.replace(/<\/?(!DOCTYPE|head|html)[^\>]*\>/gi, ''); - h = h.replace(//gi, ''); - h = h.replace(//gi, ''); - h = h.replace(/<(meta|base)[^>]*>/gi, ''); - - // Make link and style elements into pre - h = h.replace(/]*)\/>/gi, '
');
-		//h = h.replace(/]*)>(.*?)<\/style>/gi, '
$2
'); - - // Make body a div - h = h.replace(/ 0) { - body.style.cssText = tinyMCE.getAttrib(nl[0], 'style'); + // Update body attributes + elm = headerFragment.getAll('body')[0]; + if (elm) { + setAttr(elm, 'dir', data.langdir); + setAttr(elm, 'style', data.style); + setAttr(elm, 'vlink', data.visited_color); + setAttr(elm, 'link', data.link_color); + setAttr(elm, 'alink', data.active_color); + + // Update iframe body as well + dom.setAttribs(this.editor.getBody(), { + style : data.style, + dir : data.dir, + vLink : data.visited_color, + link : data.link_color, + aLink : data.active_color + }); + } + + // Set html attributes + elm = headerFragment.getAll('html')[0]; + if (elm) { + setAttr(elm, 'lang', data.langcode); + setAttr(elm, 'xml:lang', data.langcode); + } + + // Serialize header fragment and crop away body part + html = new tinymce.html.Serializer({ + validate: false, + indent: true, + apply_source_formatting : true, + indent_before: 'head,html,body,meta,title,script,link,style', + indent_after: 'head,html,body,meta,title,script,link,style' + }).serialize(headerFragment); + + this.head = html.substring(0, html.indexOf('')); + }, + + _parseHeader : function() { + // Parse the contents with a DOM parser + return new tinymce.html.DomParser({ + validate: false, + root_name: '#document' + }).parse(this.head); + }, + + _setContent : function(ed, o) { + var self = this, startPos, endPos, content = o.content, headerFragment, styles = '', dom = self.editor.dom, elm; + + function low(s) { + return s.replace(/<\/?[A-Z]+/g, function(a) { + return a.toLowerCase(); + }) + }; + + // Ignore raw updated if we already have a head, this will fix issues with undo/redo keeping the head/foot separate + if (o.format == 'raw' && self.head) + return; + + if (o.source_view && ed.getParam('fullpage_hide_in_source_view')) + return; + + // Parse out head, body and footer + content = content.replace(/<(\/?)BODY/gi, '<$1body'); + startPos = content.indexOf('', startPos); + self.head = low(content.substring(0, startPos + 1)); + + endPos = content.indexOf('\n'; + + header += editor.getParam('fullpage_default_doctype', ''); + header += '\n\n\n'; + + if (value = editor.getParam('fullpage_default_title')) + header += '' + value + '\n'; + + if (value = editor.getParam('fullpage_default_encoding')) + header += '\n'; - if ((tmp = tinyMCE.getAttrib(nl[0], 'leftmargin')) != '' && body.style.marginLeft == '') - body.style.marginLeft = tmp + "px"; + if (value = editor.getParam('fullpage_default_font_family')) + styles += 'font-family: ' + value + ';'; - if ((tmp = tinyMCE.getAttrib(nl[0], 'rightmargin')) != '' && body.style.marginRight == '') - body.style.marginRight = tmp + "px"; + if (value = editor.getParam('fullpage_default_font_size')) + styles += 'font-size: ' + value + ';'; - if ((tmp = tinyMCE.getAttrib(nl[0], 'topmargin')) != '' && body.style.marginTop == '') - body.style.marginTop = tmp + "px"; + if (value = editor.getParam('fullpage_default_text_color')) + styles += 'color: ' + value + ';'; - if ((tmp = tinyMCE.getAttrib(nl[0], 'bottommargin')) != '' && body.style.marginBottom == '') - body.style.marginBottom = tmp + "px"; + header += '\n\n'; - body.dir = tinyMCE.getAttrib(nl[0], 'dir'); - body.vLink = tinyMCE.getAttrib(nl[0], 'vlink'); - body.aLink = tinyMCE.getAttrib(nl[0], 'alink'); - body.link = tinyMCE.getAttrib(nl[0], 'link'); - body.text = tinyMCE.getAttrib(nl[0], 'text'); + return header; + }, - if ((tmp = tinyMCE.getAttrib(nl[0], 'background')) != '') - body.style.backgroundImage = "url('" + tmp + "')"; + _getContent : function(ed, o) { + var self = this; - if ((tmp = tinyMCE.getAttrib(nl[0], 'bgcolor')) != '') - body.style.backgroundColor = tmp; + if (!o.source_view || !ed.getParam('fullpage_hide_in_source_view')) + o.content = tinymce.trim(self.head) + '\n' + tinymce.trim(o.content) + '\n' + tinymce.trim(self.foot); } - } -}; + }); -tinyMCE.addPlugin("fullpage", TinyMCE_FullPagePlugin); + // Register plugin + tinymce.PluginManager.add('fullpage', tinymce.plugins.FullPagePlugin); +})();