project.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. project.prototype.getParentTarget = function (node, targetField, targetValue) {
  89. var parent = node;
  90. while (parent && parent[targetField] !== targetValue) {
  91. parent = parent.parent;
  92. }
  93. return parent;
  94. };
  95. // 提供给各模块调用的统一从后台获取数据的方法
  96. /*project.prototype.pullData = function (url, data, successCallback, errorCallback) {
  97. $.ajax({
  98. type:"POST",
  99. url: url,
  100. data: {'data': JSON.stringify(data)},
  101. dataType: 'json',
  102. cache: false,
  103. timeout: 50000,
  104. success: function(result){
  105. successCallback(result);
  106. },
  107. error: function(jqXHR, textStatus, errorThrown){
  108. alert('error ' + textStatus + " " + errorThrown);
  109. errorCallback();
  110. }
  111. });
  112. };*/
  113. // 所有模块在此从后台获取数据
  114. project.prototype.loadDatas = function (callback){
  115. $.ajax({
  116. type: "POST",
  117. url: '/project/getData',
  118. data: {'data': JSON.stringify({
  119. "project_id": tools._ID,
  120. "user_id": tools._userID
  121. })},
  122. dataType: 'json',
  123. cache: false,
  124. timeout: 50000,
  125. success: function (result) {
  126. if (!result.error) {
  127. tools.doAfterLoad(result.data, callback);
  128. } else {
  129. alert('error: ' + result.message);
  130. callback(result.error);
  131. }
  132. },
  133. error: function(jqXHR, textStatus, errorThrown){
  134. alert('error ' + textStatus + " " + errorThrown);
  135. }
  136. });
  137. };
  138. project.prototype.beginUpdate = function(operation){
  139. if (tools.updateLock === 0){
  140. tools.operation = operation
  141. }
  142. tools.updateLock += 1;
  143. };
  144. project.prototype.endUpdate = function(){
  145. if (tools.updateLock === 0){
  146. throw "project can not endUpdate before beginUpdate";
  147. }
  148. tools.updateLock -= 1;
  149. if (tools.updateLock === 0) {
  150. $.ajax({
  151. type: "POST",
  152. url: '/project/save',
  153. data: {'data': JSON.stringify({
  154. "project_id": tools._ID,
  155. "user_id": tools._userID,
  156. "date": new Date,
  157. "operation": tools.operation,
  158. "update_data": tools.updateData
  159. })},
  160. dataType: 'json',
  161. cache: false,
  162. timeout: 50000,
  163. success: function (result) {
  164. if (!result.error) {
  165. tools.doAfterUpdate(result.data);
  166. } else {
  167. alert('error: ' + result.message);
  168. }
  169. },
  170. error: function(jqXHR, textStatus, errorThrown){
  171. alert('error ' + textStatus + " " + errorThrown);
  172. }
  173. });
  174. tools.updateData = [];
  175. tools.operation = "";
  176. }
  177. };
  178. project.prototype.push = function(moduleName, data){
  179. if (tools.updateLock === 0){
  180. throw "project can not push data before beginUpdate";
  181. };
  182. var moduleData = {
  183. moduleName: moduleName,
  184. data: data
  185. };
  186. tools.updateData.push(moduleData);
  187. };
  188. project.prototype.pushNow = function (operation, moduleName, data) {
  189. this.beginUpdate(operation);
  190. this.push(moduleName, data);
  191. this.endUpdate();
  192. }
  193. project.prototype.registerModule = function(moduleName, obj){
  194. if (!tools.modules.hasOwnProperty(moduleName)){
  195. tools.modules[moduleName] = obj;
  196. }
  197. };
  198. return new project();
  199. }
  200. };