project.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. };
  16. var me = tools;
  17. tools.doAfterUpdate = function(result){
  18. if (!result) { return }
  19. result.forEach(function(item){
  20. if (me.modules[item.moduleName]){
  21. me.modules[item.moduleName].doAfterUpdate(item.err, item.data);
  22. }
  23. });
  24. };
  25. tools.doAfterLoad = function(result, callback){
  26. result.forEach(function(item){
  27. if (me.modules[item.moduleName]){
  28. me.modules[item.moduleName].loadData(item.data);
  29. }
  30. });
  31. me._project.loadMainTree();
  32. //me.test(result[0].data[0]);
  33. callback(0);
  34. };
  35. /*tools.test = function(data){
  36. me._project.beginUpdate('修改名称');
  37. data.name = 'test';
  38. data['updateType'] = 'ut_update';
  39. me._project.push(ModuleNames.bills, [data]);
  40. me._project.endUpdate();
  41. };*/
  42. // 所有通过this访问的属性,都不应在此单元外部进行写入操作
  43. var project = function () {
  44. tools._project = this;
  45. this.mainTree = cacheTree.createNew(this);
  46. this.Bills = Bills.createNew(this);
  47. this.Ration = Ration.createNew(this);
  48. this.GLJ = GLJ.createNew(this);
  49. this.masterField = {ration: 'billsItemID'};
  50. };
  51. // prototype用于定义public方法
  52. project.prototype.modify = function (modifyDatas, callback) {
  53. // To Do
  54. };
  55. // prototype用于定义public方法
  56. project.prototype.ID = function () {
  57. return tools._ID;
  58. };
  59. project.prototype.loadMainTree = function () {
  60. var that = this;
  61. var loadRationNode = function (rations, cacheNode) {
  62. var newNode, br = that.Ration.getBillsSortRation(cacheNode.source.getID()), i;
  63. for (i = 0; i < br.length; i++) {
  64. newNode = that.mainTree.addNode(cacheNode);
  65. newNode.source = br[i];
  66. newNode.sourceType = that.Ration.getSourceType();
  67. newNode.data = br[i];
  68. }
  69. };
  70. var loadIdTreeNode = function (nodes, parent) {
  71. var newNode, i;
  72. for (i = 0; i < nodes.length; i++) {
  73. newNode = that.mainTree.addNode(parent);
  74. newNode.source = nodes[i];
  75. newNode.sourceType = that.Bills.getSourceType();
  76. newNode.data = nodes[i].data;
  77. if (nodes[i].children.length === 0) {
  78. loadRationNode(that.Ration.datas, newNode);
  79. } else {
  80. loadIdTreeNode(nodes[i].children, newNode);
  81. }
  82. }
  83. };
  84. loadIdTreeNode(this.Bills.tree.roots, null);
  85. this.mainTree.sortTreeItems();
  86. };
  87. project.prototype.getParentTarget = function (node, targetField, targetValue) {
  88. var parent = node;
  89. while (parent && parent[targetField] !== targetValue) {
  90. parent = parent.parent;
  91. }
  92. return parent;
  93. };
  94. // 提供给各模块调用的统一从后台获取数据的方法
  95. /*project.prototype.pullData = function (url, data, successCallback, errorCallback) {
  96. $.ajax({
  97. type:"POST",
  98. url: url,
  99. data: {'data': JSON.stringify(data)},
  100. dataType: 'json',
  101. cache: false,
  102. timeout: 50000,
  103. success: function(result){
  104. successCallback(result);
  105. },
  106. error: function(jqXHR, textStatus, errorThrown){
  107. alert('error ' + textStatus + " " + errorThrown);
  108. errorCallback();
  109. }
  110. });
  111. };*/
  112. // 所有模块在此从后台获取数据
  113. project.prototype.loadDatas = function (callback){
  114. $.ajax({
  115. type: "POST",
  116. url: '/project/getData',
  117. data: {'data': JSON.stringify({
  118. "project_id": tools._ID,
  119. "user_id": tools._userID
  120. })},
  121. dataType: 'json',
  122. cache: false,
  123. timeout: 50000,
  124. success: function (result) {
  125. if (!result.error) {
  126. tools.doAfterLoad(result.data, callback);
  127. } else {
  128. alert('error: ' + result.message);
  129. callback(result.error);
  130. }
  131. },
  132. error: function(jqXHR, textStatus, errorThrown){
  133. alert('error ' + textStatus + " " + errorThrown);
  134. }
  135. });
  136. };
  137. project.prototype.beginUpdate = function(operation){
  138. if (tools.updateLock === 0){
  139. tools.operation = operation
  140. }
  141. tools.updateLock += 1;
  142. };
  143. project.prototype.endUpdate = function(){
  144. if (tools.updateLock === 0){
  145. throw "project can not endUpdate before beginUpdate";
  146. }
  147. tools.updateLock -= 1;
  148. if (tools.updateLock === 0) {
  149. $.ajax({
  150. type: "POST",
  151. url: '/project/save',
  152. data: {'data': JSON.stringify({
  153. "project_id": tools._ID,
  154. "user_id": tools._userID,
  155. "date": new Date,
  156. "operation": tools.operation,
  157. "update_data": tools.updateData
  158. })},
  159. dataType: 'json',
  160. cache: false,
  161. timeout: 50000,
  162. success: function (result) {
  163. if (!result.error) {
  164. tools.doAfterUpdate(result.data);
  165. } else {
  166. alert('error: ' + result.message);
  167. }
  168. },
  169. error: function(jqXHR, textStatus, errorThrown){
  170. alert('error ' + textStatus + " " + errorThrown);
  171. }
  172. });
  173. tools.updateData = [];
  174. tools.operation = "";
  175. }
  176. };
  177. project.prototype.push = function(moduleName, data){
  178. if (tools.updateLock === 0){
  179. throw "project can not push data before beginUpdate";
  180. }
  181. var moduleData = {
  182. moduleName: moduleName,
  183. data: data
  184. };
  185. tools.updateData.push(moduleData);
  186. };
  187. project.prototype.pushNow = function (operation, moduleName, data) {
  188. var that = this;
  189. this.beginUpdate(operation);
  190. if (Object.prototype.toString.apply(moduleName) === "[object Array]" && Object.prototype.toString.apply(data) === "[object Array]") {
  191. moduleName.forEach(function (name, index) {
  192. that.push(moduleName[index], data[index]);
  193. });
  194. } else {
  195. this.push(moduleName, data);
  196. }
  197. this.endUpdate();
  198. }
  199. project.prototype.registerModule = function(moduleName, obj){
  200. if (!tools.modules.hasOwnProperty(moduleName)){
  201. tools.modules[moduleName] = obj;
  202. }
  203. };
  204. return new project();
  205. }
  206. };