Lnk.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. Element.implement({
  2. "makeLnk": function(options){
  3. return new MWF.xDesktop.LnkMaker(this, options);
  4. }
  5. });
  6. MWF.xDesktop.LnkMaker = new Class({
  7. Implements: [Options, Events],
  8. initialize: function(node, options){
  9. this.setOptions(options);
  10. this.node = node;
  11. this.desktop = layout.desktop;
  12. this.tmpApps = [];
  13. this.isReadyDrag = false;
  14. this.readyDragPosition = null;
  15. this.setEvent();
  16. },
  17. setEvent: function(){
  18. this.node.addEvents({
  19. "mousedown": function(e){
  20. if (!e.rightClick){
  21. this.isReadyDrag = true;
  22. this.readyDragPosition = {"x": e.page.x, "y": e.page.y};
  23. document.body.addEvent("mousemove", function(e){
  24. if (this.isReadyDrag){
  25. if (Math.abs(e.page.x-this.readyDragPosition.y)>6 || Math.abs(e.page.y-this.readyDragPosition.y)>6){
  26. this.isReadyDrag = false;
  27. this.readyDragPosition = null;
  28. this.node.removeEvents("mousemove");
  29. this.beginMoveToDesktop(e);
  30. }
  31. }
  32. }.bind(this));
  33. }
  34. }.bind(this),
  35. "mouseup": function(e){
  36. this.isReadyDrag = false;
  37. this.readyDragPosition = null;
  38. this.node.removeEvents("mousemove");
  39. }.bind(this)
  40. });
  41. },
  42. createMoveNode: function(){
  43. var moveNode = new Element("div", {
  44. "styles": this.desktop.css.lnkMoveNode
  45. }).inject(this.desktop.desktopNode);
  46. var size = this.node.getSize();
  47. moveNode.setStyles({
  48. "width": ""+size.x+"px",
  49. "height": ""+size.y+"px"
  50. });
  51. this.node.clone().inject(moveNode);
  52. moveNode.position({"relativeTo": this.node});
  53. return moveNode;
  54. },
  55. beginMoveToDesktop: function(e){
  56. var moveNode = this.createMoveNode();
  57. var desktop = this.desktop;
  58. //var quick
  59. var drag = new Drag.Move(moveNode, {
  60. "snap": 10,
  61. "stopPropagation": true,
  62. "droppables": desktop.lnkAreas,
  63. "onStart": function(){
  64. this.fireEvent("start");
  65. this.start();
  66. this.fireEvent("afterStart");
  67. }.bind(this),
  68. "onDrag": function(dragging,e){
  69. this.fireEvent("drag", dragging);
  70. this.drag(dragging, e);
  71. this.fireEvent("afterDrag", dragging);
  72. }.bind(this),
  73. "onEnter": function(dragging, inObj){
  74. this.fireEvent("enter", dragging, inObj);
  75. this.enter(dragging, inObj);
  76. this.fireEvent("afterEnter", dragging, inObj);
  77. }.bind(this),
  78. "onLeave": function(dragging, paper){
  79. this.fireEvent("leave", dragging, paper);
  80. this.leave(dragging);
  81. this.fireEvent("afterLeave", dragging, paper);
  82. }.bind(this),
  83. "onDrop": function(dragging, inObj){
  84. this.fireEvent("drop", dragging, inObj);
  85. this.drop(dragging, e);
  86. this.fireEvent("afterDrop", dragging, inObj);
  87. }.bind(this),
  88. "onCancel": function(dragging){
  89. this.fireEvent("cancel", dragging);
  90. this.cancel(dragging);
  91. this.fireEvent("afterCancel", dragging);
  92. }.bind(this),
  93. "onComplete": function(dragging, e){
  94. this.fireEvent("complete", dragging, e);
  95. // this.complete();
  96. this.fireEvent("afterComplete", dragging, e);
  97. }.bind(this)
  98. });
  99. drag.start(e);
  100. },
  101. start: function(){
  102. this.tmpApps = [];
  103. Object.each(this.desktop.apps, function(app, id){
  104. if (!app.window.isHide){
  105. this.tmpApps.push(app);
  106. app.window._fadeOut();
  107. }
  108. }.bind(this));
  109. },
  110. enter: function(dragging, inObj){
  111. this.addLnkMark(dragging, inObj);
  112. },
  113. drag: function(dragging,e){
  114. this.moveLnkMark(dragging, e);
  115. },
  116. leave: function(dragging){
  117. this.removeLnkMark(dragging);
  118. },
  119. drop: function(dragging){
  120. this.addLnk(dragging);
  121. this.endMoveToDesktop(dragging);
  122. },
  123. cancel: function(dragging){
  124. this.endMoveToDesktop(dragging);
  125. },
  126. addLnkMark: function(node, inObj){
  127. if (!this.lnkMarkNode){
  128. this.lnkMarkNode = new Element("div", {
  129. "styles": this.desktop.css.dsektoplnkMarkNode
  130. }).inject(inObj);
  131. }
  132. },
  133. moveLnkMark: function(node,e){
  134. if (this.lnkMarkNode){
  135. var p = node.getPosition();
  136. var size = node.getSize();
  137. var area = this.lnkMarkNode.getParent();
  138. var lnks = area.getChildren();
  139. var position = false;
  140. for (var i=0; i<lnks.length; i++){
  141. if (lnks[i]!=this.lnkMarkNode && lnks[i]!=node){
  142. var lnkP = lnks[i].getPosition();
  143. var lnkSize = lnks[i].getSize();
  144. if (((lnkP.y+lnkSize.y) > (p.y+size.y/2)) && ((lnkP.x+lnkSize.x)>(p.x+size.x/2))){
  145. // var iconNode = lnks[i].getFirst("div");
  146. //
  147. // iconNode.setStyles({
  148. // "background-color": "#666"
  149. // });
  150. }else if ((lnkP.y+lnkSize.y)>(p.y+size.y)){
  151. this.lnkMarkNode.inject(lnks[i], "before");
  152. this.lnkMarkNode.setStyle("top", lnkP.y-10);
  153. position = true;
  154. break;
  155. }else{
  156. // var iconNode = lnks[i].getFirst("div");
  157. // iconNode.setStyles({
  158. // "background": "transparent"
  159. // });
  160. }
  161. //if ((lnkP.y< e.event.y) && (lnkP.y+lnkSize.y>e.event.y) && (lnkP.x<e.event.x) && (lnkP.x+lnkSize.x>e.event.x)){
  162. // debugger;
  163. // if (lnks[i].node != this.node){
  164. // alert("in")
  165. // }
  166. //}
  167. }
  168. }
  169. if (!position){
  170. this.lnkMarkNode.inject(area);
  171. this.lnkMarkNode.setStyle("top", "auto");
  172. if (this.desktop.lnks.length) this.lnkMarkNode.setStyle("top", this.lnkMarkNode.getPosition().y-10);
  173. }
  174. }
  175. },
  176. removeLnkMark: function(node){
  177. if (this.lnkMarkNode){
  178. this.lnkMarkNode.destroy();
  179. this.lnkMarkNode = null;
  180. }
  181. },
  182. addLnk: function(node){
  183. if (this.lnkMarkNode){
  184. var par = this.options.par;
  185. var lnk = new MWF.xDesktop.Lnk(par.icon, par.title, par.par);
  186. var next = this.lnkMarkNode.getNext();
  187. if (next){
  188. var nextLnk = next.retrieve("lnk");
  189. var idx = this.desktop.lnks.indexOf(nextLnk);
  190. this.desktop.lnks.splice(idx, 0, lnk);
  191. }else{
  192. this.desktop.lnks.push(lnk);
  193. }
  194. lnk.inject(this.lnkMarkNode, "after");
  195. this.removeLnkMark();
  196. this.desktop.resizeLnk();
  197. }
  198. },
  199. endMoveToDesktop: function(dragging){
  200. this.tmpApps.each(function(app){
  201. app.window._fadeIn();
  202. });
  203. this.tmpApps = [];
  204. if (dragging) dragging.destroy();
  205. }
  206. });
  207. MWF.xDesktop.LnkMove = new Class({
  208. Extends: MWF.xDesktop.LnkMaker,
  209. setEvent: function(){
  210. this.isReadySwing = false;
  211. this.readySwingTime = "";
  212. this.node.addEvents({
  213. "mousedown": function(e){
  214. if (!e.rightClick){
  215. this.isReadyDrag = true;
  216. //this.isReadySwing = true;
  217. this.readyDragPosition = {"x": e.page.x, "y": e.page.y};
  218. this.readySwingTime = new Date().getTime();
  219. // this.swingTimer = window.setTimeout(function(){
  220. // this.beginNodeSwing(e);
  221. // }.bind(this), 1000);
  222. this.node.addEvent("mousemove", function(event){
  223. if (this.isReadyDrag){
  224. if (Math.abs(event.page.x-this.readyDragPosition.x)>10 || Math.abs(event.page.y-this.readyDragPosition.y)>10){
  225. this.clearEvent();
  226. this.beginMoveToDesktop(e);
  227. }
  228. }
  229. }.bind(this));
  230. }
  231. }.bind(this),
  232. "mouseup": function(e){this.clearEvent();}.bind(this)
  233. });
  234. },
  235. clearEvent: function(){
  236. this.isReadyDrag = false;
  237. this.readyDragPosition = null;
  238. this.isReadySwing = false;
  239. this.readySwingTime = "";
  240. this.node.removeEvents("mousemove");
  241. window.clearTimeout(this.swingTimer);
  242. },
  243. beginNodeSwing: function(e){
  244. if (this.isReadySwing){
  245. this.clearEvent();
  246. this.morph = new Fx.Morph(this.node, {duration: 100});
  247. var lnk = this.node.retrieve("lnk");
  248. lnk.isSwing = true;
  249. // this.nodeSwing(1);
  250. this.nodeSwing(22);
  251. }
  252. },
  253. nodeSwing: function(o){
  254. // var opacity = (o==1) ? 0.4 : 1;
  255. var margin = (o==22) ? 18 : 22;
  256. // this.morph.start({"opacity": opacity}).chain(function(){
  257. // this.nodeSwing(opacity);
  258. // }.bind(this));
  259. this.morph.start({"margin-left": margin}).chain(function(){
  260. this.nodeSwing(margin);
  261. }.bind(this));
  262. },
  263. stopSwing: function(){
  264. this.morph.cancel();
  265. var lnk = this.node.retrieve("lnk");
  266. // this.morph.start({"opacity": 1}).chain(function(){
  267. // lnk.isSwing = false;
  268. // }.bind(this));
  269. this.morph.start({"margin-left": 20}).chain(function(){
  270. lnk.isSwing = false;
  271. }.bind(this));
  272. },
  273. createMoveNode: function(){
  274. var moveNode = this.node.clone().inject(this.desktop.desktopNode);
  275. moveNode.position({"relativeTo": this.node});
  276. moveNode.setStyles(this.desktop.css.desktopLnkNode);
  277. return moveNode;
  278. },
  279. start: function(){
  280. this.node.setStyle("opacity", 0.5);
  281. this.node.setStyles(this.desktop.css.desktopLnkNode_current);
  282. },
  283. addLnk: function(node){
  284. if (this.lnkMarkNode){
  285. var nextLnkNode = this.lnkMarkNode.getNext();
  286. var prevLnkNode = this.lnkMarkNode.getPrevious();
  287. var nextLnk = (nextLnkNode) ? nextLnkNode.retrieve("lnk") : null;
  288. var prevLnk = (prevLnkNode) ? prevLnkNode.retrieve("lnk") : null;
  289. var lnk = this.node.retrieve("lnk");
  290. if (!nextLnk){
  291. this.desktop.lnks.erase(lnk);
  292. this.desktop.lnks.push(lnk);
  293. }else if (nextLnk==lnk || prevLnk==lnk){
  294. // this.node.fade("in");
  295. }else{
  296. this.desktop.lnks.erase(lnk);
  297. var index = this.desktop.lnks.indexOf(nextLnk);
  298. if (index!=-1){
  299. this.desktop.lnks.splice(index, 0, lnk);
  300. }else{
  301. this.desktop.lnks.push(lnk);
  302. }
  303. }
  304. // this.node.setStyle("opacity", 0);
  305. this.removeLnkMark();
  306. this.desktop.resizeLnk();
  307. // this.node.fade("in");
  308. }
  309. this.node.fade("in");
  310. },
  311. endMoveToDesktop: function(dragging){
  312. this.node.setStyles(this.desktop.css.desktopLnkNode);
  313. if (dragging) dragging.destroy();
  314. }
  315. });
  316. MWF.xDesktop.Lnk = new Class({
  317. initialize: function(icon, title, par){
  318. this.desktop = layout.desktop;
  319. this.icon = icon;
  320. this.title = title;
  321. this.par = par;
  322. this.node = new Element("div", {
  323. "styles": this.desktop.css.desktopLnkNode,
  324. "title": this.title
  325. });
  326. this.node.store("lnk", this);
  327. this.iconNode = new Element("div", {
  328. "styles": this.desktop.css.desktopLnkIconNode
  329. }).inject(this.node);
  330. var img = new Element("img").inject(this.iconNode);
  331. var s = this.iconNode.getSize();
  332. if (!s.x) s.x = this.iconNode.getStyle("width").toFloat();
  333. if (!s.y) s.y = this.iconNode.getStyle("height").toFloat();
  334. var w = s.x-10;
  335. var h = s.y-10;
  336. img.setStyles({
  337. "margin": "5px",
  338. "max-width": ""+w+"px",
  339. "max-height": ""+h+"px"
  340. });
  341. if (this.icon.substr(0,3).toLowerCase()=="url"){
  342. img.set("src", this.icon.substring(4, this.icon.length-1));
  343. //this.iconNode.setStyle("background-image", this.icon);
  344. }else{
  345. img.set("src", this.icon);
  346. //this.iconNode.setStyle("background-image", "url("+this.icon+")");
  347. }
  348. //this.iconNode.setStyle("background-image", "url("+this.icon+")");
  349. this.titleNode = new Element("div", {
  350. "styles": this.desktop.css.desktopLnkTitleNode,
  351. "text": this.title
  352. }).inject(this.node);
  353. this.node.addEvents({
  354. "mouseover": function(e){this.node.setStyles(this.desktop.css.desktopLnkNode_current);}.bind(this),
  355. "mouseout": function(e){this.node.setStyles(this.desktop.css.desktopLnkNode);}.bind(this),
  356. "click": function(e){this.open(e);}.bind(this)
  357. });
  358. this.lnkMove = new MWF.xDesktop.LnkMove(this.node);
  359. this.loadMenu();
  360. },
  361. loadMenu: function(){
  362. this.menu = new MWF.widget.Menu(this.node, {"style": "lnk",
  363. "container": this.desktop.node
  364. });
  365. this.menu.load();
  366. this.menu.addMenuItem(o2.LP.desktop.deleteLink, "click", function(){
  367. this.deleteLnk();
  368. }.bind(this));
  369. },
  370. deleteLnk: function(){
  371. this.desktop.lnks.erase(this);
  372. this.node.destroy();
  373. MWF.release(this);
  374. delete this;
  375. },
  376. inject: function(node, where){
  377. this.node.inject(node, where);
  378. },
  379. open: function(e){
  380. if (!this.isSwing){
  381. var parList = this.par.split("#");
  382. var appName = parList[0];
  383. var statusStr = parList[1] || "";
  384. if (appName.toLowerCase()==="@url"){
  385. window.open(statusStr);
  386. }else{
  387. var status = JSON.decode(statusStr, false);
  388. var options = {};
  389. if (status){
  390. if (status.appId){
  391. options = {
  392. "appId": status.appId,
  393. "onQueryLoad": function(){
  394. this.status = status;
  395. }
  396. };
  397. }else{
  398. options = {
  399. "onQueryLoad": function(){
  400. this.status = status;
  401. }
  402. };
  403. }
  404. }
  405. this.desktop.openApplication(e, appName, options);
  406. }
  407. }else{
  408. this.stopSwingFun = function(){
  409. this.lnkMove.stopSwing();
  410. this.node.removeEvent("click", this.stopSwingFun);
  411. }.bind(this);
  412. this.node.addEvent("click", this.stopSwingFun);
  413. }
  414. }
  415. });