Main.js 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. this.addedCount = 0;
  102. },
  103. load: function(where){
  104. if (this.id){
  105. MWF.UD.getData(this.id, function(json){
  106. if (json.data) {
  107. this.noteData = JSON.decode(json.data);
  108. this.loadNode();
  109. }
  110. }.bind(this));
  111. }else{
  112. var d = (new Date()).getTime();
  113. this.id = "node"+d;
  114. var position = where;
  115. if (!position){
  116. var p = this.note.desktop.desktopNode.getPosition();
  117. var s = this.note.desktop.desktopNode.getSize();
  118. var y = p.y+10;
  119. var x = s.x-645;
  120. position = {"left": ""+x+"px", "top": ""+y+"px"};
  121. }
  122. this.noteData = {
  123. "id": this.id,
  124. "data": "",
  125. "position": position,
  126. "size": {"width": "200px", "height": "200px"}
  127. };
  128. this.loadNode();
  129. }
  130. },
  131. loadNode: function(){
  132. this.node = new Element("div", {"styles": this.css.itemNode}).inject(this.note.content);
  133. this.titleNode = new Element("div", {"styles": this.css.itemTitleNode}).inject(this.node);
  134. this.textarea = new Element("textarea", {"styles": this.css.itemTextarea}).inject(this.node);
  135. this.bottomNode = new Element("div", {"styles": this.css.itemBottomNode}).inject(this.node);
  136. this.addActionNode = new Element("div", {"styles": this.css.addActionNode}).inject(this.titleNode);
  137. this.closeActionNode = new Element("div", {"styles": this.css.closeActionNode}).inject(this.titleNode);
  138. this.resizeActionNode = new Element("div", {"styles": this.css.resizeActionNode}).inject(this.bottomNode);
  139. this.node.setStyles({
  140. "height": this.noteData.size.height,
  141. "width": this.noteData.size.width
  142. });
  143. Object.each(this.noteData.position, function(v, k){
  144. this.node.setStyle(k, v);
  145. }.bind(this));
  146. var x = this.noteData.size.width.toFloat()-10;
  147. var titleHeight = this.titleNode.getSize().y;
  148. var y = this.noteData.size.height.toFloat()-10-titleHeight-10-10;
  149. this.textarea.setStyles({
  150. "height": ""+y+"px",
  151. "width": ""+x+"px"
  152. });
  153. this.textarea.set("value", this.noteData.data);
  154. var drag = new Drag.Move(this.node, {
  155. "handle": this.titleNode,
  156. "container": this.note.desktop.desktopNode,
  157. "onDrop": function(){
  158. this.save();
  159. //var p = this.node.getPosition();
  160. //this.noteData.position = {
  161. // "left": p.x,
  162. // "top": p.y
  163. //}
  164. }.bind(this)
  165. });
  166. this.node.makeResizable({
  167. "handle": this.resizeActionNode,
  168. "stopPropagation": true,
  169. "preventDefault": true,
  170. "limit": {
  171. "x": [100, null],
  172. "y": [100, null]
  173. },
  174. "onDrag": function(){
  175. var s = this.node.getSize();
  176. this.noteData.size = {"width": ""+ s.x+"px", "height": ""+ s.y+"px"};
  177. var x = this.noteData.size.width.toFloat()-10;
  178. var titleHeight = this.titleNode.getSize().y;
  179. var y = this.noteData.size.height.toFloat()-10-titleHeight-10-10;
  180. this.textarea.setStyles({
  181. "height": ""+y+"px",
  182. "width": ""+x+"px"
  183. });
  184. }.bind(this),
  185. "onComplete": function(){
  186. this.save();
  187. }.bind(this)
  188. });
  189. this.setEvents();
  190. },
  191. setEvents: function(){
  192. var css = this.css;
  193. this.addActionNode.addEvents({
  194. "mouseover": function(){this.setStyles(css.addActionNode_over);},
  195. "mouseout": function(){this.setStyles(css.addActionNode);},
  196. "click": function(){
  197. this.addNote();
  198. }.bind(this)
  199. });
  200. this.closeActionNode.addEvents({
  201. "mouseover": function(){this.setStyles(css.closeActionNode_over);},
  202. "mouseout": function(){this.setStyles(css.closeActionNode);},
  203. "click": function(e){
  204. this.closeNote(e);
  205. }.bind(this)
  206. });
  207. this.textarea.addEvents({
  208. "change": function(){this.save();}.bind(this),
  209. "blur": function(){this.save();}.bind(this)
  210. });
  211. this.node.addEvent("click", function(){
  212. this.note.setCurrent();
  213. var count = this.note.notes.length;
  214. if (this.node.getStyle("z-index")<this.note.topIndex){
  215. this.node.setStyle("z-index", this.note.topIndex);
  216. this.note.topIndex++;
  217. }
  218. }.bind(this));
  219. },
  220. save: function(async){
  221. var asyncFlag = true;
  222. if (async===false) asyncFlag = false;
  223. this.noteData.data = this.textarea.get("value");
  224. var p = this.node.getPosition();
  225. var s = this.node.getSize();
  226. this.noteData.position = {"left": ""+ p.x+"px", "top": ""+ p.y+"px"};
  227. this.noteData.size = {"width": ""+ s.x+"px", "height": ""+ s.y+"px"};
  228. if (this.noteData.data){
  229. MWF.UD.putData(this.id, this.noteData, function(json){
  230. if (this.note.noteList.indexOf(this.id)==-1){
  231. this.note.noteList.push(this.id);
  232. MWF.UD.putData("noteList", this.note.noteList);
  233. }
  234. }.bind(this), asyncFlag);
  235. }
  236. },
  237. addNote: function(){
  238. var s = this.note.desktop.desktopNode.getSize();
  239. var p = this.node.getPosition();
  240. var x = p.x-205;
  241. var y = p.y;
  242. x = x - 10*this.addedCount;
  243. y = y + 10*this.addedCount;
  244. if (x<0) x = 0;
  245. if (x>s.x) x = s.x;
  246. if (y<0) y = 0;
  247. if (y>s.y) y = s.y;
  248. this.note.loadNewNote({"left": ""+x+"px", "top": ""+y+"px"});
  249. this.addedCount++;
  250. },
  251. closeNote: function(e){
  252. var _self = this;
  253. this.note.confirm("warn", e, this.note.lp.deleteNoteTitle, this.note.lp.deleteNote, 300, 120, function(){
  254. MWF.UD.deleteData(this.id, function(json){
  255. this.note.notes.erase(this);
  256. this.note.noteList.erase(this.id);
  257. MWF.UD.putData("noteList", this.note.noteList);
  258. this.node.destroy();
  259. if (!this.note.notes.length) this.note.close();
  260. MWF.release(this);
  261. }.bind(_self));
  262. this.close();
  263. }, function(){
  264. this.close();
  265. });
  266. }
  267. });