project.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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.FeeRate = FeeRate.createNew(this);
  71. this.masterField = {ration: 'billsItemID'};
  72. };
  73. // prototype用于定义public方法
  74. project.prototype.modify = function (modifyDatas, callback) {
  75. // To Do
  76. };
  77. // prototype用于定义public方法
  78. project.prototype.ID = function () {
  79. return tools._ID;
  80. };
  81. project.prototype.projCounter = function () {
  82. return tools.projCounter;
  83. }
  84. project.prototype.loadMainTree = function () {
  85. var that = this;
  86. var loadRationNode = function (rations, cacheNode) {
  87. var newNode, br = that.Ration.getBillsSortRation(cacheNode.source.getID()), i;
  88. for (i = 0; i < br.length; i++) {
  89. newNode = that.mainTree.addNode(cacheNode);
  90. newNode.source = br[i];
  91. newNode.sourceType = that.Ration.getSourceType();
  92. newNode.data = br[i];
  93. }
  94. };
  95. var loadIdTreeNode = function (nodes, parent) {
  96. var newNode, i;
  97. for (i = 0; i < nodes.length; i++) {
  98. newNode = that.mainTree.addNode(parent);
  99. newNode.source = nodes[i];
  100. newNode.sourceType = that.Bills.getSourceType();
  101. newNode.data = nodes[i].data;
  102. if (nodes[i].children.length === 0) {
  103. loadRationNode(that.Ration.datas, newNode);
  104. } else {
  105. loadIdTreeNode(nodes[i].children, newNode);
  106. }
  107. }
  108. };
  109. loadIdTreeNode(this.Bills.tree.roots, null);
  110. this.mainTree.sortTreeItems();
  111. };
  112. project.prototype.getParentTarget = function (node, targetField, targetValue) {
  113. var parent = node;
  114. while (parent && parent[targetField] !== targetValue) {
  115. parent = parent.parent;
  116. }
  117. return parent;
  118. };
  119. // 提供给各模块调用的统一从后台获取数据的方法
  120. /*project.prototype.pullData = function (url, data, successCallback, errorCallback) {
  121. $.ajax({
  122. type:"POST",
  123. url: url,
  124. data: {'data': JSON.stringify(data)},
  125. dataType: 'json',
  126. cache: false,
  127. timeout: 50000,
  128. success: function(result){
  129. successCallback(result);
  130. },
  131. error: function(jqXHR, textStatus, errorThrown){
  132. alert('error ' + textStatus + " " + errorThrown);
  133. errorCallback();
  134. }
  135. });
  136. };*/
  137. // 所有模块在此从后台获取数据
  138. project.prototype.loadDatas = function (callback){
  139. $.ajax({
  140. type: "POST",
  141. url: '/project/getData',
  142. data: {'data': JSON.stringify({
  143. "project_id": tools._ID,
  144. "user_id": tools._userID
  145. })},
  146. dataType: 'json',
  147. cache: false,
  148. timeout: 50000,
  149. success: function (result) {
  150. if (!result.error) {
  151. tools.doAfterLoad(result.data, callback);
  152. // for test calc
  153. //tools.doAfterLoad([{moduleName: 'bills', data: BillsData}, {'moduleName': 'ration', data: DrawingData}], callback);
  154. } else {
  155. alert('error: ' + result.message);
  156. callback(result.error);
  157. }
  158. },
  159. error: function(jqXHR, textStatus, errorThrown){
  160. alert('error ' + textStatus + " " + errorThrown);
  161. }
  162. });
  163. };
  164. project.prototype.beginUpdate = function(operation){
  165. if (tools.updateLock === 0){
  166. tools.operation = operation
  167. }
  168. tools.updateLock += 1;
  169. };
  170. project.prototype.endUpdate = function(){
  171. if (tools.updateLock === 0){
  172. throw "project can not endUpdate before beginUpdate";
  173. }
  174. tools.updateLock -= 1;
  175. if (tools.updateLock === 0) {
  176. $.ajax({
  177. type: "POST",
  178. url: '/project/save',
  179. data: {'data': JSON.stringify({
  180. "project_id": tools._ID,
  181. "user_id": tools._userID,
  182. "date": new Date,
  183. "operation": tools.operation,
  184. "update_data": tools.updateData
  185. })},
  186. dataType: 'json',
  187. cache: false,
  188. timeout: 50000,
  189. success: function (result) {
  190. if (!result.error) {
  191. tools.doAfterUpdate(result.data);
  192. } else {
  193. alert('error: ' + result.message);
  194. }
  195. },
  196. error: function(jqXHR, textStatus, errorThrown){
  197. alert('error ' + textStatus + " " + errorThrown);
  198. }
  199. });
  200. tools.updateData = [];
  201. tools.operation = "";
  202. }
  203. };
  204. project.prototype.push = function(moduleName, data){
  205. if (tools.updateLock === 0){
  206. throw "project can not push data before beginUpdate";
  207. }
  208. var moduleData = {
  209. moduleName: moduleName,
  210. data: data
  211. };
  212. tools.updateData.push(moduleData);
  213. };
  214. project.prototype.pushNow = function (operation, moduleName, data) {
  215. var that = this;
  216. this.beginUpdate(operation);
  217. if (Object.prototype.toString.apply(moduleName) === "[object Array]" && Object.prototype.toString.apply(data) === "[object Array]") {
  218. moduleName.forEach(function (name, index) {
  219. that.push(moduleName[index], data[index]);
  220. });
  221. } else {
  222. this.push(moduleName, data);
  223. }
  224. this.endUpdate();
  225. }
  226. project.prototype.registerModule = function(moduleName, obj){
  227. if (!tools.modules.hasOwnProperty(moduleName)){
  228. tools.modules[moduleName] = obj;
  229. }
  230. };
  231. return new project();
  232. }
  233. };