Panel.js 12 KB

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