/static/scripts/tiny_mce/plugins/template/editor_plugin_src.js

http://n23.googlecode.com/ · JavaScript · 156 lines · 112 code · 31 blank · 13 comment · 15 complexity · e89036bf03bcce43308365d2d17a5a77 MD5 · raw file

  1. /**
  2. * $Id: editor_plugin_src.js 201 2007-02-12 15:56:56Z spocke $
  3. *
  4. * @author Moxiecode
  5. * @copyright Copyright Š 2004-2008, Moxiecode Systems AB, All rights reserved.
  6. */
  7. (function() {
  8. var each = tinymce.each;
  9. tinymce.create('tinymce.plugins.TemplatePlugin', {
  10. init : function(ed, url) {
  11. var t = this;
  12. t.editor = ed;
  13. // Register commands
  14. ed.addCommand('mceTemplate', function(ui) {
  15. ed.windowManager.open({
  16. file : url + '/template.htm',
  17. width : ed.getParam('template_popup_width', 750),
  18. height : ed.getParam('template_popup_height', 600),
  19. inline : 1
  20. }, {
  21. plugin_url : url
  22. });
  23. });
  24. ed.addCommand('mceInsertTemplate', t._insertTemplate, t);
  25. // Register buttons
  26. ed.addButton('template', {title : 'template.desc', cmd : 'mceTemplate'});
  27. ed.onPreProcess.add(function(ed, o) {
  28. var dom = ed.dom;
  29. each(dom.select('div', o.node), function(e) {
  30. if (dom.hasClass(e, 'mceTmpl')) {
  31. each(dom.select('*', e), function(e) {
  32. if (dom.hasClass(e, ed.getParam('template_mdate_classes', 'mdate').replace(/\s+/g, '|')))
  33. e.innerHTML = t._getDateTime(new Date(), ed.getParam("template_mdate_format", ed.getLang("template.mdate_format")));
  34. });
  35. t._replaceVals(e);
  36. }
  37. });
  38. });
  39. },
  40. getInfo : function() {
  41. return {
  42. longname : 'Template plugin',
  43. author : 'Moxiecode Systems AB',
  44. authorurl : 'http://www.moxiecode.com',
  45. infourl : 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/template',
  46. version : tinymce.majorVersion + "." + tinymce.minorVersion
  47. };
  48. },
  49. _insertTemplate : function(ui, v) {
  50. var t = this, ed = t.editor, h, el, dom = ed.dom, sel = ed.selection.getContent();
  51. h = v.content;
  52. each(t.editor.getParam('template_replace_values'), function(v, k) {
  53. if (typeof(v) != 'function')
  54. h = h.replace(new RegExp('\\{\\$' + k + '\\}', 'g'), v);
  55. });
  56. el = dom.create('div', null, h);
  57. // Find template element within div
  58. n = dom.select('.mceTmpl', el);
  59. if (n && n.length > 0) {
  60. el = dom.create('div', null);
  61. el.appendChild(n[0].cloneNode(true));
  62. }
  63. function hasClass(n, c) {
  64. return new RegExp('\\b' + c + '\\b', 'g').test(n.className);
  65. };
  66. each(dom.select('*', el), function(n) {
  67. // Replace cdate
  68. if (hasClass(n, ed.getParam('template_cdate_classes', 'cdate').replace(/\s+/g, '|')))
  69. n.innerHTML = t._getDateTime(new Date(), ed.getParam("template_cdate_format", ed.getLang("template.cdate_format")));
  70. // Replace mdate
  71. if (hasClass(n, ed.getParam('template_mdate_classes', 'mdate').replace(/\s+/g, '|')))
  72. n.innerHTML = t._getDateTime(new Date(), ed.getParam("template_mdate_format", ed.getLang("template.mdate_format")));
  73. // Replace selection
  74. if (hasClass(n, ed.getParam('template_selected_content_classes', 'selcontent').replace(/\s+/g, '|')))
  75. n.innerHTML = sel;
  76. });
  77. t._replaceVals(el);
  78. ed.execCommand('mceInsertContent', false, el.innerHTML);
  79. ed.addVisual();
  80. },
  81. _replaceVals : function(e) {
  82. var dom = this.editor.dom, vl = this.editor.getParam('template_replace_values');
  83. each(dom.select('*', e), function(e) {
  84. each(vl, function(v, k) {
  85. if (dom.hasClass(e, k)) {
  86. if (typeof(vl[k]) == 'function')
  87. vl[k](e);
  88. }
  89. });
  90. });
  91. },
  92. _getDateTime : function(d, fmt) {
  93. if (!fmt)
  94. return "";
  95. function addZeros(value, len) {
  96. var i;
  97. value = "" + value;
  98. if (value.length < len) {
  99. for (i=0; i<(len-value.length); i++)
  100. value = "0" + value;
  101. }
  102. return value;
  103. }
  104. fmt = fmt.replace("%D", "%m/%d/%y");
  105. fmt = fmt.replace("%r", "%I:%M:%S %p");
  106. fmt = fmt.replace("%Y", "" + d.getFullYear());
  107. fmt = fmt.replace("%y", "" + d.getYear());
  108. fmt = fmt.replace("%m", addZeros(d.getMonth()+1, 2));
  109. fmt = fmt.replace("%d", addZeros(d.getDate(), 2));
  110. fmt = fmt.replace("%H", "" + addZeros(d.getHours(), 2));
  111. fmt = fmt.replace("%M", "" + addZeros(d.getMinutes(), 2));
  112. fmt = fmt.replace("%S", "" + addZeros(d.getSeconds(), 2));
  113. fmt = fmt.replace("%I", "" + ((d.getHours() + 11) % 12 + 1));
  114. fmt = fmt.replace("%p", "" + (d.getHours() < 12 ? "AM" : "PM"));
  115. fmt = fmt.replace("%B", "" + tinyMCE.getLang("template_months_long").split(',')[d.getMonth()]);
  116. fmt = fmt.replace("%b", "" + tinyMCE.getLang("template_months_short").split(',')[d.getMonth()]);
  117. fmt = fmt.replace("%A", "" + tinyMCE.getLang("template_day_long").split(',')[d.getDay()]);
  118. fmt = fmt.replace("%a", "" + tinyMCE.getLang("template_day_short").split(',')[d.getDay()]);
  119. fmt = fmt.replace("%%", "%");
  120. return fmt;
  121. }
  122. });
  123. // Register plugin
  124. tinymce.PluginManager.add('template', tinymce.plugins.TemplatePlugin);
  125. })();