project.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /**
  2. * Created by Mai on 2017/4/1.
  3. */
  4. var PROJECT = {
  5. createNew: function (projectID, userID) {
  6. // 定义private方法
  7. var tools = {
  8. _project: null,
  9. _ID: projectID,
  10. _userID: userID,
  11. updateLock: 0,
  12. updateData: [],
  13. operation: '',
  14. modules: {}
  15. };
  16. var me = tools;
  17. tools.doAfterUpdate = function(result){
  18. result.forEach(function(item){
  19. if (me.modules[item.moduleName]){
  20. me.modules[item.moduleName].doAfterUpdate(item.err, item.data);
  21. }
  22. });
  23. };
  24. tools.doAfterLoad = function(result, callback){
  25. result.forEach(function(item){
  26. if (me.modules[item.moduleName]){
  27. me.modules[item.moduleName].loadData(item.data);
  28. }
  29. });
  30. me._project.loadMainTree();
  31. //me.test(result[0].data[0]);
  32. callback(0);
  33. };
  34. /*tools.test = function(data){
  35. me._project.beginUpdate('修改名称');
  36. data.name = 'test';
  37. data['updateType'] = 'ut_update';
  38. me._project.push(ModuleNames.bills, [data]);
  39. me._project.endUpdate();
  40. };*/
  41. // 所有通过this访问的属性,都不应在此单元外部进行写入操作
  42. var project = function () {
  43. tools._project = this;
  44. this.mainTree = cacheTree.createNew(this);
  45. this.Bills = Bills.createNew(this);
  46. this.Ration = Ration.createNew(this);
  47. this.GLJ = GLJ.createNew(this);
  48. this.masterField = {ration: 'billsItemID'};
  49. };
  50. // prototype用于定义public方法
  51. project.prototype.modify = function (modifyDatas, callback) {
  52. // To Do
  53. };
  54. // prototype用于定义public方法
  55. project.prototype.ID = function () {
  56. return tools._ID;
  57. };
  58. project.prototype.loadMainTree = function () {
  59. var that = this;
  60. var loadRationNode = function (rations, cacheNode) {
  61. var newNode;
  62. rations.forEach(function (ration) {
  63. if (ration[that.masterField.ration] && ration[that.masterField.ration] === cacheNode.source.getID()) {
  64. newNode = that.mainTree.addNode(cacheNode);
  65. newNode.source = ration;
  66. newNode.sourceType = that.Ration.getSourceType();
  67. newNode.data = ration;
  68. }
  69. });
  70. };
  71. var loadIdTreeNode = function (nodes, parent) {
  72. var newNode, i;
  73. for (i = 0; i < nodes.length; i++) {
  74. newNode = that.mainTree.addNode(parent);
  75. newNode.source = nodes[i];
  76. newNode.sourceType = that.Bills.getSourceType();
  77. newNode.data = nodes[i].data;
  78. if (nodes[i].children.length === 0) {
  79. loadRationNode(that.Ration.datas, newNode);
  80. } else {
  81. loadIdTreeNode(nodes[i].children, newNode);
  82. }
  83. }
  84. };
  85. loadIdTreeNode(this.Bills.tree.roots, null);
  86. this.mainTree.sortTreeItems();
  87. };
  88. // 提供给各模块调用的统一从后台获取数据的方法
  89. /*project.prototype.pullData = function (url, data, successCallback, errorCallback) {
  90. $.ajax({
  91. type:"POST",
  92. url: url,
  93. data: {'data': JSON.stringify(data)},
  94. dataType: 'json',
  95. cache: false,
  96. timeout: 50000,
  97. success: function(result){
  98. successCallback(result);
  99. },
  100. error: function(jqXHR, textStatus, errorThrown){
  101. alert('error ' + textStatus + " " + errorThrown);
  102. errorCallback();
  103. }
  104. });
  105. };*/
  106. // 所有模块在此从后台获取数据
  107. project.prototype.loadDatas = function (callback){
  108. $.ajax({
  109. type: "POST",
  110. url: '/project/getData',
  111. data: {'data': JSON.stringify({
  112. "project_id": tools._ID
  113. })},
  114. dataType: 'json',
  115. cache: false,
  116. timeout: 50000,
  117. success: function (result) {
  118. if (!result.error) {
  119. tools.doAfterLoad(result.data, callback);
  120. } else {
  121. alert('error: ' + result.message);
  122. callback(result.error);
  123. }
  124. },
  125. error: function(jqXHR, textStatus, errorThrown){
  126. alert('error ' + textStatus + " " + errorThrown);
  127. }
  128. });
  129. };
  130. project.prototype.beginUpdate = function(operation){
  131. if (tools.updateLock === 0){
  132. tools.operation = operation
  133. }
  134. tools.updateLock += 1;
  135. };
  136. project.prototype.endUpdate = function(){
  137. if (tools.updateLock === 0){
  138. throw "project can not endUpdate before beginUpdate";
  139. }
  140. tools.updateLock -= 1;
  141. if (tools.updateLock === 0) {
  142. $.ajax({
  143. type: "POST",
  144. url: '/project/save',
  145. data: {'data': JSON.stringify({
  146. "project_id": tools._ID,
  147. "user_id": tools._userID,
  148. "date": new Date,
  149. "operation": tools.operation,
  150. "update_data": tools.updateData
  151. })},
  152. dataType: 'json',
  153. cache: false,
  154. timeout: 50000,
  155. success: function (result) {
  156. if (!result.error) {
  157. tools.doAfterUpdate(result.data);
  158. } else {
  159. alert('error: ' + result.message);
  160. }
  161. },
  162. error: function(jqXHR, textStatus, errorThrown){
  163. alert('error ' + textStatus + " " + errorThrown);
  164. }
  165. });
  166. tools.updateData = [];
  167. tools.operation = "";
  168. }
  169. };
  170. project.prototype.push = function(moduleName, data){
  171. if (tools.updateLock === 0){
  172. throw "project can not push data before beginUpdate";
  173. };
  174. var moduleData = {
  175. moduleName: moduleName,
  176. data: data
  177. };
  178. tools.updateData.push(moduleData);
  179. };
  180. project.prototype.registerModule = function(moduleName, obj){
  181. if (!tools.modules.hasOwnProperty(moduleName)){
  182. tools.modules[moduleName] = obj;
  183. }
  184. };
  185. return new project();
  186. }
  187. };