/static/scripts/tiny_mce/plugins/fullpage/js/fullpage.js

http://n23.googlecode.com/ · JavaScript · 462 lines · 354 code · 86 blank · 22 comment · 96 complexity · 296470a2b7ac6622dea2967a0d167358 MD5 · raw file

  1. tinyMCEPopup.requireLangPack();
  2. var doc;
  3. var defaultDocTypes =
  4. 'XHTML 1.0 Transitional=<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">,' +
  5. 'XHTML 1.0 Frameset=<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">,' +
  6. 'XHTML 1.0 Strict=<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">,' +
  7. 'XHTML 1.1=<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">,' +
  8. 'HTML 4.01 Transitional=<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">,' +
  9. 'HTML 4.01 Strict=<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">,' +
  10. 'HTML 4.01 Frameset=<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">';
  11. var defaultEncodings =
  12. 'Western european (iso-8859-1)=iso-8859-1,' +
  13. 'Central European (iso-8859-2)=iso-8859-2,' +
  14. 'Unicode (UTF-8)=utf-8,' +
  15. 'Chinese traditional (Big5)=big5,' +
  16. 'Cyrillic (iso-8859-5)=iso-8859-5,' +
  17. 'Japanese (iso-2022-jp)=iso-2022-jp,' +
  18. 'Greek (iso-8859-7)=iso-8859-7,' +
  19. 'Korean (iso-2022-kr)=iso-2022-kr,' +
  20. 'ASCII (us-ascii)=us-ascii';
  21. var defaultMediaTypes =
  22. 'all=all,' +
  23. 'screen=screen,' +
  24. 'print=print,' +
  25. 'tty=tty,' +
  26. 'tv=tv,' +
  27. 'projection=projection,' +
  28. 'handheld=handheld,' +
  29. 'braille=braille,' +
  30. 'aural=aural';
  31. var defaultFontNames = 'Arial=arial,helvetica,sans-serif;Courier New=courier new,courier,monospace;Georgia=georgia,times new roman,times,serif;Tahoma=tahoma,arial,helvetica,sans-serif;Times New Roman=times new roman,times,serif;Verdana=verdana,arial,helvetica,sans-serif;Impact=impact;WingDings=wingdings';
  32. var defaultFontSizes = '10px,11px,12px,13px,14px,15px,16px';
  33. function init() {
  34. var f = document.forms['fullpage'], el = f.elements, e, i, p, doctypes, encodings, mediaTypes, fonts, ed = tinyMCEPopup.editor, dom = tinyMCEPopup.dom, style;
  35. // Setup doctype select box
  36. doctypes = ed.getParam("fullpage_doctypes", defaultDocTypes).split(',');
  37. for (i=0; i<doctypes.length; i++) {
  38. p = doctypes[i].split('=');
  39. if (p.length > 1)
  40. addSelectValue(f, 'doctypes', p[0], p[1]);
  41. }
  42. // Setup fonts select box
  43. fonts = ed.getParam("fullpage_fonts", defaultFontNames).split(';');
  44. for (i=0; i<fonts.length; i++) {
  45. p = fonts[i].split('=');
  46. if (p.length > 1)
  47. addSelectValue(f, 'fontface', p[0], p[1]);
  48. }
  49. // Setup fontsize select box
  50. fonts = ed.getParam("fullpage_fontsizes", defaultFontSizes).split(',');
  51. for (i=0; i<fonts.length; i++)
  52. addSelectValue(f, 'fontsize', fonts[i], fonts[i]);
  53. // Setup mediatype select boxs
  54. mediaTypes = ed.getParam("fullpage_media_types", defaultMediaTypes).split(',');
  55. for (i=0; i<mediaTypes.length; i++) {
  56. p = mediaTypes[i].split('=');
  57. if (p.length > 1) {
  58. addSelectValue(f, 'element_style_media', p[0], p[1]);
  59. addSelectValue(f, 'element_link_media', p[0], p[1]);
  60. }
  61. }
  62. // Setup encodings select box
  63. encodings = ed.getParam("fullpage_encodings", defaultEncodings).split(',');
  64. for (i=0; i<encodings.length; i++) {
  65. p = encodings[i].split('=');
  66. if (p.length > 1) {
  67. addSelectValue(f, 'docencoding', p[0], p[1]);
  68. addSelectValue(f, 'element_script_charset', p[0], p[1]);
  69. addSelectValue(f, 'element_link_charset', p[0], p[1]);
  70. }
  71. }
  72. document.getElementById('bgcolor_pickcontainer').innerHTML = getColorPickerHTML('bgcolor_pick','bgcolor');
  73. document.getElementById('link_color_pickcontainer').innerHTML = getColorPickerHTML('link_color_pick','link_color');
  74. //document.getElementById('hover_color_pickcontainer').innerHTML = getColorPickerHTML('hover_color_pick','hover_color');
  75. document.getElementById('visited_color_pickcontainer').innerHTML = getColorPickerHTML('visited_color_pick','visited_color');
  76. document.getElementById('active_color_pickcontainer').innerHTML = getColorPickerHTML('active_color_pick','active_color');
  77. document.getElementById('textcolor_pickcontainer').innerHTML = getColorPickerHTML('textcolor_pick','textcolor');
  78. document.getElementById('stylesheet_browsercontainer').innerHTML = getBrowserHTML('stylesheetbrowser','stylesheet','file','fullpage');
  79. document.getElementById('link_href_pickcontainer').innerHTML = getBrowserHTML('link_href_browser','element_link_href','file','fullpage');
  80. document.getElementById('script_src_pickcontainer').innerHTML = getBrowserHTML('script_src_browser','element_script_src','file','fullpage');
  81. document.getElementById('bgimage_pickcontainer').innerHTML = getBrowserHTML('bgimage_browser','bgimage','image','fullpage');
  82. // Resize some elements
  83. if (isVisible('stylesheetbrowser'))
  84. document.getElementById('stylesheet').style.width = '220px';
  85. if (isVisible('link_href_browser'))
  86. document.getElementById('element_link_href').style.width = '230px';
  87. if (isVisible('bgimage_browser'))
  88. document.getElementById('bgimage').style.width = '210px';
  89. // Add iframe
  90. dom.add(document.body, 'iframe', {id : 'documentIframe', src : 'javascript:""', style : {display : 'none'}});
  91. doc = dom.get('documentIframe').contentWindow.document;
  92. h = tinyMCEPopup.getWindowArg('head_html');
  93. // Preprocess the HTML disable scripts and urls
  94. h = h.replace(/<script>/gi, '<script type="text/javascript">');
  95. h = h.replace(/type=([\"\'])?/gi, 'type=$1-mce-');
  96. h = h.replace(/(src=|href=)/g, 'mce_$1');
  97. // Write in the content in the iframe
  98. doc.write(h + '</body></html>');
  99. doc.close();
  100. // Parse xml and doctype
  101. xmlVer = getReItem(/<\?\s*?xml.*?version\s*?=\s*?"(.*?)".*?\?>/gi, h, 1);
  102. xmlEnc = getReItem(/<\?\s*?xml.*?encoding\s*?=\s*?"(.*?)".*?\?>/gi, h, 1);
  103. docType = getReItem(/<\!DOCTYPE.*?>/gi, h, 0);
  104. f.langcode.value = getReItem(/lang="(.*?)"/gi, h, 1);
  105. // Parse title
  106. if (e = doc.getElementsByTagName('title')[0])
  107. el.metatitle.value = e.textContent || e.text;
  108. // Parse meta
  109. tinymce.each(doc.getElementsByTagName('meta'), function(n) {
  110. var na = (n.getAttribute('name', 2) || '').toLowerCase(), va = n.getAttribute('content', 2), eq = n.getAttribute('httpEquiv', 2) || '';
  111. e = el['meta' + na];
  112. if (na == 'robots') {
  113. selectByValue(f, 'metarobots', tinymce.trim(va), true, true);
  114. return;
  115. }
  116. switch (eq.toLowerCase()) {
  117. case "content-type":
  118. tmp = getReItem(/charset\s*=\s*(.*)\s*/gi, value, 1);
  119. // Override XML encoding
  120. if (tmp != "")
  121. xmlEnc = tmp;
  122. return;
  123. }
  124. if (e)
  125. e.value = va;
  126. });
  127. selectByValue(f, 'doctypes', docType, true, true);
  128. selectByValue(f, 'docencoding', xmlEnc, true, true);
  129. selectByValue(f, 'langdir', doc.body.getAttribute('dir', 2) || '', true, true);
  130. if (xmlVer != '')
  131. el.xml_pi.checked = true;
  132. // Parse appearance
  133. // Parse primary stylesheet
  134. tinymce.each(doc.getElementsByTagName("link"), function(l) {
  135. var m = l.getAttribute('media', 2) || '', t = l.getAttribute('type', 2) || '';
  136. if (t == "-mce-text/css" && (m == "" || m == "screen" || m == "all") && (l.getAttribute('rel', 2) || '') == "stylesheet") {
  137. f.stylesheet.value = l.getAttribute('mce_href', 2) || '';
  138. return false;
  139. }
  140. });
  141. // Get from style elements
  142. tinymce.each(doc.getElementsByTagName("style"), function(st) {
  143. var tmp = parseStyleElement(st);
  144. for (x=0; x<tmp.length; x++) {
  145. if (tmp[x].rule.indexOf('a:visited') != -1 && tmp[x].data['color'])
  146. f.visited_color.value = tmp[x].data['color'];
  147. if (tmp[x].rule.indexOf('a:link') != -1 && tmp[x].data['color'])
  148. f.link_color.value = tmp[x].data['color'];
  149. if (tmp[x].rule.indexOf('a:active') != -1 && tmp[x].data['color'])
  150. f.active_color.value = tmp[x].data['color'];
  151. }
  152. });
  153. f.textcolor.value = tinyMCEPopup.dom.getAttrib(doc.body, "text");
  154. f.active_color.value = tinyMCEPopup.dom.getAttrib(doc.body, "alink");
  155. f.link_color.value = tinyMCEPopup.dom.getAttrib(doc.body, "link");
  156. f.visited_color.value = tinyMCEPopup.dom.getAttrib(doc.body, "vlink");
  157. f.bgcolor.value = tinyMCEPopup.dom.getAttrib(doc.body, "bgcolor");
  158. f.bgimage.value = tinyMCEPopup.dom.getAttrib(doc.body, "background");
  159. // Get from style info
  160. style = tinyMCEPopup.dom.parseStyle(tinyMCEPopup.dom.getAttrib(doc.body, 'style'));
  161. if (style['font-family'])
  162. selectByValue(f, 'fontface', style['font-family'], true, true);
  163. else
  164. selectByValue(f, 'fontface', ed.getParam("fullpage_default_fontface", ""), true, true);
  165. if (style['font-size'])
  166. selectByValue(f, 'fontsize', style['font-size'], true, true);
  167. else
  168. selectByValue(f, 'fontsize', ed.getParam("fullpage_default_fontsize", ""), true, true);
  169. if (style['color'])
  170. f.textcolor.value = convertRGBToHex(style['color']);
  171. if (style['background-image'])
  172. f.bgimage.value = style['background-image'].replace(new RegExp("url\\('?([^']*)'?\\)", 'gi'), "$1");
  173. if (style['background-color'])
  174. f.bgcolor.value = style['background-color'];
  175. if (style['margin']) {
  176. tmp = style['margin'].replace(/[^0-9 ]/g, '');
  177. tmp = tmp.split(/ +/);
  178. f.topmargin.value = tmp.length > 0 ? tmp[0] : '';
  179. f.rightmargin.value = tmp.length > 1 ? tmp[1] : tmp[0];
  180. f.bottommargin.value = tmp.length > 2 ? tmp[2] : tmp[0];
  181. f.leftmargin.value = tmp.length > 3 ? tmp[3] : tmp[0];
  182. }
  183. if (style['margin-left'])
  184. f.leftmargin.value = style['margin-left'].replace(/[^0-9]/g, '');
  185. if (style['margin-right'])
  186. f.rightmargin.value = style['margin-right'].replace(/[^0-9]/g, '');
  187. if (style['margin-top'])
  188. f.topmargin.value = style['margin-top'].replace(/[^0-9]/g, '');
  189. if (style['margin-bottom'])
  190. f.bottommargin.value = style['margin-bottom'].replace(/[^0-9]/g, '');
  191. f.style.value = tinyMCEPopup.dom.serializeStyle(style);
  192. // Update colors
  193. updateColor('textcolor_pick', 'textcolor');
  194. updateColor('bgcolor_pick', 'bgcolor');
  195. updateColor('visited_color_pick', 'visited_color');
  196. updateColor('active_color_pick', 'active_color');
  197. updateColor('link_color_pick', 'link_color');
  198. }
  199. function getReItem(r, s, i) {
  200. var c = r.exec(s);
  201. if (c && c.length > i)
  202. return c[i];
  203. return '';
  204. }
  205. function updateAction() {
  206. var f = document.forms[0], nl, i, h, v, s, head, html, l, tmp, addlink = true, ser;
  207. head = doc.getElementsByTagName('head')[0];
  208. // Fix scripts without a type
  209. nl = doc.getElementsByTagName('script');
  210. for (i=0; i<nl.length; i++) {
  211. if (tinyMCEPopup.dom.getAttrib(nl[i], 'mce_type') == '')
  212. nl[i].setAttribute('mce_type', 'text/javascript');
  213. }
  214. // Get primary stylesheet
  215. nl = doc.getElementsByTagName("link");
  216. for (i=0; i<nl.length; i++) {
  217. l = nl[i];
  218. tmp = tinyMCEPopup.dom.getAttrib(l, 'media');
  219. if (tinyMCEPopup.dom.getAttrib(l, 'mce_type') == "text/css" && (tmp == "" || tmp == "screen" || tmp == "all") && tinyMCEPopup.dom.getAttrib(l, 'rel') == "stylesheet") {
  220. addlink = false;
  221. if (f.stylesheet.value == '')
  222. l.parentNode.removeChild(l);
  223. else
  224. l.setAttribute('mce_href', f.stylesheet.value);
  225. break;
  226. }
  227. }
  228. // Add new link
  229. if (f.stylesheet.value != '') {
  230. l = doc.createElement('link');
  231. l.setAttribute('type', 'text/css');
  232. l.setAttribute('mce_href', f.stylesheet.value);
  233. l.setAttribute('rel', 'stylesheet');
  234. head.appendChild(l);
  235. }
  236. setMeta(head, 'keywords', f.metakeywords.value);
  237. setMeta(head, 'description', f.metadescription.value);
  238. setMeta(head, 'author', f.metaauthor.value);
  239. setMeta(head, 'copyright', f.metacopyright.value);
  240. setMeta(head, 'robots', getSelectValue(f, 'metarobots'));
  241. setMeta(head, 'Content-Type', getSelectValue(f, 'docencoding'));
  242. doc.body.dir = getSelectValue(f, 'langdir');
  243. doc.body.style.cssText = f.style.value;
  244. doc.body.setAttribute('vLink', f.visited_color.value);
  245. doc.body.setAttribute('link', f.link_color.value);
  246. doc.body.setAttribute('text', f.textcolor.value);
  247. doc.body.setAttribute('aLink', f.active_color.value);
  248. doc.body.style.fontFamily = getSelectValue(f, 'fontface');
  249. doc.body.style.fontSize = getSelectValue(f, 'fontsize');
  250. doc.body.style.backgroundColor = f.bgcolor.value;
  251. if (f.leftmargin.value != '')
  252. doc.body.style.marginLeft = f.leftmargin.value + 'px';
  253. if (f.rightmargin.value != '')
  254. doc.body.style.marginRight = f.rightmargin.value + 'px';
  255. if (f.bottommargin.value != '')
  256. doc.body.style.marginBottom = f.bottommargin.value + 'px';
  257. if (f.topmargin.value != '')
  258. doc.body.style.marginTop = f.topmargin.value + 'px';
  259. html = doc.getElementsByTagName('html')[0];
  260. html.setAttribute('lang', f.langcode.value);
  261. html.setAttribute('xml:lang', f.langcode.value);
  262. if (f.bgimage.value != '')
  263. doc.body.style.backgroundImage = "url('" + f.bgimage.value + "')";
  264. else
  265. doc.body.style.backgroundImage = '';
  266. ser = tinyMCEPopup.editor.plugins.fullpage._createSerializer();
  267. ser.setRules('-title,meta[http-equiv|name|content],base[href|target],link[href|rel|type|title|media],style[type],script[type|language|src],html[lang|xml::lang|xmlns],body[style|dir|vlink|link|text|alink],head');
  268. h = ser.serialize(doc.documentElement);
  269. h = h.substring(0, h.lastIndexOf('</body>'));
  270. if (h.indexOf('<title>') == -1)
  271. h = h.replace(/<head.*?>/, '$&\n' + '<title>' + tinyMCEPopup.dom.encode(f.metatitle.value) + '</title>');
  272. else
  273. h = h.replace(/<title>(.*?)<\/title>/, '<title>' + tinyMCEPopup.dom.encode(f.metatitle.value) + '</title>');
  274. if ((v = getSelectValue(f, 'doctypes')) != '')
  275. h = v + '\n' + h;
  276. if (f.xml_pi.checked) {
  277. s = '<?xml version="1.0"';
  278. if ((v = getSelectValue(f, 'docencoding')) != '')
  279. s += ' encoding="' + v + '"';
  280. s += '?>\n';
  281. h = s + h;
  282. }
  283. h = h.replace(/type=\"\-mce\-/gi, 'type="');
  284. tinyMCEPopup.editor.plugins.fullpage.head = h;
  285. tinyMCEPopup.editor.plugins.fullpage._setBodyAttribs(tinyMCEPopup.editor, {});
  286. tinyMCEPopup.close();
  287. }
  288. function changedStyleField(field) {
  289. //alert(field.id);
  290. }
  291. function setMeta(he, k, v) {
  292. var nl, i, m;
  293. nl = he.getElementsByTagName('meta');
  294. for (i=0; i<nl.length; i++) {
  295. if (k == 'Content-Type' && tinyMCEPopup.dom.getAttrib(nl[i], 'http-equiv') == k) {
  296. if (v == '')
  297. nl[i].parentNode.removeChild(nl[i]);
  298. else
  299. nl[i].setAttribute('content', "text/html; charset=" + v);
  300. return;
  301. }
  302. if (tinyMCEPopup.dom.getAttrib(nl[i], 'name') == k) {
  303. if (v == '')
  304. nl[i].parentNode.removeChild(nl[i]);
  305. else
  306. nl[i].setAttribute('content', v);
  307. return;
  308. }
  309. }
  310. if (v == '')
  311. return;
  312. m = doc.createElement('meta');
  313. if (k == 'Content-Type')
  314. m.httpEquiv = k;
  315. else
  316. m.setAttribute('name', k);
  317. m.setAttribute('content', v);
  318. he.appendChild(m);
  319. }
  320. function parseStyleElement(e) {
  321. var v = e.innerHTML;
  322. var p, i, r;
  323. v = v.replace(/<!--/gi, '');
  324. v = v.replace(/-->/gi, '');
  325. v = v.replace(/[\n\r]/gi, '');
  326. v = v.replace(/\s+/gi, ' ');
  327. r = [];
  328. p = v.split(/{|}/);
  329. for (i=0; i<p.length; i+=2) {
  330. if (p[i] != "")
  331. r[r.length] = {rule : tinymce.trim(p[i]), data : tinyMCEPopup.dom.parseStyle(p[i+1])};
  332. }
  333. return r;
  334. }
  335. function serializeStyleElement(d) {
  336. var i, s, st;
  337. s = '<!--\n';
  338. for (i=0; i<d.length; i++) {
  339. s += d[i].rule + ' {\n';
  340. st = tinyMCE.serializeStyle(d[i].data);
  341. if (st != '')
  342. st += ';';
  343. s += st.replace(/;/g, ';\n');
  344. s += '}\n';
  345. if (i != d.length - 1)
  346. s += '\n';
  347. }
  348. s += '\n-->';
  349. return s;
  350. }
  351. tinyMCEPopup.onInit.add(init);