Panel.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. o2.widget = o2.widget || {};
  2. o2.require("o2.widget.Common", null, false);
  3. o2.widget.Panel = new Class({
  4. Implements: [Options, Events],
  5. Extends: o2.widget.Common,
  6. options: {
  7. "title": "Panel",
  8. "style": "default",
  9. "width": 260,
  10. "height": 300,
  11. "top": 0,
  12. "left": 0,
  13. "minTop": 0,
  14. "minLeft": 0,
  15. "isMove": true,
  16. "isClose": true,
  17. "isMax": true,
  18. "isExpand": true,
  19. "isResize": true,
  20. "limitMove": true,
  21. "transition": Fx.Transitions.Back.easeIn,
  22. "transitionOut": Fx.Transitions.Back.easeOut,
  23. "duration": 100,
  24. "target": null
  25. },
  26. initialize: function(node, options){
  27. this.setOptions(options);
  28. this.node = $(node);
  29. this.container = new Element("div");
  30. this.path = o2.session.path+"/widget/$Panel/";
  31. this.cssPath = o2.session.path+"/widget/$Panel/"+this.options.style+"/css.wcss";
  32. this._loadCss();
  33. this.setStatus();
  34. o2.widget.Panel.panels.push(this);
  35. },
  36. load: function(){
  37. if (this.fireEvent("queryLoad")){
  38. this.container.set("styles", this.css.container);
  39. this.container.set("styles", {
  40. "width": ""+this.options.width.toFloat()+"px",
  41. "height": ""+this.options.height.toFloat()+"px"
  42. });
  43. this.createTitleNode();
  44. this.content = new Element("div", {
  45. "styles": this.css.panelContent
  46. }).inject(this.container);
  47. this.createBottomNode();
  48. if (!this.options.target) this.options.target = $(document.body);
  49. this.container.inject(this.options.target);
  50. this.content.setStyle("height", this.getContentHeight());
  51. this.node.inject(this.content);
  52. this.setPositions();
  53. if (this.options.isMove) this.setMove();
  54. if (this.options.isResize) this.setResize();
  55. this.show();
  56. }
  57. },
  58. show: function(){
  59. if (!this.showHideMorph){
  60. this.showHideMorph = new Fx.Morph(this.container, {
  61. "duration": this.options.duration,
  62. "transition": this.options.transition
  63. });
  64. }
  65. this.showHideMorph.start(this.css.containerShow).chain(function(){
  66. this.fireEvent("postLoad");
  67. }.bind(this));
  68. },
  69. getContentHeight: function(top, bottom, container){
  70. var topSize = top;
  71. if (!topSize) topSize = this.titleNode.getSize();
  72. var bottomSize = bottom;
  73. if (!bottomSize) bottomSize = this.bottomNode.getSize();
  74. var containerSize = container;
  75. if (!containerSize) containerSize = this.container.getSize();
  76. paddingTop = this.container.getStyle("padding-top");
  77. paddingBottom = this.container.getStyle("padding-bottom");
  78. paddingLeft = this.container.getStyle("padding-left");
  79. paddingRight = this.container.getStyle("padding-right");
  80. this.options.height = (containerSize.y.toFloat())-(paddingTop.toFloat())-(paddingBottom.toFloat());
  81. this.options.width = (containerSize.x.toFloat())-(paddingLeft.toFloat())-(paddingRight.toFloat());
  82. // var contentHeight = (containerSize.y.toFloat())-(paddingTop.toFloat())-(paddingBottom.toFloat())-(topSize.y.toFloat())-(bottomSize.y.toFloat());
  83. var contentHeight = (this.options.height.toFloat())-(topSize.y.toFloat())-(bottomSize.y.toFloat());
  84. if (contentHeight<10){
  85. contentHeight = 10;
  86. // this.options.height = contentHeight+(topSize.y.toFloat())+(bottomSize.y.toFloat());
  87. }
  88. return contentHeight-2;
  89. },
  90. setMove: function(){
  91. //this.titleNode.setStyle("cursor", "move");
  92. this.panelMove = new Drag.Move(this.container, {
  93. "container": (this.options.limitMove) ? this.options.target : null,
  94. "handle": this.titleNode,
  95. "onDrag": function(el, e){
  96. this.fireEvent("drag", [el, e]);
  97. }.bind(this),
  98. "onComplete": function(el, e){
  99. this.options.top = this.container.getStyle("top").toFloat();
  100. this.options.left = this.container.getStyle("left").toFloat();
  101. this.fireEvent("completeMove", [el, e]);
  102. }.bind(this)
  103. });
  104. },
  105. setResize: function(){
  106. this.container.makeResizable({
  107. "handle": this.resizeAction,
  108. "onDrag": function(){
  109. this.content.setStyle("height", this.getContentHeight());
  110. this.fireEvent("resize");
  111. }.bind(this),
  112. "limit": {x:[120,null], y:[120,null]}
  113. });
  114. },
  115. setPositions: function(){
  116. if (this.options.status.active){
  117. var p = this.options.target.getPosition();
  118. var top = (p.y.toFloat()) + (this.options.top.toFloat());
  119. var left = (p.x.toFloat()) + (this.options.left.toFloat());
  120. var pNode = this.options.target.getOffsetParent();
  121. if (pNode){
  122. var pNodePosition = pNode.getPosition();
  123. top = top - pNodePosition.y;
  124. left = left - pNodePosition.x;
  125. };
  126. this.container.setStyles({
  127. "position": "absolute",
  128. "top": top,
  129. "left": left
  130. });
  131. // this.container.setPosition({"x": left, "y": top});
  132. // alert(top)
  133. // this.container.position({
  134. // relativeTo: this.options.target,
  135. // position: 'upperRight',
  136. // edge: 'upperRight'
  137. // });
  138. }
  139. },
  140. setStatus: function(){
  141. this.options.status = {};
  142. this.options.status.active = true;
  143. this.options.status.expand = true;
  144. this.options.status.max = false;
  145. },
  146. createTitleNode: function(){
  147. this.titleNode = new Element("div", {
  148. "styles": this.css.panelTitleNode
  149. }).inject(this.container);
  150. this.actionNode = new Element("div", {
  151. "styles": this.css.panelActionNode
  152. }).inject(this.titleNode);
  153. this.textNode = new Element("div", {
  154. "styles": this.css.panelTextNode,
  155. "text": this.options.title
  156. }).inject(this.titleNode);
  157. if (this.options.isClose) this.createCloseAction();
  158. if (this.options.isMax) this.createMaxAction();
  159. if (this.options.isExpand) this.createExpandAction();
  160. this.titleNode.addEvent("click", function(){
  161. var maxIndex = this.container.getStyle("z-index").toInt();
  162. var idx = maxIndex;
  163. o2.widget.Panel.panels.each(function(panel){
  164. var index = panel.container.getStyle("z-index");
  165. if (maxIndex.toFloat()<index.toFloat()){
  166. maxIndex = index;
  167. }
  168. }.bind(this));
  169. maxIndex = (maxIndex.toFloat())+1;
  170. if (idx!=maxIndex){
  171. this.container.setStyle("z-index", maxIndex);
  172. }
  173. }.bind(this));
  174. },
  175. createCloseAction: function(){
  176. this.closeAction = new Element("div", {
  177. "styles": this.css.closeAction
  178. }).inject(this.actionNode);
  179. this.closeAction.addEvent("click", function(){
  180. this.closePanel();
  181. }.bind(this));
  182. },
  183. closePanel: function(){
  184. this.fireEvent("queryClose");
  185. if (!this.showHideMorph){
  186. this.showHideMorph = new Fx.Morph(this.container, {
  187. "duration": this.options.duration,
  188. "transition": this.options.transition
  189. });
  190. }
  191. this.showHideMorph.start(this.css.container).chain(function(){
  192. o2.widget.Panel.panels.erase(this);
  193. this.container.destroy();
  194. this.fireEvent("postClose");
  195. o2.release(this);
  196. }.bind(this));
  197. },
  198. destroy: function(){
  199. this.container.destroy();
  200. },
  201. createMaxAction: function(){
  202. this.maxAction = new Element("div", {
  203. "styles": this.css.maxAction
  204. }).inject(this.actionNode);
  205. this.maxAction.addEvent("click", function(){
  206. this.maxReturnPanel();
  207. }.bind(this));
  208. },
  209. maxReturnPanel: function(){
  210. if (this.options.status.max){
  211. this.returnPanel();
  212. var img = this.maxAction.getStyle("background-image");
  213. img = img.replace(/return.gif/g, "max.gif");;
  214. this.maxAction.setStyle("background-image", img);
  215. this.options.status.max = false;
  216. }else{
  217. this.maxPanel();
  218. var img = this.maxAction.getStyle("background-image");
  219. img = img.replace(/max.gif/g, "return.gif");;
  220. this.maxAction.setStyle("background-image", img);
  221. this.options.status.max = true;
  222. }
  223. this.fireEvent("resize");
  224. },
  225. returnPanel: function(){
  226. if (!this.options.status.expand) this.setExpand();
  227. var pos = this.returnMaxContainerPosition;
  228. this.container.setStyles({
  229. "position": "absolute",
  230. "top": (pos && pos.y) ? pos.y : this.options.top,
  231. "left": (pos && pos.x) ? pos.x : this.options.left,
  232. "width": this.returnMaxContainerSize.x,
  233. "height": this.returnMaxContainerSize.y
  234. });
  235. this.content.setStyle("height", this.getContentHeight());
  236. this.panelMove.attach();
  237. // this.resizeAction.setStyle("display", "block");
  238. },
  239. maxPanel: function(){
  240. if (!this.options.status.expand) this.setExpand();
  241. this.returnMaxContainerSize = this.container.getSize();
  242. this.returnMaxContainerPosition = {};
  243. var top = this.container.getStyle("top") || "";
  244. var left = this.container.getStyle("left") || "";
  245. if ( parseFloat(top).toString() !== "NaN" ){
  246. this.returnMaxContainerPosition.y = parseFloat(top);
  247. }
  248. if ( parseFloat(left).toString() !== "NaN" ){
  249. this.returnMaxContainerPosition.x = parseFloat(left);
  250. }
  251. var size = this.container.getOffsetParent().getSize();
  252. this.container.setStyles({
  253. "position": "absolute",
  254. "top": 2,
  255. "left": 2,
  256. "width": size.x-6,
  257. "height": size.y
  258. });
  259. this.panelMove.detach();
  260. // this.resizeAction.setStyle("display", "none");
  261. this.content.setStyle("height", this.getContentHeight());
  262. },
  263. createExpandAction: function(){
  264. this.explandAction = new Element("div", {
  265. "styles": this.css.explandAction
  266. }).inject(this.actionNode);
  267. this.explandAction.addEvent("click", function(){
  268. this.explandPanel();
  269. }.bind(this));
  270. },
  271. explandPanel: function(){
  272. if (this.options.status.expand){
  273. this.collapse();
  274. var img = this.explandAction.getStyle("background-image");
  275. img = img.replace(/up.gif/g, "down.gif");;
  276. this.explandAction.setStyle("background-image", img);
  277. this.options.status.expand = false;
  278. }else{
  279. this.expand();
  280. var img = this.explandAction.getStyle("background-image");
  281. img = img.replace(/down.gif/g, "up.gif");;
  282. this.explandAction.setStyle("background-image", img);
  283. this.options.status.expand = true;
  284. }
  285. },
  286. collapse: function(){
  287. debugger;
  288. this.returnTopSize = this.titleNode.getComputedSize();
  289. this.returnBottomSize = this.bottomNode.getComputedSize();
  290. this.returnContainerSize = this.container.getComputedSize();
  291. this.returnContentSize = this.content.getComputedSize();
  292. this.returnContainerPosition = this.container.getPosition(this.options.target);
  293. var top = this.container.getStyle("top") || "";
  294. var left = this.container.getStyle("left") || "";
  295. if ( parseFloat(top).toString() !== "NaN" ){
  296. this.returnContainerPosition.y = parseFloat(top);
  297. }
  298. if ( parseFloat(left).toString() !== "NaN" ){
  299. this.returnContainerPosition.x = parseFloat(left);
  300. }
  301. if (!this.containerTween){
  302. this.containerTween = new Fx.Morph(this.container, {
  303. "duration": this.options.duration,
  304. "transition": this.options.transition
  305. });
  306. }
  307. if (!this.bottomTween){
  308. this.bottomTween = new Fx.Morph(this.bottomNode, {
  309. "duration": this.options.duration,
  310. "transition": this.options.transition
  311. });
  312. }
  313. if (!this.contentTween){
  314. this.contentTween = new Fx.Morph(this.content, {
  315. "duration": this.options.duration,
  316. "transition": this.options.transition
  317. });
  318. }
  319. this.containerTween.options.transition = this.options.transition;
  320. this.bottomTween.options.transition = this.options.transition;
  321. this.contentTween.options.transition = this.options.transition;
  322. this.contentTween.start({"height": 0});
  323. this.bottomTween.start({"height": 0});
  324. this.containerTween.start({
  325. "height": this.returnTopSize.height,
  326. "width": "120px",
  327. "top": (this.options.minTop) || this.returnContainerPosition.y,
  328. "left": (this.options.minLeft) || this.returnContainerPosition.x
  329. }).chain(function(){
  330. this.bottomNode.setStyle("display", "none");
  331. this.content.setStyle("display", "none");
  332. }.bind(this));
  333. // if (!this.containerTween){
  334. // this.containerTween = new Fx.Tween(this.container, {
  335. // "duration": this.options.duration,
  336. // "transition": this.options.transition
  337. // });
  338. // }
  339. // if (!this.bottomTween){
  340. // this.bottomTween = new Fx.Tween(this.bottomNode, {
  341. // "duration": this.options.duration,
  342. // "transition": this.options.transition
  343. // });
  344. // }
  345. // if (!this.contentTween){
  346. // this.contentTween = new Fx.Tween(this.content, {
  347. // "duration": this.options.duration,
  348. // "transition": this.options.transition
  349. // });
  350. // }
  351. // this.containerTween.options.transition = this.options.transition;
  352. // this.bottomTween.options.transition = this.options.transition;
  353. // this.contentTween.options.transition = this.options.transition;
  354. //
  355. // this.contentTween.start("height", 0);
  356. // this.bottomTween.start("height", 0);
  357. // this.containerTween.start("height", this.returnTopSize.y).chain(function(){
  358. // this.bottomNode.setStyle("display", "none");
  359. // this.content.setStyle("display", "none");
  360. // }.bind(this));
  361. },
  362. expand: function(){
  363. if (!this.containerTween){
  364. this.containerTween = new Fx.Morph(this.container, {
  365. "duration": this.options.duration,
  366. "transition": this.options.transition
  367. });
  368. }
  369. if (!this.bottomTween){
  370. this.bottomTween = new Fx.Morph(this.bottomNode, {
  371. "duration": this.options.duration,
  372. "transition": this.options.transition
  373. });
  374. }
  375. if (!this.contentTween){
  376. this.contentTween = new Fx.Morph(this.content, {
  377. "duration": this.options.duration,
  378. "transition": this.options.transition
  379. });
  380. }
  381. this.containerTween.options.transition = this.options.transitionOut;
  382. this.bottomTween.options.transition = this.options.transitionOut;
  383. this.contentTween.options.transition = this.options.transitionOut;
  384. this.bottomNode.setStyle("display", "block");
  385. this.content.setStyle("display", "block");
  386. //var height = (this.returnContainerSize.y.toFloat())-(this.container.getStyle("padding-top").toFloat())-(this.container.getStyle("padding-bottom").toFloat())-2;
  387. this.containerTween.start({
  388. "height": this.returnContainerSize.height,
  389. "width": this.returnContainerSize.width,
  390. "left": this.returnContainerPosition.x,
  391. "top": this.returnContainerPosition.y
  392. });
  393. //this.contentTween.start("height", (this.options.height.toFloat())-(this.returnTopSize.y.toFloat())-(this.returnBottomSize.y.toFloat()));
  394. this.contentTween.start({"height": this.returnContentSize.height, "width": "auto"});
  395. //this.bottomTween.start("height", this.returnBottomSize.y);
  396. this.bottomTween.start({"height": this.returnBottomSize.height, "width": "auto"});
  397. // if (!this.containerTween){
  398. // this.containerTween = new Fx.Tween(this.container, {
  399. // "duration": this.options.duration,
  400. // "transition": this.options.transition
  401. // });
  402. // }
  403. // if (!this.bottomTween){
  404. // this.bottomTween = new Fx.Tween(this.bottomNode, {
  405. // "duration": this.options.duration,
  406. // "transition": this.options.transition
  407. // });
  408. // }
  409. // if (!this.contentTween){
  410. // this.contentTween = new Fx.Tween(this.content, {
  411. // "duration": this.options.duration,
  412. // "transition": this.options.transition
  413. // });
  414. // }
  415. // this.containerTween.options.transition = this.options.transitionOut;
  416. // this.bottomTween.options.transition = this.options.transitionOut;
  417. // this.contentTween.options.transition = this.options.transitionOut;
  418. //
  419. // this.bottomNode.setStyle("display", "block");
  420. // this.content.setStyle("display", "block");
  421. //
  422. // this.containerTween.start("height", (this.returnContainerSize.y.toFloat())-(this.container.getStyle("padding-top").toFloat())-(this.container.getStyle("padding-bottom").toFloat())-2);
  423. // this.contentTween.start("height", (this.options.height.toFloat())-(this.returnTopSize.y.toFloat())-(this.returnBottomSize.y.toFloat()));
  424. // this.bottomTween.start("height", this.returnBottomSize.y);
  425. },
  426. setExpand: function(){
  427. //this.container.setStyle("height", (this.returnContainerSize.y.toFloat())-(this.container.getStyle("padding-top").toFloat())-(this.container.getStyle("padding-bottom").toFloat())-2);
  428. this.container.setStyle("height", ""+this.returnContainerSize.height+"px");
  429. this.container.setStyle("width", ""+this.returnContainerSize.width+"px");
  430. //this.content.setStyle("height", (this.options.height.toFloat())-(this.returnTopSize.y.toFloat())-(this.returnBottomSize.y.toFloat()));
  431. this.content.setStyle("height", ""+this.returnContentSize.height+"px");
  432. this.content.setStyle("width", "auto");
  433. //this.bottomNode.setStyle("height", this.returnBottomSize.y);
  434. this.bottomNode.setStyle("height", ""+this.returnBottomSize.height+"px");
  435. this.bottomNode.setStyle("width", "auto");
  436. this.bottomNode.setStyle("display", "block");
  437. this.content.setStyle("display", "block");
  438. var img = this.explandAction.getStyle("background-image");
  439. img = img.replace(/down.gif/g, "up.gif");;
  440. this.explandAction.setStyle("background-image", img);
  441. this.options.status.expand = true;
  442. },
  443. createBottomNode: function(){
  444. this.bottomNode = new Element("div", {
  445. "styles": this.css.bottomNode
  446. }).inject(this.container);
  447. if (this.options.isResize) this.createResizeAction();
  448. },
  449. createResizeAction: function(){
  450. this.resizeAction = new Element("div", {
  451. "styles": this.css.resizeAction
  452. }).inject(this.bottomNode);
  453. }
  454. });
  455. o2.widget.Panel.panels = [];