Main.js 9.8 KB

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