ui.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /**
  2. * Created by hn on 14-3-17.
  3. */
  4. define( function ( require, exports, modules ) {
  5. var kity = require( "kity"),
  6. Utils = require( "base/utils" ),
  7. Toolbar = require( "ui/toolbar/toolbar" ),
  8. // 控制组件
  9. ScrollZoom = require( "ui/control/zoom" ),
  10. ELEMENT_LIST = require( "ui/toolbar-ele-list" ),
  11. UIComponent = kity.createClass( 'UIComponent', {
  12. constructor: function ( kfEditor ) {
  13. var currentDocument = null;
  14. this.container = kfEditor.getContainer();
  15. currentDocument = this.container.ownerDocument;
  16. // ui组件实例集合
  17. this.components = {};
  18. this.kfEditor = kfEditor;
  19. this.resizeTimer = null;
  20. this.toolbarContainer = createToolbarContainer( currentDocument );
  21. this.editArea = createEditArea( currentDocument );
  22. this.canvasContainer = createCanvasContainer( currentDocument );
  23. this.updateContainerSize( this.container, this.toolbarContainer, this.editArea, this.canvasContainer );
  24. this.container.appendChild( this.toolbarContainer );
  25. this.editArea.appendChild( this.canvasContainer );
  26. this.container.appendChild( this.editArea );
  27. this.initCanvas();
  28. this.initComponents();
  29. this.initServices();
  30. this.initResizeEvent();
  31. },
  32. // 组件实例化
  33. initComponents: function () {
  34. // 工具栏组件
  35. this.components.toolbar = new Toolbar( this.kfEditor, this, ELEMENT_LIST );
  36. this.components.scrollZoom = new ScrollZoom( this, this.kfEditor, this.canvasContainer );
  37. },
  38. initCanvas: function () {
  39. },
  40. updateContainerSize: function ( container, toolbar, editArea, canvasContainer ) {
  41. var containerBox = container.getBoundingClientRect();
  42. toolbar.style.width = containerBox.width - 12 + "px";
  43. toolbar.style.height = 80 + "px";
  44. editArea.style.marginTop = 80 + "px";
  45. editArea.style.width = containerBox.width + "px";
  46. editArea.style.height = containerBox.height - 80 + "px";
  47. },
  48. // 初始化服务
  49. initServices: function () {
  50. this.kfEditor.registerService( "ui.get.canvas.container", this, {
  51. getCanvasContainer: SERVICE_LIST.getCanvasContainer
  52. } );
  53. this.kfEditor.registerService( "ui.canvas.container.event", this, {
  54. on: SERVICE_LIST.addEvent,
  55. off: SERVICE_LIST.removeEvent,
  56. trigger: SERVICE_LIST.trigger,
  57. fire: SERVICE_LIST.trigger
  58. } );
  59. },
  60. initResizeEvent: function () {
  61. var _self = this;
  62. this.canvasContainer.ownerDocument.defaultView.onresize = function () {
  63. window.clearTimeout( _self.resizeTimer );
  64. _self.resizeTimer = window.setTimeout( function () {
  65. _self.kfEditor.requestService( "render.relocation" );
  66. }, 80 );
  67. };
  68. }
  69. } ),
  70. SERVICE_LIST = {
  71. getCanvasContainer: function () {
  72. return this.canvasContainer;
  73. },
  74. addEvent: function ( type, handler ) {
  75. Utils.addEvent( this.canvasContainer, type, handler );
  76. },
  77. removeEvent: function () {},
  78. trigger: function ( type ) {
  79. Utils.trigger( this.canvasContainer, type );
  80. }
  81. };
  82. function createToolbarContainer ( doc ) {
  83. var container = doc.createElement( "div" );
  84. container.className = "kf-editor-toolbar";
  85. return container;
  86. }
  87. function createEditArea ( doc ) {
  88. var container = doc.createElement( "div" );
  89. container.className = "kf-editor-edit-area";
  90. container.style.width = "80%";
  91. container.style.height = "800px";
  92. return container;
  93. }
  94. function createCanvasContainer ( doc ) {
  95. var container = doc.createElement( "div" );
  96. container.className = "kf-editor-canvas-container";
  97. return container;
  98. }
  99. return UIComponent;
  100. } );