Main.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. MWF.xApplication.Note.options.multitask = false;
  2. MWF.xApplication.Note.Main = new Class({
  3. Extends: MWF.xApplication.Common.Main,
  4. Implements: [Options, Events],
  5. options: {
  6. "style": "default",
  7. "name": "Note",
  8. "icon": "icon.png",
  9. "width": "400",
  10. "height": "500",
  11. "isResize": false,
  12. "isMax": false,
  13. "title": MWF.xApplication.Note.LP.title
  14. },
  15. onQueryLoad: function(){
  16. this.lp = MWF.xApplication.Note.LP;
  17. },
  18. loadWindow: function(isCurrent){
  19. this.fireAppEvent("queryLoadWindow");
  20. this.window = new MWF.xDesktop.WindowTransparent(this);
  21. this.fireAppEvent("loadWindow");
  22. this.window.show();
  23. this.content = this.window.content;
  24. if (isCurrent) this.setCurrent();
  25. this.fireAppEvent("postLoadWindow");
  26. this.fireAppEvent("queryLoadApplication");
  27. this.loadApplication(function(){
  28. this.fireAppEvent("postLoadApplication");
  29. }.bind(this));
  30. },
  31. loadApplication: function(callback){
  32. this.noteList = [];
  33. this.notes = [];
  34. this.topIndex = 101;
  35. MWF.UD.getData("noteList", function(json){
  36. if (json.data) {
  37. this.noteList = JSON.decode(json.data);
  38. }
  39. this.loadNotes();
  40. if (callback) callback();
  41. }.bind(this));
  42. this.addEvent("queryClose", function(){
  43. this.notes.each(function(note){ note.save(false); });
  44. }.bind(this));
  45. this.unloadSaveFun = function(){
  46. this.notes.each(function(note){ note.save(false); });
  47. }.bind(this);
  48. this.desktop.addEvent("unload", this.unloadSaveFun);
  49. this.addEvent("postClose", function(){
  50. this.desktop.removeEvent("unload", this.unloadSaveFun);
  51. }.bind(this));
  52. },
  53. loadNotes: function(){
  54. if (this.noteList.length){
  55. this.noteList.each(function(noteId){
  56. this.loadNote(noteId);
  57. }.bind(this));
  58. }else{
  59. this.loadNewNote();
  60. }
  61. },
  62. loadNote: function(id){
  63. this.notes.push(new MWF.xApplication.Note.NoteItem(this, id));
  64. },
  65. loadNewNote: function(where){
  66. this.notes.push(new MWF.xApplication.Note.NoteItem(this, "", where));
  67. }
  68. });
  69. MWF.xApplication.Note.NoteItem = new Class({
  70. Implements: [Events],
  71. initialize: function(note, id, where){
  72. this.note = note;
  73. this.css = this.note.css;
  74. this.id = id;
  75. this.load(where);
  76. },
  77. load: function(where){
  78. if (this.id){
  79. MWF.UD.getData(this.id, function(json){
  80. if (json.data) {
  81. this.noteData = JSON.decode(json.data);
  82. this.loadNode();
  83. }
  84. }.bind(this));
  85. }else{
  86. var d = (new Date()).getTime();
  87. this.id = "node"+d;
  88. var position = where;
  89. if (!position){
  90. var p = this.note.desktop.desktopNode.getPosition();
  91. var s = this.note.desktop.desktopNode.getSize();
  92. var y = p.y+10;
  93. var x = s.x-645;
  94. position = {"left": ""+x+"px", "top": ""+y+"px"};
  95. }
  96. this.noteData = {
  97. "id": this.id,
  98. "data": "",
  99. "position": position,
  100. "size": {"width": "200px", "height": "200px"}
  101. };
  102. this.loadNode();
  103. }
  104. },
  105. loadNode: function(){
  106. this.node = new Element("div", {"styles": this.css.itemNode}).inject(this.note.content);
  107. this.titleNode = new Element("div", {"styles": this.css.itemTitleNode}).inject(this.node);
  108. this.textarea = new Element("textarea", {"styles": this.css.itemTextarea}).inject(this.node);
  109. this.bottomNode = new Element("div", {"styles": this.css.itemBottomNode}).inject(this.node);
  110. this.addActionNode = new Element("div", {"styles": this.css.addActionNode}).inject(this.titleNode);
  111. this.closeActionNode = new Element("div", {"styles": this.css.closeActionNode}).inject(this.titleNode);
  112. this.resizeActionNode = new Element("div", {"styles": this.css.resizeActionNode}).inject(this.bottomNode);
  113. this.node.setStyles({
  114. "height": this.noteData.size.height,
  115. "width": this.noteData.size.width
  116. });
  117. Object.each(this.noteData.position, function(v, k){
  118. this.node.setStyle(k, v);
  119. }.bind(this));
  120. var x = this.noteData.size.width.toFloat()-10;
  121. var titleHeight = this.titleNode.getSize().y;
  122. var y = this.noteData.size.height.toFloat()-10-titleHeight-10-10;
  123. this.textarea.setStyles({
  124. "height": ""+y+"px",
  125. "width": ""+x+"px"
  126. });
  127. this.textarea.set("value", this.noteData.data);
  128. var drag = new Drag.Move(this.node, {
  129. "handle": this.titleNode,
  130. "container": this.note.desktop.desktopNode,
  131. "onDrop": function(){
  132. this.save();
  133. //var p = this.node.getPosition();
  134. //this.noteData.position = {
  135. // "left": p.x,
  136. // "top": p.y
  137. //}
  138. }.bind(this)
  139. });
  140. this.node.makeResizable({
  141. "handle": this.resizeActionNode,
  142. "stopPropagation": true,
  143. "preventDefault": true,
  144. "limit": {
  145. "x": [100, null],
  146. "y": [100, null]
  147. },
  148. "onDrag": function(){
  149. var s = this.node.getSize();
  150. this.noteData.size = {"width": ""+ s.x+"px", "height": ""+ s.y+"px"};
  151. var x = this.noteData.size.width.toFloat()-10;
  152. var titleHeight = this.titleNode.getSize().y;
  153. var y = this.noteData.size.height.toFloat()-10-titleHeight-10-10;
  154. this.textarea.setStyles({
  155. "height": ""+y+"px",
  156. "width": ""+x+"px"
  157. });
  158. }.bind(this),
  159. "onComplete": function(){
  160. this.save();
  161. }.bind(this)
  162. });
  163. this.setEvents();
  164. },
  165. setEvents: function(){
  166. var css = this.css;
  167. this.addActionNode.addEvents({
  168. "mouseover": function(){this.setStyles(css.addActionNode_over);},
  169. "mouseout": function(){this.setStyles(css.addActionNode);},
  170. "click": function(){
  171. this.addNote();
  172. }.bind(this)
  173. });
  174. this.closeActionNode.addEvents({
  175. "mouseover": function(){this.setStyles(css.closeActionNode_over);},
  176. "mouseout": function(){this.setStyles(css.closeActionNode);},
  177. "click": function(e){
  178. this.closeNote(e);
  179. }.bind(this)
  180. });
  181. this.textarea.addEvents({
  182. "change": function(){this.save();}.bind(this),
  183. "blur": function(){this.save();}.bind(this)
  184. });
  185. this.node.addEvent("click", function(){
  186. this.note.setCurrent();
  187. var count = this.note.notes.length;
  188. if (this.node.getStyle("z-index")<this.note.topIndex){
  189. this.node.setStyle("z-index", this.note.topIndex);
  190. this.note.topIndex++;
  191. }
  192. }.bind(this));
  193. },
  194. save: function(async){
  195. var asyncFlag = true;
  196. if (async===false) asyncFlag = false;
  197. this.noteData.data = this.textarea.get("value");
  198. var p = this.node.getPosition();
  199. var s = this.node.getSize();
  200. this.noteData.position = {"left": ""+ p.x+"px", "top": ""+ p.y+"px"};
  201. this.noteData.size = {"width": ""+ s.x+"px", "height": ""+ s.y+"px"};
  202. if (this.noteData.data){
  203. MWF.UD.putData(this.id, this.noteData, function(json){
  204. if (this.note.noteList.indexOf(this.id)==-1){
  205. this.note.noteList.push(this.id);
  206. MWF.UD.putData("noteList", this.note.noteList);
  207. }
  208. }.bind(this), asyncFlag);
  209. }
  210. },
  211. addNote: function(){
  212. var s = this.note.desktop.desktopNode.getSize();
  213. var p = this.node.getPosition();
  214. var x = p.x-205;
  215. var y = p.y;
  216. if (x<0) x = 0;
  217. if (x>s.x) x = s.x;
  218. if (y<0) y = 0;
  219. if (y>s.y) y = s.y;
  220. this.note.loadNewNote({"left": ""+x+"px", "top": ""+y+"px"});
  221. },
  222. closeNote: function(e){
  223. var _self = this;
  224. this.note.confirm("warn", e, this.note.lp.deleteNoteTitle, this.note.lp.deleteNote, 300, 120, function(){
  225. MWF.UD.deleteData(this.id, function(json){
  226. this.note.notes.erase(this);
  227. this.note.noteList.erase(this.id);
  228. MWF.UD.putData("noteList", this.note.noteList);
  229. this.node.destroy();
  230. if (!this.note.notes.length) this.note.close();
  231. MWF.release(this);
  232. }.bind(_self));
  233. this.close();
  234. }, function(){
  235. this.close();
  236. });
  237. }
  238. });