Toolbar.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. o2.widget = o2.widget || {};
  2. o2.widget.Toolbar = new Class({
  3. Implements: [Options, Events],
  4. Extends: o2.widget.Common,
  5. options: {
  6. "style": "default"
  7. },
  8. initialize: function(container, options, bindObject){
  9. this.setOptions(options);
  10. this.bindObject = bindObject;
  11. this.items = [];
  12. this.children = [];
  13. this.childrenButton = [];
  14. this.childrenMenu = [];
  15. this.path = o2.session.path+"/widget/$Toolbar/";
  16. this.cssPath = o2.session.path+"/widget/$Toolbar/"+this.options.style+"/css.wcss";
  17. this._loadCss();
  18. this.node = $(container);
  19. this.node.onselectstart = function (){return false;};
  20. this.node.oncontextmenu = function (){return false;};
  21. },
  22. load: function(){
  23. if (this.fireEvent("queryLoad")){
  24. this.node.set("styles", this.css.container);
  25. this._loadToolbarItemNode();
  26. this._loadToolbarItems();
  27. this.fireEvent("postLoad");
  28. }
  29. },
  30. _loadToolbarItemNode: function(){
  31. var subNodes = this.node.getChildren();
  32. subNodes.each(function(node, idx){
  33. var type = node.get("MWFnodetype");
  34. if (type){
  35. if (typeOf(this[type])=="array"){
  36. this[type].push(node);
  37. }else{
  38. this[type] = [];
  39. this[type].push(node);
  40. }
  41. }
  42. }.bind(this));
  43. },
  44. _loadToolbarItems: function(){
  45. this._loadToolBarSeparator(this.MWFToolBarSeparator);
  46. this._loadToolBarButton(this.MWFToolBarButton);
  47. this._loadToolBarOnOffButton(this.MWFToolBarOnOffButton);
  48. this._loadToolBarMenu(this.MWFToolBarMenu);
  49. },
  50. _loadToolBarSeparator: function(nodes){
  51. if (nodes) {
  52. nodes.each(function(node, idx){
  53. node.set("styles", this.css.toolbarSeparator);
  54. }.bind(this));
  55. }
  56. },
  57. _loadToolBarButton: function(nodes){
  58. if (nodes) {
  59. nodes.each(function(node, idx){
  60. var btn = new o2.widget.ToolbarButton(node, this, this.options);
  61. btn.load();
  62. this.fireEvent("buttonLoad", [btn]);
  63. if (btn.buttonID){
  64. this.items[btn.buttonID] = btn;
  65. }
  66. this.children.push(btn);
  67. this.childrenButton.push(btn);
  68. }.bind(this));
  69. }
  70. },
  71. _loadToolBarOnOffButton: function(nodes){
  72. if (nodes) {
  73. nodes.each(function(node, idx){
  74. var btn = new o2.widget.ToolbarOnOffButton(node, this, this.options);
  75. btn.load();
  76. this.fireEvent("buttonLoad", [btn]);
  77. if (btn.buttonID){
  78. this.items[btn.buttonID] = btn;
  79. }
  80. this.children.push(btn);
  81. this.childrenButton.push(btn);
  82. }.bind(this));
  83. }
  84. },
  85. _loadToolBarMenu: function(nodes){
  86. var _self = this;
  87. if (nodes) {
  88. nodes.each(function(node, idx){
  89. var btn = new o2.widget.ToolbarMenu(node, this, {
  90. "onAddMenuItem": function(item){
  91. this.fireEvent("addMenuItem", [item]);
  92. }.bind(this),
  93. "onMenuQueryShow": function(menu){
  94. _self.fireEvent("menuQueryShow", [this, menu]);
  95. },
  96. "onMenuPostShow": function(menu){
  97. _self.fireEvent("menuPostShow", [this, menu]);
  98. },
  99. "onMenuQueryHide": function(menu){
  100. _self.fireEvent("menuQueryHide", [this, menu]);
  101. },
  102. "onMenuPostHide": function(menu){
  103. _self.fireEvent("menuPostHide", [this, menu]);
  104. },
  105. "onLoad": function(menu){
  106. _self.fireEvent("menuLoaded", [this, menu]);
  107. }
  108. });
  109. btn.load();
  110. this.fireEvent("menuLoad", [btn]);
  111. if (btn.buttonID){
  112. this.items[btn.buttonID] = btn;
  113. }
  114. this.children.push(btn);
  115. this.childrenMenu.push(btn);
  116. }.bind(this));
  117. }
  118. }
  119. });
  120. o2.widget.ToolbarButton = new Class({
  121. Implements: [Options, Events],
  122. options: {
  123. "text": "",
  124. "title": "",
  125. "pic": "",
  126. "action": "",
  127. "actionScript": "",
  128. "disable": false
  129. },
  130. initialize: function(node, toolbar, options){
  131. this.setOptions(options);
  132. this.node = $(node);
  133. this.toolbar = toolbar;
  134. this.buttonID = this.node.get("MWFnodeid") || this.node.get("id");
  135. if (!this.node){
  136. this.node = new Element("div").inject(this.toolbar.node);
  137. }else{
  138. var buttonText = this.node.get("MWFButtonText");
  139. if (buttonText) this.options.text = buttonText;
  140. var title = this.node.get("title");
  141. if (title) this.options.title = title;
  142. var buttonImage = this.node.get("MWFButtonImage");
  143. if (buttonImage) this.options.pic = buttonImage;
  144. var buttonDisable = this.node.get("MWFButtonDisable");
  145. if (buttonDisable) this.options.disable = true;
  146. var buttonAction = this.node.get("MWFButtonAction");
  147. if (buttonAction) this.options.action = buttonAction;
  148. //var buttonActionScript = this.node.get("MWFButtonActionScript");
  149. //if (buttonActionScript) this.options.actionScript = buttonActionScript;
  150. }
  151. this.modifiyStyle = true;
  152. },
  153. load: function(){
  154. this._addButtonEvent();
  155. this.node.title = this.options.title;
  156. this.node.set("styles", this.toolbar.css.button);
  157. if (this.options.pic) this.picNode = this._createImageNode(this.options.pic);
  158. if (this.options.text) this.textNode = this._createTextNode(this.options.text);
  159. this.setDisable(this.options.disable);
  160. },
  161. enable: function(){
  162. if (this.options.disable){
  163. this.setDisable(false);
  164. this.options.disable = false;
  165. }
  166. },
  167. disable: function(){
  168. if (!this.options.disable){
  169. this.setDisable(true);
  170. this.options.disable = true;
  171. }
  172. },
  173. setDisable: function(flag){
  174. if (flag){
  175. if (!this.options.disable){
  176. this.options.disable = true;
  177. this.node.set("styles", this.toolbar.css.buttonDisable);
  178. if (this.picNode){
  179. this.picNode.set("styles", this.toolbar.css.buttonImgDivDisable);
  180. var img = this.picNode.getElement("img");
  181. var src = img.get("src");
  182. var ext = src.substr(src.lastIndexOf("."), src.length);
  183. src = src.substr(0, src.lastIndexOf("."));
  184. src = src+"_gray"+ext;
  185. img.set("src", src);
  186. }
  187. if (this.textNode) this.textNode.set("styles", this.toolbar.css.buttonTextDivDisable);
  188. }
  189. }else{
  190. if (this.options.disable){
  191. this.options.disable = false;
  192. this.node.set("styles", this.toolbar.css.button);
  193. if (this.picNode){
  194. this.picNode.set("styles", this.toolbar.css.buttonImgDiv);
  195. var img = this.picNode.getElement("img");
  196. var src = img.get("src");
  197. src = src.replace("_gray", "");
  198. img.set("src", src);
  199. }
  200. if (this.textNode) this.textNode.set("styles", this.toolbar.css.buttonTextDiv);
  201. }
  202. }
  203. },
  204. _createImageNode: function(src){
  205. if (src){
  206. var div = new Element("span", {"styles": this.toolbar.css.buttonImgDiv}).inject(this.node);
  207. var img = new Element("img", {
  208. "styles": this.toolbar.css.buttonImg,
  209. "src": src
  210. }).inject(div);
  211. return div;
  212. }else{
  213. return null;
  214. }
  215. },
  216. _createTextNode: function(text){
  217. if (text){
  218. var div = new Element("span", {
  219. "styles": this.toolbar.css.buttonTextDiv,
  220. "text": text
  221. }).inject(this.node);
  222. return div;
  223. }else{
  224. return null;
  225. }
  226. },
  227. setText: function(text){
  228. this.node.getLast().set("text", text);
  229. },
  230. _addButtonEvent: function(){
  231. this.node.addEvent("mouseover", this._buttonMouseOver.bind(this));
  232. this.node.addEvent("mouseout", this._buttonMouseOut.bind(this));
  233. this.node.addEvent("mousedown", this._buttonMouseDown.bind(this));
  234. this.node.addEvent("mouseup", this._buttonMouseUp.bind(this));
  235. this.node.addEvent("click", this._buttonClick.bind(this));
  236. },
  237. _buttonMouseOver: function(){
  238. if (this.modifiyStyle) if (!this.options.disable){this.node.set("styles", this.toolbar.css.buttonOver);};
  239. },
  240. _buttonMouseOut: function(){
  241. if (this.modifiyStyle) if (!this.options.disable){this.node.set("styles", this.toolbar.css.buttonOut);};
  242. },
  243. _buttonMouseDown: function(){
  244. if (this.modifiyStyle) if (!this.options.disable){this.node.set("styles", this.toolbar.css.buttonDown);};
  245. },
  246. _buttonMouseUp: function(){
  247. if (this.modifiyStyle) if (!this.options.disable){this.node.set("styles", this.toolbar.css.buttonUp);};
  248. },
  249. _buttonClick: function(e){
  250. debugger;
  251. if (!this.options.disable){
  252. if (this.options.action){
  253. if (typeOf(this.options.action)==="string"){
  254. var tmparr = this.options.action.split(":");
  255. var action = tmparr.shift();
  256. var bindObj = (this.toolbar.bindObject) ? this.toolbar.bindObject : window;
  257. if (bindObj[action]){
  258. tmparr.push(this);
  259. tmparr.push(e);
  260. bindObj[action].apply(bindObj,tmparr);
  261. }else{
  262. if (window[action]){
  263. window[action].apply(this,tmparr);
  264. }
  265. }
  266. }else{
  267. this.options.action();
  268. }
  269. }
  270. }
  271. }
  272. });
  273. o2.widget.ToolbarOnOffButton = new Class({
  274. Implements: [Options, Events],
  275. Extends: o2.widget.ToolbarButton,
  276. on: function(){
  277. if (this.status !== "on"){
  278. if (!this.options.disable){this.node.set("styles", this.toolbar.css.buttonDown);}
  279. this.modifiyStyle = false;
  280. this.status = "on";
  281. if (this.textNode) this.textNode.set("styles", this.toolbar.css.buttonTextDivDown);
  282. }
  283. },
  284. off: function(){
  285. if (this.status === "on"){
  286. if (!this.options.disable){this.node.set("styles", this.toolbar.css.buttonOut);}
  287. this.modifiyStyle = true;
  288. this.status = "off";
  289. if (this.textNode) this.textNode.set("styles", this.toolbar.css.buttonTextDiv);
  290. }
  291. },
  292. _buttonClick: function(e){
  293. if (this.status === "on"){
  294. if (!this.options.disable){this.node.set("styles", this.toolbar.css.buttonOut);}
  295. this.modifiyStyle = true;
  296. this.status = "off";
  297. if (this.textNode) this.textNode.set("styles", this.toolbar.css.buttonTextDiv);
  298. }else{
  299. if (!this.options.disable){this.node.set("styles", this.toolbar.css.buttonDown);}
  300. this.modifiyStyle = false;
  301. this.status = "on";
  302. if (this.textNode) this.textNode.set("styles", this.toolbar.css.buttonTextDivDown);
  303. }
  304. if (!this.options.disable){
  305. if (this.options.action){
  306. if (typeOf(this.options.action)==="string"){
  307. var tmparr = this.options.action.split(":");
  308. var action = tmparr.shift();
  309. var bindObj = (this.toolbar.bindObject) ? this.toolbar.bindObject : window;
  310. tmparr.push(this.status);
  311. tmparr.push(this);
  312. if (bindObj[action]){
  313. tmparr.push(this);
  314. tmparr.push(e);
  315. bindObj[action].apply(bindObj,tmparr);
  316. }else{
  317. if (window[action]){
  318. window[action].apply(this,tmparr);
  319. }
  320. }
  321. }else{
  322. this.options.action();
  323. }
  324. }
  325. }
  326. }
  327. });
  328. o2.widget.ToolbarMenu = new Class({
  329. Implements: [Options, Events],
  330. Extends: o2.widget.ToolbarButton,
  331. initialize: function(node, toolbar, options){
  332. this.parent(node, toolbar, options);
  333. this.modifiyStyle = true;
  334. this.menu = null;
  335. this.buttonID = this.node.get("MWFnodeid") || this.node.get("id");
  336. },
  337. setDisable: function(flag){
  338. if (this.menu) this.menu.options.disable = flag;
  339. if (flag){
  340. this.node.set("styles", this.toolbar.css.buttonDisable);
  341. if (this.picNode){
  342. this.picNode.set("styles", this.toolbar.css.buttonImgDivDisable);
  343. var img = this.picNode.getElement("img");
  344. var src = img.get("src");
  345. var ext = src.substr(src.lastIndexOf("."), src.length);
  346. src = src.substr(0, src.lastIndexOf("."));
  347. src = src+"_gray"+ext;
  348. // src = src.substr(0, src.lastIndexOf("."));
  349. // src = src+"_gray.gif";
  350. img.set("src", src);
  351. }
  352. if (this.textNode) this.textNode.set("styles", this.toolbar.css.buttonTextDivDisable);
  353. }else{
  354. this.node.set("styles", this.toolbar.css.button);
  355. if (this.picNode){
  356. this.picNode.set("styles", this.toolbar.css.buttonImgDiv);
  357. var img = this.picNode.getElement("img");
  358. var src = img.get("src");
  359. src = src.replace("_gray", "");
  360. img.set("src", src);
  361. }
  362. if (this.textNode) this.textNode.set("styles", this.toolbar.css.buttonTextDiv);
  363. }
  364. },
  365. load: function(){
  366. this._addButtonEvent();
  367. this.node.title = this.options.title;
  368. this.node.set("styles", this.toolbar.css.button);
  369. if (this.options.pic) this.picNode = this._createImageNode(this.options.pic);
  370. if (this.options.text) this.textNode = this._createTextNode(this.options.text);
  371. this._createDownNode();
  372. var toolbarMenu = this;
  373. o2.require("o2.widget.Menu", function(){
  374. this.menu = new o2.widget.Menu(this.node, {
  375. "style": this.toolbar.options.style,
  376. "bindObject": this.options.bindObject,
  377. "event": "click",
  378. "disable": toolbarMenu.options.disable,
  379. "onQueryShow": function(){
  380. var p = toolbarMenu.node.getPosition(toolbarMenu.node.getOffsetParent());
  381. var s = toolbarMenu.node.getSize();
  382. this.setOptions({
  383. "top": p.y+s.y-2,
  384. "left": p.x
  385. });
  386. toolbarMenu.fireEvent("menuQueryShow", [this]);
  387. return true;
  388. },
  389. "onPostShow": function(){
  390. var p = toolbarMenu.node.getPosition(toolbarMenu.node.getOffsetParent());
  391. var s = toolbarMenu.node.getSize();
  392. toolbarMenu.node.set("styles", {
  393. "background-color": this.node.getStyle("background-color"),
  394. "border-top": this.node.getStyle("border-top"),
  395. "border-right": this.node.getStyle("border-top"),
  396. "border-left": this.node.getStyle("border-top"),
  397. "border-bottom": "0"
  398. });
  399. toolbarMenu.modifiyStyle = false;
  400. toolbarMenu.tmpStyleNode = new Element("div.MWFtmpStyleNode",{
  401. "styles":{
  402. "position":"absolute",
  403. "top": p.y+s.y-2,
  404. "left": p.x+1,
  405. "width": s.x-2,
  406. "z-index": this.node.getStyle("z-index")+1,
  407. "background-color": this.node.getStyle("background-color"),
  408. "height": "1px",
  409. "overflow": "hidden"
  410. }
  411. }).inject(toolbarMenu.node);
  412. toolbarMenu.fireEvent("menuPostShow", [this]);
  413. },
  414. "onQueryHide": function(){
  415. toolbarMenu.fireEvent("menuQueryHide", [this]);
  416. },
  417. "onPostHide": function(){
  418. toolbarMenu.node.set("styles", toolbarMenu.toolbar.css.button);
  419. toolbarMenu.modifiyStyle = true;
  420. if (toolbarMenu.tmpStyleNode) toolbarMenu.tmpStyleNode.destroy();
  421. toolbarMenu.fireEvent("menuPostHide", [this]);
  422. }
  423. });
  424. this.menu.load();
  425. this.fireEvent("load", [this.menu]);
  426. this.addMenuItems();
  427. }.bind(this));
  428. this.setDisable(this.options.disable);
  429. },
  430. _createDownNode: function(){
  431. var spanNode = new Element("div",{
  432. "styles": this.toolbar.css.toolbarButtonDownNode
  433. }).inject(this.node);
  434. spanNode.set("html", "<img src=\""+o2.session.path+"/widget/$Toolbar/"+this.toolbar.options.style+"/downicon.gif"+"\" />");
  435. return spanNode;
  436. },
  437. addMenuItems: function(){
  438. var els = this.node.getChildren();
  439. els.each(function(node, idx){
  440. var type = node.get("MWFnodetype");
  441. if (type=="MWFToolBarMenuItem"){
  442. this._loadMenuItem(node);
  443. }
  444. if (type=="MWFToolBarMenuLine"){
  445. this._loadMenuLine(node);
  446. }
  447. }.bind(this));
  448. },
  449. _loadMenuItem: function(node){
  450. var toolBarMenu = this;
  451. var item = this.menu.addMenuItem(node.get("MWFButtonText"),"click",function(e){toolBarMenu._menuItemClick(node.get("MWFButtonAction"), e, this);}, node.get("MWFButtonImage"), node.get("MWFButtonDisable"));
  452. this.fireEvent("addMenuItem", [item]);
  453. },
  454. _menuItemClick: function(itemAction, e, item){
  455. if (itemAction){
  456. var tmparr = itemAction.split(":");
  457. var action = tmparr.shift();
  458. var bindObj = (this.toolbar.bindObject) ? this.toolbar.bindObject : window;
  459. if (bindObj[action]){
  460. tmparr.push(this);
  461. tmparr.push(e);
  462. tmparr.push(item);
  463. bindObj[action].apply(bindObj,tmparr);
  464. this.menu.hideMenu();
  465. }else{
  466. if (window[action]){
  467. window[action].apply(this,tmparr);
  468. this.menu.hideMenu();
  469. }
  470. }
  471. }
  472. },
  473. _loadMenuLine: function(){
  474. this.menu.addMenuLine();
  475. }
  476. });