plugin.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. window.Tracker = window.Tracker || {};
  2. Tracker.Plugins = function(){
  3. var klass, instCache;
  4. klass = function( name ){
  5. instCache[ name ] = this;
  6. this.activeFuze = Tracker.Promise.fuze();
  7. this.startUpFuze = Tracker.Promise.fuze();
  8. };
  9. klass.prototype = Tracker.Event.bind( {
  10. onStartUp: function( fn ){
  11. // this.startUpFuze( Tracker.Util.bind( fn, this ) );
  12. Tracker.Plugins.onControllerLoad( Tracker.Util.bind( fn, this ) );
  13. },
  14. onActive: function( fn ){
  15. if( this.activeFuze.fired )
  16. this.activeFuze( fn );
  17. this.on( "active", fn );
  18. }
  19. } );
  20. instCache = {};
  21. return {
  22. // prepare for a plugin, use in this script.
  23. prepare: function( config ){
  24. var name, type, define, inst, label, bodyId, setuped, started;
  25. name = config.name;
  26. type = config.type;
  27. define = config.define; // plugin define source
  28. inst = new klass( name );
  29. if( type == "TopPanel" ){
  30. label = config.label;
  31. bodyId = config.bodyId;
  32. this.addPanel( name, label, function(){
  33. this.innerHTML = "<div id='" + bodyId + "'></div>";
  34. inst.body = this.firstChild;
  35. }, Tracker.Util.bind( function(){
  36. if( !setuped )
  37. setuped = true,
  38. define && this.setup( define );
  39. // if( !started )
  40. // started = true,
  41. // inst.startUpFuze.fire();
  42. if( !inst.actived )
  43. inst.activeFuze.fire(),
  44. inst.fire( "active" );
  45. }, this ) );
  46. }
  47. },
  48. // define a plugin, use in a separate script.
  49. addOn: function( name, fn ){
  50. var inst, window, document;
  51. window = Tracker.View.ControlFrame.getWindow( "tracker_controller" );
  52. document = window.document;
  53. if( inst = instCache[ name ] )
  54. fn.call( inst, window, document );
  55. else
  56. throw new Error( "illegal plugin name." );
  57. },
  58. // for appending a style tag to 'ControlPanel' view.
  59. addStyle: function( text ){
  60. this.onControllerLoad( function( window, document ){
  61. var style;
  62. style = document.createElement( "style" );
  63. style.type = "text/css";
  64. style.appendChild( document.createTextNode( text ) );
  65. document.head.appendChild( style );
  66. } );
  67. },
  68. // for appending a top bar item to 'ControlPanel' view.
  69. addPanel: function( name, title, panelDefine, activeHandler ){
  70. if( typeof title == "function" ){ // missing argument 'name'
  71. activeHandler = panelDefine;
  72. panelDefine = title;
  73. title = name;
  74. name = null;
  75. }
  76. this.onControllerLoad( function( window, document ){
  77. var topNav, panels, titleEl, panelEl, meIndex;
  78. topNav = document.getElementById( "top-nav" );
  79. panels = document.getElementById( topNav.getAttribute( "data-target" ) );
  80. titleEl = document.createElement( "li" );
  81. titleEl.className = "relative";
  82. if( name )
  83. titleEl.setAttribute( "data-name", name );
  84. titleEl.innerHTML = "<a href='' onclick='return false'>" + title + "</a>";
  85. topNav.appendChild( titleEl );
  86. panelEl = document.createElement( "li" );
  87. panels.appendChild( panelEl );
  88. meIndex = topNav.childNodes.length - 1;
  89. if( activeHandler )
  90. topNav.tabEvent.on( "active", function( index ){
  91. if( index === meIndex )
  92. activeHandler();
  93. } );
  94. panelDefine.call( panelEl, window, document );
  95. } );
  96. },
  97. // for setup a plugin.
  98. setup: function( pluginSrc ){
  99. var script;
  100. script = document.createElement( "script" );
  101. script.charset = "utf-8";
  102. script.type = "text/javascript";
  103. script.src = pluginSrc;
  104. document.head.appendChild( script );
  105. },
  106. // ::: privates :::
  107. onControllerLoad: function( fn ){
  108. var f = function(){
  109. var window, document;
  110. window = Tracker.View.ControlFrame.getWindow( "tracker_controller" );
  111. document = window.document;
  112. fn.call( this, window, document );
  113. };
  114. if( Tracker.controllerOnLoad.fired )
  115. Tracker.controllerOnLoad( f );
  116. Tracker.View.ControlFrame.on( "controllerLoad", f );
  117. }
  118. }
  119. }();