project.js 8.5 KB

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