Toolbar.js 16 KB

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