project.js 8.7 KB

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