project.js 8.3 KB

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