Toolbar.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  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. "hide": false
  130. },
  131. initialize: function(node, toolbar, options){
  132. this.setOptions(options);
  133. this.node = $(node);
  134. this.toolbar = toolbar;
  135. this.buttonID = this.node.get("MWFnodeid") || this.node.get("id");
  136. if (!this.node){
  137. this.node = new Element("div").inject(this.toolbar.node);
  138. }else{
  139. var buttonText = this.node.get("MWFButtonText");
  140. if (buttonText) this.options.text = buttonText;
  141. var title = this.node.get("title");
  142. if (title) this.options.title = title;
  143. var buttonImage = this.node.get("MWFButtonImage");
  144. if (buttonImage) this.options.pic = buttonImage;
  145. var buttonImageOver = this.node.get("MWFButtonImageOver");
  146. if (buttonImageOver) this.options.pic_over = buttonImageOver;
  147. var buttonDisable = this.node.get("MWFButtonDisable");
  148. if (buttonDisable) this.options.disable = true;
  149. var buttonAction = this.node.get("MWFButtonAction");
  150. if (buttonAction) this.options.action = buttonAction;
  151. var buttonHide = this.node.get("MWFHide");
  152. this.options.hide = !!buttonHide;
  153. //var buttonActionScript = this.node.get("MWFButtonActionScript");
  154. //if (buttonActionScript) this.options.actionScript = buttonActionScript;
  155. }
  156. this.modifiyStyle = true;
  157. },
  158. load: function(){
  159. this._addButtonEvent();
  160. this.node.title = this.options.title;
  161. this.node.set("styles", this.toolbar.css.button);
  162. if (this.options.pic) this.picNode = this._createImageNode(this.options.pic);
  163. if (this.options.text) this.textNode = this._createTextNode(this.options.text);
  164. this.setDisable(this.options.disable);
  165. this.setHide(this.options.hide);
  166. },
  167. hide: function(){
  168. this.node.hide();
  169. this.options.hide = true;
  170. },
  171. show: function(){
  172. this.node.show();
  173. this.options.hide = false;
  174. },
  175. setHide: function(flag){
  176. if (flag){
  177. this.hide();
  178. }else{
  179. this.show();
  180. }
  181. },
  182. enable: function(){
  183. if (this.options.disable){
  184. this.setDisable(false);
  185. this.options.disable = false;
  186. }
  187. },
  188. disable: function(){
  189. if (!this.options.disable){
  190. this.setDisable(true);
  191. this.options.disable = true;
  192. }
  193. },
  194. setDisable: function(flag){
  195. if (flag){
  196. if (!this.options.disable){
  197. this.options.disable = true;
  198. this.node.set("styles", this.toolbar.css.buttonDisable);
  199. if (this.picNode){
  200. this.picNode.set("styles", this.toolbar.css.buttonImgDivDisable);
  201. var img = this.picNode.getElement("img");
  202. var src = img.get("src");
  203. var ext = src.substr(src.lastIndexOf("."), src.length);
  204. src = src.substr(0, src.lastIndexOf("."));
  205. src = src+"_gray"+ext;
  206. img.set("src", src);
  207. }
  208. if (this.textNode) this.textNode.set("styles", this.toolbar.css.buttonTextDivDisable);
  209. }
  210. }else{
  211. if (this.options.disable){
  212. this.options.disable = false;
  213. this.node.set("styles", this.toolbar.css.button);
  214. if (this.picNode){
  215. this.picNode.set("styles", this.toolbar.css.buttonImgDiv);
  216. var img = this.picNode.getElement("img");
  217. var src = img.get("src");
  218. src = src.replace("_gray", "");
  219. img.set("src", src);
  220. }
  221. if (this.textNode) this.textNode.set("styles", this.toolbar.css.buttonTextDiv);
  222. }
  223. }
  224. },
  225. _createImageNode: function(src){
  226. if (src){
  227. var div = new Element("span", {"styles": this.toolbar.css.buttonImgDiv}).inject(this.node);
  228. var img = this.imgNode = new Element("img", {
  229. "styles": this.toolbar.css.buttonImg,
  230. "src": src
  231. }).inject(div);
  232. return div;
  233. }else{
  234. return null;
  235. }
  236. },
  237. _createTextNode: function(text){
  238. if (text){
  239. var div = new Element("span", {
  240. "styles": this.toolbar.css.buttonTextDiv,
  241. "text": text
  242. }).inject(this.node);
  243. return div;
  244. }else{
  245. return null;
  246. }
  247. },
  248. setText: function(text){
  249. this.node.getLast().set("text", text);
  250. },
  251. _addButtonEvent: function(){
  252. this.node.addEvent("mouseover", this._buttonMouseOver.bind(this));
  253. this.node.addEvent("mouseout", this._buttonMouseOut.bind(this));
  254. this.node.addEvent("mousedown", this._buttonMouseDown.bind(this));
  255. this.node.addEvent("mouseup", this._buttonMouseUp.bind(this));
  256. this.node.addEvent("click", this._buttonClick.bind(this));
  257. },
  258. _buttonMouseOver: function(){
  259. if (this.modifiyStyle) if (!this.options.disable){this.node.set("styles", this.toolbar.css.buttonOver);};
  260. if( this.options.pic_over )if (!this.options.disable && this.imgNode){this.imgNode.set("src", this.options.pic_over);};
  261. },
  262. _buttonMouseOut: function(){
  263. if (this.modifiyStyle) if (!this.options.disable){this.node.set("styles", this.toolbar.css.buttonOut);};
  264. if( this.options.pic_over )if (!this.options.disable && this.imgNode){this.imgNode.set("src", this.options.pic);};
  265. },
  266. _buttonMouseDown: function(){
  267. if (this.modifiyStyle) if (!this.options.disable){this.node.set("styles", this.toolbar.css.buttonDown);};
  268. },
  269. _buttonMouseUp: function(){
  270. if (this.modifiyStyle) if (!this.options.disable){this.node.set("styles", this.toolbar.css.buttonUp);};
  271. },
  272. _buttonClick: function(e){
  273. if (!this.options.disable){
  274. if (this.options.action){
  275. if (typeOf(this.options.action)==="string"){
  276. var tmparr = this.options.action.split(":");
  277. var action = tmparr.shift();
  278. var bindObj = (this.toolbar.bindObject) ? this.toolbar.bindObject : window;
  279. if (bindObj[action]){
  280. tmparr.push(this);
  281. tmparr.push(e);
  282. bindObj[action].apply(bindObj,tmparr);
  283. }else{
  284. if (window[action]){
  285. window[action].apply(this,tmparr);
  286. }
  287. }
  288. }else{
  289. this.options.action();
  290. }
  291. }
  292. }
  293. }
  294. });
  295. o2.widget.ToolbarOnOffButton = new Class({
  296. Implements: [Options, Events],
  297. Extends: o2.widget.ToolbarButton,
  298. on: function(){
  299. if (this.status !== "on"){
  300. if (!this.options.disable){this.node.set("styles", this.toolbar.css.buttonDown);}
  301. this.modifiyStyle = false;
  302. this.status = "on";
  303. if (this.textNode) this.textNode.set("styles", this.toolbar.css.buttonTextDivDown);
  304. }
  305. },
  306. off: function(){
  307. if (this.status === "on"){
  308. if (!this.options.disable){this.node.set("styles", this.toolbar.css.buttonOut);}
  309. this.modifiyStyle = true;
  310. this.status = "off";
  311. if (this.textNode) this.textNode.set("styles", this.toolbar.css.buttonTextDiv);
  312. }
  313. },
  314. _buttonClick: function(e){
  315. if (this.status === "on"){
  316. if (!this.options.disable){this.node.set("styles", this.toolbar.css.buttonOut);}
  317. this.modifiyStyle = true;
  318. this.status = "off";
  319. if (this.textNode) this.textNode.set("styles", this.toolbar.css.buttonTextDiv);
  320. }else{
  321. if (!this.options.disable){this.node.set("styles", this.toolbar.css.buttonDown);}
  322. this.modifiyStyle = false;
  323. this.status = "on";
  324. if (this.textNode) this.textNode.set("styles", this.toolbar.css.buttonTextDivDown);
  325. }
  326. if (!this.options.disable){
  327. if (this.options.action){
  328. if (typeOf(this.options.action)==="string"){
  329. var tmparr = this.options.action.split(":");
  330. var action = tmparr.shift();
  331. var bindObj = (this.toolbar.bindObject) ? this.toolbar.bindObject : window;
  332. tmparr.push(this.status);
  333. tmparr.push(this);
  334. if (bindObj[action]){
  335. tmparr.push(this);
  336. tmparr.push(e);
  337. bindObj[action].apply(bindObj,tmparr);
  338. }else{
  339. if (window[action]){
  340. window[action].apply(this,tmparr);
  341. }
  342. }
  343. }else{
  344. this.options.action();
  345. }
  346. }
  347. }
  348. }
  349. });
  350. o2.widget.ToolbarMenu = new Class({
  351. Implements: [Options, Events],
  352. Extends: o2.widget.ToolbarButton,
  353. initialize: function(node, toolbar, options){
  354. this.parent(node, toolbar, options);
  355. this.modifiyStyle = true;
  356. this.menu = null;
  357. this.buttonID = this.node.get("MWFnodeid") || this.node.get("id");
  358. },
  359. setDisable: function(flag){
  360. if (this.menu) this.menu.options.disable = flag;
  361. if (flag){
  362. this.node.set("styles", this.toolbar.css.buttonDisable);
  363. if (this.picNode){
  364. this.picNode.set("styles", this.toolbar.css.buttonImgDivDisable);
  365. var img = this.picNode.getElement("img");
  366. var src = img.get("src");
  367. var ext = src.substr(src.lastIndexOf("."), src.length);
  368. src = src.substr(0, src.lastIndexOf("."));
  369. src = src+"_gray"+ext;
  370. // src = src.substr(0, src.lastIndexOf("."));
  371. // src = src+"_gray.gif";
  372. img.set("src", src);
  373. }
  374. if (this.textNode) this.textNode.set("styles", this.toolbar.css.buttonTextDivDisable);
  375. }else{
  376. this.node.set("styles", this.toolbar.css.button);
  377. if (this.picNode){
  378. this.picNode.set("styles", this.toolbar.css.buttonImgDiv);
  379. var img = this.picNode.getElement("img");
  380. var src = img.get("src");
  381. src = src.replace("_gray", "");
  382. img.set("src", src);
  383. }
  384. if (this.textNode) this.textNode.set("styles", this.toolbar.css.buttonTextDiv);
  385. }
  386. },
  387. load: function(){
  388. this._addButtonEvent();
  389. this.node.title = this.options.title;
  390. this.node.set("styles", this.toolbar.css.button);
  391. if (this.options.pic) this.picNode = this._createImageNode(this.options.pic);
  392. if (this.options.text) this.textNode = this._createTextNode(this.options.text);
  393. this._createDownNode();
  394. var toolbarMenu = this;
  395. o2.require("o2.widget.Menu", function(){
  396. this.menu = new o2.widget.Menu(this.node, {
  397. "style": this.toolbar.options.style,
  398. "bindObject": this.options.bindObject,
  399. "event": "click",
  400. "disable": toolbarMenu.options.disable,
  401. "onQueryShow": function(){
  402. var p = toolbarMenu.node.getPosition(toolbarMenu.node.getOffsetParent());
  403. var s = toolbarMenu.node.getSize();
  404. this.setOptions({
  405. "top": p.y+s.y-2,
  406. "left": p.x
  407. });
  408. toolbarMenu.fireEvent("menuQueryShow", [this]);
  409. return true;
  410. },
  411. "onPostShow": function(){
  412. var p = toolbarMenu.node.getPosition(toolbarMenu.node.getOffsetParent());
  413. var s = toolbarMenu.node.getSize();
  414. toolbarMenu.node.set("styles", {
  415. "background-color": this.node.getStyle("background-color"),
  416. "border-top": this.node.getStyle("border-top"),
  417. "border-right": this.node.getStyle("border-top"),
  418. "border-left": this.node.getStyle("border-top"),
  419. "border-bottom": "0"
  420. });
  421. toolbarMenu.modifiyStyle = false;
  422. toolbarMenu.tmpStyleNode = new Element("div.MWFtmpStyleNode",{
  423. "styles":{
  424. "position":"absolute",
  425. "top": p.y+s.y-2,
  426. "left": p.x+1,
  427. "width": s.x-2,
  428. "z-index": this.node.getStyle("z-index")+1,
  429. "background-color": this.node.getStyle("background-color"),
  430. "height": "1px",
  431. "overflow": "hidden"
  432. }
  433. }).inject(toolbarMenu.node);
  434. toolbarMenu.fireEvent("menuPostShow", [this]);
  435. },
  436. "onQueryHide": function(){
  437. toolbarMenu.fireEvent("menuQueryHide", [this]);
  438. },
  439. "onPostHide": function(){
  440. toolbarMenu.node.set("styles", toolbarMenu.toolbar.css.button);
  441. toolbarMenu.modifiyStyle = true;
  442. if (toolbarMenu.tmpStyleNode) toolbarMenu.tmpStyleNode.destroy();
  443. toolbarMenu.fireEvent("menuPostHide", [this]);
  444. }
  445. });
  446. this.menu.load();
  447. this.fireEvent("load", [this.menu]);
  448. this.addMenuItems();
  449. }.bind(this));
  450. this.setDisable(this.options.disable);
  451. },
  452. _createDownNode: function(){
  453. var spanNode = new Element("div",{
  454. "styles": this.toolbar.css.toolbarButtonDownNode
  455. }).inject(this.node);
  456. spanNode.set("html", "<img src=\""+o2.session.path+"/widget/$Toolbar/"+this.toolbar.options.style+"/downicon.gif"+"\" />");
  457. return spanNode;
  458. },
  459. addMenuItems: function(){
  460. var els = this.node.getChildren();
  461. els.each(function(node, idx){
  462. var type = node.get("MWFnodetype");
  463. if (type=="MWFToolBarMenuItem"){
  464. this._loadMenuItem(node);
  465. }
  466. if (type=="MWFToolBarMenuLine"){
  467. this._loadMenuLine(node);
  468. }
  469. }.bind(this));
  470. },
  471. _loadMenuItem: function(node){
  472. var toolBarMenu = this;
  473. var item = this.menu.addMenuItem(node.get("MWFButtonText"),"click",function(e){toolBarMenu._menuItemClick(node.get("MWFButtonAction"), e, this);}, node.get("MWFButtonImage"), node.get("MWFButtonDisable"));
  474. this.fireEvent("addMenuItem", [item]);
  475. },
  476. _menuItemClick: function(itemAction, e, item){
  477. if (itemAction){
  478. var tmparr = itemAction.split(":");
  479. var action = tmparr.shift();
  480. var bindObj = (this.toolbar.bindObject) ? this.toolbar.bindObject : window;
  481. if (bindObj[action]){
  482. tmparr.push(this);
  483. tmparr.push(e);
  484. tmparr.push(item);
  485. bindObj[action].apply(bindObj,tmparr);
  486. this.menu.hideMenu();
  487. }else{
  488. if (window[action]){
  489. window[action].apply(this,tmparr);
  490. this.menu.hideMenu();
  491. }
  492. }
  493. }
  494. },
  495. _loadMenuLine: function(){
  496. this.menu.addMenuLine();
  497. }
  498. });