project.js 9.0 KB

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