PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/BlogEngine/BlogEngine.NET/editors/tiny_mce_3_4_3_1/plugins/xhtmlxtras/js/element_common.js

#
JavaScript | 229 lines | 170 code | 37 blank | 22 comment | 44 complexity | b817f5e2d514147d72765c92c1f60f3d MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, BSD-3-Clause
  1. /**
  2. * element_common.js
  3. *
  4. * Copyright 2009, Moxiecode Systems AB
  5. * Released under LGPL License.
  6. *
  7. * License: http://tinymce.moxiecode.com/license
  8. * Contributing: http://tinymce.moxiecode.com/contributing
  9. */
  10. tinyMCEPopup.requireLangPack();
  11. function initCommonAttributes(elm) {
  12. var formObj = document.forms[0], dom = tinyMCEPopup.editor.dom;
  13. // Setup form data for common element attributes
  14. setFormValue('title', dom.getAttrib(elm, 'title'));
  15. setFormValue('id', dom.getAttrib(elm, 'id'));
  16. selectByValue(formObj, 'class', dom.getAttrib(elm, 'class'), true);
  17. setFormValue('style', dom.getAttrib(elm, 'style'));
  18. selectByValue(formObj, 'dir', dom.getAttrib(elm, 'dir'));
  19. setFormValue('lang', dom.getAttrib(elm, 'lang'));
  20. setFormValue('onfocus', dom.getAttrib(elm, 'onfocus'));
  21. setFormValue('onblur', dom.getAttrib(elm, 'onblur'));
  22. setFormValue('onclick', dom.getAttrib(elm, 'onclick'));
  23. setFormValue('ondblclick', dom.getAttrib(elm, 'ondblclick'));
  24. setFormValue('onmousedown', dom.getAttrib(elm, 'onmousedown'));
  25. setFormValue('onmouseup', dom.getAttrib(elm, 'onmouseup'));
  26. setFormValue('onmouseover', dom.getAttrib(elm, 'onmouseover'));
  27. setFormValue('onmousemove', dom.getAttrib(elm, 'onmousemove'));
  28. setFormValue('onmouseout', dom.getAttrib(elm, 'onmouseout'));
  29. setFormValue('onkeypress', dom.getAttrib(elm, 'onkeypress'));
  30. setFormValue('onkeydown', dom.getAttrib(elm, 'onkeydown'));
  31. setFormValue('onkeyup', dom.getAttrib(elm, 'onkeyup'));
  32. }
  33. function setFormValue(name, value) {
  34. if(document.forms[0].elements[name]) document.forms[0].elements[name].value = value;
  35. }
  36. function insertDateTime(id) {
  37. document.getElementById(id).value = getDateTime(new Date(), "%Y-%m-%dT%H:%M:%S");
  38. }
  39. function getDateTime(d, fmt) {
  40. fmt = fmt.replace("%D", "%m/%d/%y");
  41. fmt = fmt.replace("%r", "%I:%M:%S %p");
  42. fmt = fmt.replace("%Y", "" + d.getFullYear());
  43. fmt = fmt.replace("%y", "" + d.getYear());
  44. fmt = fmt.replace("%m", addZeros(d.getMonth()+1, 2));
  45. fmt = fmt.replace("%d", addZeros(d.getDate(), 2));
  46. fmt = fmt.replace("%H", "" + addZeros(d.getHours(), 2));
  47. fmt = fmt.replace("%M", "" + addZeros(d.getMinutes(), 2));
  48. fmt = fmt.replace("%S", "" + addZeros(d.getSeconds(), 2));
  49. fmt = fmt.replace("%I", "" + ((d.getHours() + 11) % 12 + 1));
  50. fmt = fmt.replace("%p", "" + (d.getHours() < 12 ? "AM" : "PM"));
  51. fmt = fmt.replace("%%", "%");
  52. return fmt;
  53. }
  54. function addZeros(value, len) {
  55. var i;
  56. value = "" + value;
  57. if (value.length < len) {
  58. for (i=0; i<(len-value.length); i++)
  59. value = "0" + value;
  60. }
  61. return value;
  62. }
  63. function selectByValue(form_obj, field_name, value, add_custom, ignore_case) {
  64. if (!form_obj || !form_obj.elements[field_name])
  65. return;
  66. var sel = form_obj.elements[field_name];
  67. var found = false;
  68. for (var i=0; i<sel.options.length; i++) {
  69. var option = sel.options[i];
  70. if (option.value == value || (ignore_case && option.value.toLowerCase() == value.toLowerCase())) {
  71. option.selected = true;
  72. found = true;
  73. } else
  74. option.selected = false;
  75. }
  76. if (!found && add_custom && value != '') {
  77. var option = new Option('Value: ' + value, value);
  78. option.selected = true;
  79. sel.options[sel.options.length] = option;
  80. }
  81. return found;
  82. }
  83. function setAttrib(elm, attrib, value) {
  84. var formObj = document.forms[0];
  85. var valueElm = formObj.elements[attrib.toLowerCase()];
  86. tinyMCEPopup.editor.dom.setAttrib(elm, attrib, value || valueElm.value);
  87. }
  88. function setAllCommonAttribs(elm) {
  89. setAttrib(elm, 'title');
  90. setAttrib(elm, 'id');
  91. setAttrib(elm, 'class');
  92. setAttrib(elm, 'style');
  93. setAttrib(elm, 'dir');
  94. setAttrib(elm, 'lang');
  95. /*setAttrib(elm, 'onfocus');
  96. setAttrib(elm, 'onblur');
  97. setAttrib(elm, 'onclick');
  98. setAttrib(elm, 'ondblclick');
  99. setAttrib(elm, 'onmousedown');
  100. setAttrib(elm, 'onmouseup');
  101. setAttrib(elm, 'onmouseover');
  102. setAttrib(elm, 'onmousemove');
  103. setAttrib(elm, 'onmouseout');
  104. setAttrib(elm, 'onkeypress');
  105. setAttrib(elm, 'onkeydown');
  106. setAttrib(elm, 'onkeyup');*/
  107. }
  108. SXE = {
  109. currentAction : "insert",
  110. inst : tinyMCEPopup.editor,
  111. updateElement : null
  112. }
  113. SXE.focusElement = SXE.inst.selection.getNode();
  114. SXE.initElementDialog = function(element_name) {
  115. addClassesToList('class', 'xhtmlxtras_styles');
  116. TinyMCE_EditableSelects.init();
  117. element_name = element_name.toLowerCase();
  118. var elm = SXE.inst.dom.getParent(SXE.focusElement, element_name.toUpperCase());
  119. if (elm != null && elm.nodeName.toUpperCase() == element_name.toUpperCase()) {
  120. SXE.currentAction = "update";
  121. }
  122. if (SXE.currentAction == "update") {
  123. initCommonAttributes(elm);
  124. SXE.updateElement = elm;
  125. }
  126. document.forms[0].insert.value = tinyMCEPopup.getLang(SXE.currentAction, 'Insert', true);
  127. }
  128. SXE.insertElement = function(element_name) {
  129. var elm = SXE.inst.dom.getParent(SXE.focusElement, element_name.toUpperCase()), h, tagName;
  130. if (elm == null) {
  131. var s = SXE.inst.selection.getContent();
  132. if(s.length > 0) {
  133. tagName = element_name;
  134. insertInlineElement(element_name);
  135. var elementArray = tinymce.grep(SXE.inst.dom.select(element_name));
  136. for (var i=0; i<elementArray.length; i++) {
  137. var elm = elementArray[i];
  138. if (SXE.inst.dom.getAttrib(elm, 'data-mce-new')) {
  139. elm.id = '';
  140. elm.setAttribute('id', '');
  141. elm.removeAttribute('id');
  142. elm.removeAttribute('data-mce-new');
  143. setAllCommonAttribs(elm);
  144. }
  145. }
  146. }
  147. } else {
  148. setAllCommonAttribs(elm);
  149. }
  150. SXE.inst.nodeChanged();
  151. tinyMCEPopup.execCommand('mceEndUndoLevel');
  152. }
  153. SXE.removeElement = function(element_name){
  154. element_name = element_name.toLowerCase();
  155. elm = SXE.inst.dom.getParent(SXE.focusElement, element_name.toUpperCase());
  156. if(elm && elm.nodeName.toUpperCase() == element_name.toUpperCase()){
  157. tinyMCE.execCommand('mceRemoveNode', false, elm);
  158. SXE.inst.nodeChanged();
  159. tinyMCEPopup.execCommand('mceEndUndoLevel');
  160. }
  161. }
  162. SXE.showRemoveButton = function() {
  163. document.getElementById("remove").style.display = '';
  164. }
  165. SXE.containsClass = function(elm,cl) {
  166. return (elm.className.indexOf(cl) > -1) ? true : false;
  167. }
  168. SXE.removeClass = function(elm,cl) {
  169. if(elm.className == null || elm.className == "" || !SXE.containsClass(elm,cl)) {
  170. return true;
  171. }
  172. var classNames = elm.className.split(" ");
  173. var newClassNames = "";
  174. for (var x = 0, cnl = classNames.length; x < cnl; x++) {
  175. if (classNames[x] != cl) {
  176. newClassNames += (classNames[x] + " ");
  177. }
  178. }
  179. elm.className = newClassNames.substring(0,newClassNames.length-1); //removes extra space at the end
  180. }
  181. SXE.addClass = function(elm,cl) {
  182. if(!SXE.containsClass(elm,cl)) elm.className ? elm.className += " " + cl : elm.className = cl;
  183. return true;
  184. }
  185. function insertInlineElement(en) {
  186. var ed = tinyMCEPopup.editor, dom = ed.dom;
  187. ed.getDoc().execCommand('FontName', false, 'mceinline');
  188. tinymce.each(dom.select('span,font'), function(n) {
  189. if (n.style.fontFamily == 'mceinline' || n.face == 'mceinline')
  190. dom.replace(dom.create(en, {'data-mce-new' : 1}), n, 1);
  191. });
  192. }