Toolbar.js 17 KB

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