Toolbar.js 17 KB

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