includes/clientside/tinymce/plugins/fullpage/editor_plugin_src.js
changeset 335 67bd3121a12e
parent 1 fe660c52c48f
child 395 fa4c5ecb7c9a
equal deleted inserted replaced
334:c72b545f1304 335:67bd3121a12e
     1 /**
     1 /**
     2  * $Id: editor_plugin_src.js 201 2007-02-12 15:56:56Z spocke $
     2  * $Id: editor_plugin_src.js 373 2007-11-12 17:57:47Z spocke $
     3  *
     3  *
     4  * @author Moxiecode
     4  * @author Moxiecode
     5  * @copyright Copyright © 2004-2007, Moxiecode Systems AB, All rights reserved.
     5  * @copyright Copyright © 2004-2007, Moxiecode Systems AB, All rights reserved.
     6  */
     6  */
     7 
     7 
     8 /* Import plugin specific language pack */
     8 (function() {
     9 tinyMCE.importPluginLanguagePack('fullpage');
     9 	tinymce.create('tinymce.plugins.FullPagePlugin', {
       
    10 		init : function(ed, url) {
       
    11 			var t = this;
    10 
    12 
    11 var TinyMCE_FullPagePlugin = {
    13 			t.editor = ed;
    12 	getInfo : function() {
       
    13 		return {
       
    14 			longname : 'Fullpage',
       
    15 			author : 'Moxiecode Systems AB',
       
    16 			authorurl : 'http://tinymce.moxiecode.com',
       
    17 			infourl : 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/fullpage',
       
    18 			version : tinyMCE.majorVersion + "." + tinyMCE.minorVersion
       
    19 		};
       
    20 	},
       
    21 
    14 
    22 	getControlHTML : function(cn) {
    15 			// Register commands
    23 		switch (cn) {
    16 			ed.addCommand('mceFullPageProperties', function() {
    24 			case "fullpage":
    17 				ed.windowManager.open({
    25 				return tinyMCE.getButtonHTML(cn, 'lang_fullpage_desc', '{$pluginurl}/images/fullpage.gif', 'mceFullPageProperties');
    18 					file : url + '/fullpage.htm',
       
    19 					width : 430 + ed.getLang('fullpage.delta_width', 0),
       
    20 					height : 495 + ed.getLang('fullpage.delta_height', 0),
       
    21 					inline : 1
       
    22 				}, {
       
    23 					plugin_url : url,
       
    24 					head_html : t.head
       
    25 				});
       
    26 			});
       
    27 
       
    28 			// Register buttons
       
    29 			ed.addButton('fullpage', {title : 'fullpage.desc', cmd : 'mceFullPageProperties'});
       
    30 
       
    31 			ed.onBeforeSetContent.add(t._setContent, t);
       
    32 			ed.onGetContent.add(t._getContent, t);
       
    33 		},
       
    34 
       
    35 		getInfo : function() {
       
    36 			return {
       
    37 				longname : 'Fullpage',
       
    38 				author : 'Moxiecode Systems AB',
       
    39 				authorurl : 'http://tinymce.moxiecode.com',
       
    40 				infourl : 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/fullpage',
       
    41 				version : tinymce.majorVersion + "." + tinymce.minorVersion
       
    42 			};
       
    43 		},
       
    44 
       
    45 		// Private plugin internal methods
       
    46 
       
    47 		_createSerializer : function() {
       
    48 			return new tinymce.dom.Serializer({
       
    49 				dom : this.editor.dom,
       
    50 				apply_source_formatting : true
       
    51 			});
       
    52 		},
       
    53 
       
    54 		_setContent : function(ed, o) {
       
    55 			var t = this, sp, ep, c = o.content;
       
    56 
       
    57 			// Parse out head, body and footer
       
    58 			sp = c.indexOf('<body');
       
    59 			if (sp == -1)
       
    60 				sp = c.indexOf('<BODY');
       
    61 
       
    62 			if (sp != -1) {
       
    63 				sp = c.indexOf('>', sp);
       
    64 				t.head = c.substring(0, sp + 1);
       
    65 
       
    66 				ep = c.indexOf('</body', sp);
       
    67 				if (ep == -1)
       
    68 					ep = c.indexOf('</body', ep);
       
    69 
       
    70 				o.content = c.substring(sp + 1, ep);
       
    71 				t.foot = c.substring(ep);
       
    72 			} else {
       
    73 				t.head = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">';
       
    74 				t.head += '\n<html>\n<head>\n<title>Untitled document</title>\n</head>\n<body>\n';
       
    75 				t.foot = '\n</body>\n</html>';
       
    76 			}
       
    77 		},
       
    78 
       
    79 		_getContent : function(ed, o) {
       
    80 			var t = this;
       
    81 
       
    82 			o.content = tinymce.trim(t.head) + '\n' + tinymce.trim(o.content) + '\n' + tinymce.trim(t.foot);
    26 		}
    83 		}
       
    84 	});
    27 
    85 
    28 		return "";
    86 	// Register plugin
    29 	},
    87 	tinymce.PluginManager.add('fullpage', tinymce.plugins.FullPagePlugin);
    30 
    88 })();
    31 	execCommand : function(editor_id, element, command, user_interface, value) {
       
    32 		// Handle commands
       
    33 		switch (command) {
       
    34 			case "mceFullPageProperties":
       
    35 				var template = new Array();
       
    36 
       
    37 				template['file']   = '../../plugins/fullpage/fullpage.htm';
       
    38 				template['width']  = 430;
       
    39 				template['height'] = 485 + (tinyMCE.isOpera ? 5 : 0);
       
    40 
       
    41 				template['width'] += tinyMCE.getLang('lang_fullpage_delta_width', 0);
       
    42 				template['height'] += tinyMCE.getLang('lang_fullpage_delta_height', 0);
       
    43 
       
    44 				tinyMCE.openWindow(template, {editor_id : editor_id, inline : "yes"});
       
    45 			return true;
       
    46 
       
    47 			case "mceFullPageUpdate":
       
    48 				TinyMCE_FullPagePlugin._addToHead(tinyMCE.getInstanceById(editor_id));
       
    49 				return true;
       
    50 	   }
       
    51 
       
    52 	   // Pass to next handler in chain
       
    53 	   return false;
       
    54 	},
       
    55 
       
    56 	cleanup : function(type, content, inst) {
       
    57 		switch (type) {
       
    58 			case "insert_to_editor":
       
    59 				var tmp = content.toLowerCase();
       
    60 				var pos = tmp.indexOf('<body'), pos2;
       
    61 
       
    62 				// Split page in header and body chunks
       
    63 				if (pos != -1) {
       
    64 					pos = tmp.indexOf('>', pos);
       
    65 					pos2 = tmp.lastIndexOf('</body>');
       
    66 					inst.fullpageTopContent = content.substring(0, pos + 1);
       
    67 					content = content.substring(pos + 1, pos2);
       
    68 					// tinyMCE.debug(inst.fullpageTopContent, content);
       
    69 				} else {
       
    70 					if (!inst.fullpageTopContent) {
       
    71 						var docType = tinyMCE.getParam("fullpage_default_doctype", '<!DOCTYPE html PUBLIC "-/'+'/W3C//DTD XHTML 1.0 Transitional/'+'/EN" "http:/'+'/www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">');
       
    72 						var enc = tinyMCE.getParam("fullpage_default_encoding", 'utf-8');
       
    73 						var title = tinyMCE.getParam("fullpage_default_title", 'Untitled document');
       
    74 						var lang = tinyMCE.getParam("fullpage_default_langcode", 'en');
       
    75 						var pi = tinyMCE.getParam("fullpage_default_xml_pi", true);
       
    76 						var ff = tinyMCE.getParam("fullpage_default_font_family", "");
       
    77 						var fz = tinyMCE.getParam("fullpage_default_font_size", "");
       
    78 						var ds = tinyMCE.getParam("fullpage_default_style", "");
       
    79 						var dtc = tinyMCE.getParam("fullpage_default_text_color", "");
       
    80 
       
    81 						// Xml encode it
       
    82 						title = title.replace(/&/g, '&amp;');
       
    83 						title = title.replace(/\"/g, '&quot;');
       
    84 						title = title.replace(/</g, '&lt;');
       
    85 						title = title.replace(/>/g, '&gt;');
       
    86 
       
    87 						tmp = '';
       
    88 
       
    89 						// Make default chunk
       
    90 						if (pi)
       
    91 							tmp += '<?xml version="1.0" encoding="' + enc + '"?>\n';
       
    92 
       
    93 						tmp += docType + '\n';
       
    94 						tmp += '<html xmlns="http:/'+'/www.w3.org/1999/xhtml" lang="' + lang + '" xml:lang="' + lang + '">\n';
       
    95 						tmp += '<head>\n';
       
    96 						tmp += '\t<title>' + title + '</title>\n';
       
    97 						tmp += '\t<meta http-equiv="Content-Type" content="text/html; charset=' + enc + '" />\n';
       
    98 						tmp += '</head>\n';
       
    99 						tmp += '<body';
       
   100 
       
   101 						if (ff != '' || fz != '') {
       
   102 							tmp += ' style="';
       
   103 
       
   104 							if (ds != '')
       
   105 								tmp += ds + ";";
       
   106 
       
   107 							if (ff != '')
       
   108 								tmp += 'font-family: ' + ff + ";";
       
   109 
       
   110 							if (fz != '')
       
   111 								tmp += 'font-size: ' + fz + ";";
       
   112 
       
   113 							tmp += '"';
       
   114 						}
       
   115 
       
   116 						if (dtc != '')
       
   117 							tmp += ' text="' + dtc + '"';
       
   118 
       
   119 						tmp += '>\n';
       
   120 
       
   121 						inst.fullpageTopContent = tmp;
       
   122 					}
       
   123 				}
       
   124 
       
   125 				this._addToHead(inst);
       
   126 
       
   127 				break;
       
   128 
       
   129 			case "get_from_editor":
       
   130 				if (inst.fullpageTopContent)
       
   131 					content = inst.fullpageTopContent + content + "\n</body>\n</html>";
       
   132 
       
   133 				break;
       
   134 		}
       
   135 
       
   136 		// Pass through to next handler in chain
       
   137 		return content;
       
   138 	},
       
   139 
       
   140 	// Private plugin internal methods
       
   141 
       
   142 	_addToHead : function(inst) {
       
   143 		var doc = inst.getDoc();
       
   144 		var head = doc.getElementsByTagName("head")[0];
       
   145 		var body = doc.body;
       
   146 		var h = inst.fullpageTopContent;
       
   147 		var e = doc.createElement("body");
       
   148 		var nl, i, le, tmp;
       
   149 
       
   150 		// Remove stuff we don't want
       
   151 		h = h.replace(/(\r|\n)/gi, '');
       
   152 		h = h.replace(/<\?[^\>]*\>/gi, '');
       
   153 		h = h.replace(/<\/?(!DOCTYPE|head|html)[^\>]*\>/gi, '');
       
   154 		h = h.replace(/<script(.*?)<\/script>/gi, '');
       
   155 		h = h.replace(/<title(.*?)<\/title>/gi, '');
       
   156 		h = h.replace(/<(meta|base)[^>]*>/gi, '');
       
   157 
       
   158 		// Make link and style elements into pre
       
   159 		h = h.replace(/<link([^>]*)\/>/gi, '<pre mce_type="link" $1></pre>');
       
   160 		//h = h.replace(/<style([^>]*)>(.*?)<\/style>/gi, '<pre mce_type="style" $1>$2</pre>');
       
   161 
       
   162 		// Make body a div
       
   163 		h = h.replace(/<body/gi, '<div mce_type="body"');
       
   164 		h += '</div>';
       
   165 
       
   166 		// Now crapy MSIE can parse it
       
   167 		e.innerHTML = h;
       
   168 
       
   169 		// Reset all body attributes
       
   170 		body.vLink = body.aLink = body.link = body.text = '';
       
   171 		body.style.cssText = '';
       
   172 
       
   173 		// Delete all old links
       
   174 		nl = head.getElementsByTagName('link');
       
   175 		for (i=0; i<nl.length; i++) {
       
   176 			if (tinyMCE.getAttrib(nl[i], 'mce_head') == "true")
       
   177 				nl[i].parentNode.removeChild(nl[i]);
       
   178 		}
       
   179 
       
   180 		// Add link elements
       
   181 		nl = e.getElementsByTagName('pre');
       
   182 		for (i=0; i<nl.length; i++) {
       
   183 			tmp = tinyMCE.getAttrib(nl[i], 'media');
       
   184 			if (tinyMCE.getAttrib(nl[i], 'mce_type') == "link" && (tmp == "" || tmp == "screen" || tmp == "all") && tinyMCE.getAttrib(nl[i], 'rel') == "stylesheet") {
       
   185 				le = doc.createElement("link");
       
   186 
       
   187 				le.rel = "stylesheet";
       
   188 				le.href = tinyMCE.getAttrib(nl[i], 'href');
       
   189 				le.setAttribute("mce_head", "true");
       
   190 
       
   191 				head.appendChild(le);
       
   192 			}
       
   193 		}
       
   194 
       
   195 		// Add body attributes
       
   196 		nl = e.getElementsByTagName('div');
       
   197 		if (nl.length > 0) {
       
   198 			body.style.cssText = tinyMCE.getAttrib(nl[0], 'style');
       
   199 
       
   200 			if ((tmp = tinyMCE.getAttrib(nl[0], 'leftmargin')) != '' && body.style.marginLeft == '')
       
   201 				body.style.marginLeft = tmp + "px";
       
   202 
       
   203 			if ((tmp = tinyMCE.getAttrib(nl[0], 'rightmargin')) != '' && body.style.marginRight == '')
       
   204 				body.style.marginRight = tmp + "px";
       
   205 
       
   206 			if ((tmp = tinyMCE.getAttrib(nl[0], 'topmargin')) != '' && body.style.marginTop == '')
       
   207 				body.style.marginTop = tmp + "px";
       
   208 
       
   209 			if ((tmp = tinyMCE.getAttrib(nl[0], 'bottommargin')) != '' && body.style.marginBottom == '')
       
   210 				body.style.marginBottom = tmp + "px";
       
   211 
       
   212 			body.dir = tinyMCE.getAttrib(nl[0], 'dir');
       
   213 			body.vLink = tinyMCE.getAttrib(nl[0], 'vlink');
       
   214 			body.aLink = tinyMCE.getAttrib(nl[0], 'alink');
       
   215 			body.link = tinyMCE.getAttrib(nl[0], 'link');
       
   216 			body.text = tinyMCE.getAttrib(nl[0], 'text');
       
   217 
       
   218 			if ((tmp = tinyMCE.getAttrib(nl[0], 'background')) != '')
       
   219 				body.style.backgroundImage = "url('" + tmp + "')";
       
   220 
       
   221 			if ((tmp = tinyMCE.getAttrib(nl[0], 'bgcolor')) != '')
       
   222 				body.style.backgroundColor = tmp;
       
   223 		}
       
   224 	}
       
   225 };
       
   226 
       
   227 tinyMCE.addPlugin("fullpage", TinyMCE_FullPagePlugin);