project.js 8.5 KB

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