project.js 8.4 KB

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