uibase.js 2.8 KB

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