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

/BlogEngine/BlogEngine.NET/editors/tiny_mce_3_4_3_1/plugins/template/editor_plugin_src.js

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