uibase.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. (function() {
  2. var utils = baidu.editor.utils,
  3. uiUtils = baidu.editor.ui.uiUtils,
  4. EventBase = baidu.editor.EventBase,
  5. UIBase = (baidu.editor.ui.UIBase = function() {});
  6. UIBase.prototype = {
  7. className: "",
  8. uiName: "",
  9. initOptions: function(options) {
  10. var me = this;
  11. for (var k in options) {
  12. me[k] = options[k];
  13. }
  14. this.id = this.id || "edui" + uiUtils.uid();
  15. },
  16. initUIBase: function() {
  17. this._globalKey = utils.unhtml(uiUtils.setGlobal(this.id, this));
  18. },
  19. render: function(holder) {
  20. var html = this.renderHtml();
  21. var el = uiUtils.createElementByHtml(html);
  22. //by xuheng 给每个node添加class
  23. var list = domUtils.getElementsByTagName(el, "*");
  24. var theme = "edui-" + (this.theme || this.editor.options.theme);
  25. var layer = document.getElementById("edui_fixedlayer");
  26. for (var i = 0, node; (node = list[i++]); ) {
  27. domUtils.addClass(node, theme);
  28. }
  29. domUtils.addClass(el, theme);
  30. if (layer) {
  31. layer.className = "";
  32. domUtils.addClass(layer, theme);
  33. }
  34. var seatEl = this.getDom();
  35. if (seatEl != null) {
  36. seatEl.parentNode.replaceChild(el, seatEl);
  37. uiUtils.copyAttributes(el, seatEl);
  38. } else {
  39. if (typeof holder == "string") {
  40. holder = document.getElementById(holder);
  41. }
  42. holder = holder || uiUtils.getFixedLayer();
  43. domUtils.addClass(holder, theme);
  44. holder.appendChild(el);
  45. }
  46. this.postRender();
  47. },
  48. getDom: function(name) {
  49. if (!name) {
  50. return document.getElementById(this.id);
  51. } else {
  52. return document.getElementById(this.id + "_" + name);
  53. }
  54. },
  55. postRender: function() {
  56. this.fireEvent("postrender");
  57. },
  58. getHtmlTpl: function() {
  59. return "";
  60. },
  61. formatHtml: function(tpl) {
  62. var prefix = "edui-" + this.uiName;
  63. return tpl
  64. .replace(/##/g, this.id)
  65. .replace(/%%-/g, this.uiName ? prefix + "-" : "")
  66. .replace(/%%/g, (this.uiName ? prefix : "") + " " + this.className)
  67. .replace(/\$\$/g, this._globalKey);
  68. },
  69. renderHtml: function() {
  70. return this.formatHtml(this.getHtmlTpl());
  71. },
  72. dispose: function() {
  73. var box = this.getDom();
  74. if (box) baidu.editor.dom.domUtils.remove(box);
  75. uiUtils.unsetGlobal(this.id);
  76. }
  77. };
  78. utils.inherits(UIBase, EventBase);
  79. })();