uibase.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. var seatEl = this.getDom();
  23. if (seatEl != null) {
  24. seatEl.parentNode.replaceChild(el, seatEl);
  25. uiUtils.copyAttributes(el, seatEl);
  26. } else {
  27. if (typeof holder == 'string') {
  28. holder = document.getElementById(holder);
  29. }
  30. holder = holder || uiUtils.getFixedLayer();
  31. holder.appendChild(el);
  32. }
  33. this.postRender();
  34. },
  35. getDom: function (name){
  36. if (!name) {
  37. return document.getElementById( this.id );
  38. } else {
  39. return document.getElementById( this.id + '_' + name );
  40. }
  41. },
  42. postRender: function (){
  43. this.fireEvent('postrender');
  44. },
  45. getHtmlTpl: function (){
  46. return '';
  47. },
  48. formatHtml: function (tpl){
  49. var prefix = 'edui-' + this.uiName;
  50. return (tpl
  51. .replace(/##/g, this.id)
  52. .replace(/%%-/g, this.uiName ? prefix + '-' : '')
  53. .replace(/%%/g, (this.uiName ? prefix : '') + ' ' + this.className)
  54. .replace(/\$\$/g, this._globalKey));
  55. },
  56. renderHtml: function (){
  57. return this.formatHtml(this.getHtmlTpl());
  58. },
  59. dispose: function (){
  60. var box = this.getDom();
  61. if (box) baidu.editor.dom.domUtils.remove( box );
  62. uiUtils.unsetGlobal(this.id);
  63. }
  64. };
  65. utils.inherits(UIBase, EventBase);
  66. })();