ScriptHelp.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. o2.widget = o2.widget || {};
  2. o2.require("o2.widget.Menu", null, false);
  3. o2.widget.ScriptHelpCodes = {};
  4. o2.widget.ScriptHelp = new Class({
  5. Implements: [Options, Events],
  6. Extends: o2.widget.Common,
  7. options: {
  8. "code": "code.json",
  9. "style": "default"
  10. },
  11. initialize: function(node, editor, options){
  12. this.setOptions(options);
  13. this.node = $(node);
  14. this.editor = editor;
  15. this.path = o2.session.path+"/widget/$ScriptHelp/";
  16. this.cssPath = o2.session.path+"/widget/$ScriptHelp/"+this.options.style+"/css.wcss";
  17. this.load();
  18. },
  19. getEditor: function(){
  20. return null;
  21. },
  22. load: function(){
  23. this.menu = new o2.widget.ScriptHelp.Menu(this.node, {"event": "click", "style": "script"});
  24. this.menu.scriptHelp = this;
  25. this.menu.load();
  26. if (!o2.widget.ScriptHelpCodes[this.path+this.options.style+"/"+this.options.code]){
  27. o2.getJSON(this.path+this.options.style+"/"+this.options.code, function(json){
  28. this.codeJson = json;
  29. this.loadMenuItems(this.codeJson, this.menu);
  30. o2.widget.ScriptHelpCodes[this.path+this.options.style+"/"+this.options.code] = json;
  31. this.fireEvent("postLoad");
  32. }.bind(this), true, true, false);
  33. }else{
  34. this.codeJson = o2.widget.ScriptHelpCodes[this.path+this.options.style+"/"+this.options.code];
  35. this.loadMenuItems(this.codeJson, this.menu);
  36. this.fireEvent("postLoad");
  37. }
  38. },
  39. loadMenuItems: function(json, menu){
  40. json.each(function(code){
  41. if (code=="-"){
  42. menu.addMenuLine();
  43. }else{
  44. if (typeOf(code.value)=="string"){
  45. menu.addMenuItem(code.name, "click", function(){
  46. var editor = this.getEditor();
  47. if (editor){
  48. editor.insert(code.value);
  49. editor.focus();
  50. }
  51. }.bind(this));
  52. }else{
  53. var subMenu = new o2.widget.ScriptHelp.Menu(this.node, {"style": "script"});
  54. subMenu.load();
  55. this.loadMenuItems(code.value, subMenu);
  56. menu.addMenuMenu(code.name, "", subMenu);
  57. }
  58. }
  59. }.bind(this));
  60. },
  61. show: function(){
  62. this.menu.showIm();
  63. }
  64. });
  65. o2.widget.ScriptHelp.Menu = new Class({
  66. Extends: o2.widget.Menu,
  67. showIm: function(e){
  68. if (!this.options.disable){
  69. this.hide = this.hideIm.bind(this);
  70. if (this.fireEvent("queryShow", [e])){
  71. this.tmpBodyOncontextmenu = document.body.oncontextmenu;
  72. document.body.oncontextmenu = function(){return false;};
  73. if (this.pauseCount<=0){
  74. this.setItemWidth();
  75. this.node.setStyles({
  76. "display": "block",
  77. "opacity": this.options.opacity || 1
  78. });
  79. this.setPosition(e);
  80. $(document.body).removeEvent("mousedown", this.hide);
  81. $(document.body).addEvent("mousedown", this.hide);
  82. this.show = true;
  83. }else{
  84. this.pauseCount--;
  85. }
  86. this.node.focus();
  87. if (!this.isSetKeyEvents) this.setKeyEvents();
  88. this.fireEvent("postShow");
  89. }
  90. }
  91. },
  92. setKeyEvents: function(){
  93. // this.node.addEvents({
  94. // "keydown:keys(down)": function(e){this.keyDown(e);}.bind(this),
  95. // "keydown:keys(up)": function(e){this.keyUp(e);}.bind(this),
  96. // "keydown:keys(left)": function(e){this.keyLeft(e);}.bind(this),
  97. // "keydown:keys(right)": function(e){this.keyRight(e);}.bind(this),
  98. // "keydown:keys(esc)": function(e){this.keyEsc(e);}.bind(this),
  99. // "keydown:keys(space)": function(e){this.keyEnter(e);}.bind(this),
  100. // "keydown:keys(enter)": function(e){this.keyEnter(e);}.bind(this),
  101. //// "keydown": function(e){this.keyEsc(e);}.bind(this)
  102. // });
  103. //this.node.addEvent("keydown:keys(down)", function(e){this.keyMenuDown(e);}.bind(this));
  104. //this.node.addEvent("keydown:keys(up)", function(e){this.keyMenuUp(e);}.bind(this));
  105. //this.node.addEvent("keydown:keys(left)", function(e){this.keyMenuLeft(e);}.bind(this));
  106. //this.node.addEvent("keydown:keys(right)", function(e){this.keyMenuRight(e);}.bind(this));
  107. //this.node.addEvent("keydown:keys(esc)", function(e){this.keyMenuEsc(e);}.bind(this));
  108. //this.node.addEvent("keydown:keys(space)", function(e){this.keyMenuEnter(e);}.bind(this));
  109. //this.node.addEvent("keydown:keys(enter)", function(e){this.keyMenuEnter(e);}.bind(this));
  110. this.node.addEvent("keydown", function(e){this.keyMenuAction(e);}.bind(this));
  111. this.isSetKeyEvents = true;
  112. },
  113. keyMenuAction: function(e){
  114. switch (e.key){
  115. case "down":
  116. this.keyMenuDown(e);
  117. break;
  118. case "up":
  119. this.keyMenuUp(e);
  120. break;
  121. case "left":
  122. this.keyMenuLeft(e);
  123. break;
  124. case "right":
  125. this.keyMenuRight(e);
  126. break;
  127. case "esc":
  128. this.keyMenuEsc(e);
  129. break;
  130. case "space":
  131. this.keyMenuEnter(e);
  132. break;
  133. case "enter":
  134. this.keyMenuEnter(e);
  135. break;
  136. default:
  137. }
  138. },
  139. keyMenuDown: function(e){
  140. if (!this.current){
  141. this.items[0]._menuItemMouseOver(e);
  142. }else{
  143. idx = this.items.indexOf(this.current);
  144. idx++;
  145. while (idx<this.items.length && this.items[idx].type=="line"){
  146. idx++;
  147. }
  148. var item = this.items[idx];
  149. if ((idx)>=this.items.length) item = this.items[0];
  150. item._menuItemMouseOver(e);
  151. if (item.type=="menu") this.node.focus();
  152. }
  153. },
  154. keyMenuUp: function(e){
  155. if (!this.current){
  156. this.items[this.items.length-1]._menuItemMouseOver(e);
  157. }else{
  158. idx = this.items.indexOf(this.current);
  159. idx--;
  160. while (idx>=0 && this.items[idx].type=="line"){
  161. idx--;
  162. }
  163. var item = this.items[idx];
  164. if ((idx)<0) item = this.items[this.items.length-1];
  165. item._menuItemMouseOver(e);
  166. if (item.type=="menu") this.node.focus();
  167. }
  168. },
  169. keyMenuRight: function(e){
  170. if (this.current){
  171. if (this.current.type=="menu"){
  172. this.current.subMenu.showIm();
  173. this.current.subMenu.node.focus();
  174. this.current.subMenu.current = null;
  175. this.current.subMenu.keyMenuDown();
  176. }
  177. }
  178. },
  179. keyMenuLeft: function(e){
  180. if (this.topMenu){
  181. this.hideIm();
  182. this.topMenu.node.focus();
  183. }
  184. },
  185. keyMenuEsc: function(){
  186. if (this.topMenu){
  187. this.hideIm();
  188. this.topMenu.node.focus();
  189. }else{
  190. this.hideIm();
  191. this.scriptHelp.getEditor().focus();
  192. }
  193. },
  194. keyMenuEnter: function(e){
  195. if (this.current) this.current.doAction();
  196. e.stopPropagation();
  197. }
  198. });