project.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. } else {
  153. alert('error: ' + result.message);
  154. callback(result.error);
  155. }
  156. },
  157. error: function(jqXHR, textStatus, errorThrown){
  158. alert('error ' + textStatus + " " + errorThrown);
  159. }
  160. });
  161. };
  162. project.prototype.beginUpdate = function(operation){
  163. if (tools.updateLock === 0){
  164. tools.operation = operation
  165. }
  166. tools.updateLock += 1;
  167. };
  168. project.prototype.endUpdate = function(){
  169. if (tools.updateLock === 0){
  170. throw "project can not endUpdate before beginUpdate";
  171. }
  172. tools.updateLock -= 1;
  173. if (tools.updateLock === 0) {
  174. $.ajax({
  175. type: "POST",
  176. url: '/project/save',
  177. data: {'data': JSON.stringify({
  178. "project_id": tools._ID,
  179. "user_id": tools._userID,
  180. "date": new Date,
  181. "operation": tools.operation,
  182. "update_data": tools.updateData
  183. })},
  184. dataType: 'json',
  185. cache: false,
  186. timeout: 50000,
  187. success: function (result) {
  188. if (!result.error) {
  189. tools.doAfterUpdate(result.data);
  190. } else {
  191. alert('error: ' + result.message);
  192. }
  193. },
  194. error: function(jqXHR, textStatus, errorThrown){
  195. alert('error ' + textStatus + " " + errorThrown);
  196. }
  197. });
  198. tools.updateData = [];
  199. tools.operation = "";
  200. }
  201. };
  202. project.prototype.push = function(moduleName, data){
  203. if (tools.updateLock === 0){
  204. throw "project can not push data before beginUpdate";
  205. }
  206. var moduleData = {
  207. moduleName: moduleName,
  208. data: data
  209. };
  210. tools.updateData.push(moduleData);
  211. };
  212. project.prototype.pushNow = function (operation, moduleName, data) {
  213. var that = this;
  214. this.beginUpdate(operation);
  215. if (Object.prototype.toString.apply(moduleName) === "[object Array]" && Object.prototype.toString.apply(data) === "[object Array]") {
  216. moduleName.forEach(function (name, index) {
  217. that.push(moduleName[index], data[index]);
  218. });
  219. } else {
  220. this.push(moduleName, data);
  221. }
  222. this.endUpdate();
  223. }
  224. project.prototype.registerModule = function(moduleName, obj){
  225. if (!tools.modules.hasOwnProperty(moduleName)){
  226. tools.modules[moduleName] = obj;
  227. }
  228. };
  229. return new project();
  230. }
  231. };