project.js 8.4 KB

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