Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

editcontrollerbuilder.business.js 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /**
  2. * Created by robin on 12/15/16.
  3. */
  4. (function () {
  5. 'use strict';
  6. angular.module('appSdk')
  7. .factory('editControllerBuilder', ['luBusyBusiness', 'AppUtilsBusiness', '$transitions',
  8. 'ItemsBusiness', 'itemTracker', '$state', '$q', '$mdDialog',
  9. function (luBusyBusiness, AppUtilsBusiness, $transitions,
  10. ItemsBusiness, itemTracker, $state, $q, $mdDialog) {
  11. var EditControllerBuilder = {};
  12. EditControllerBuilder.create = function ($scope, $stateParams) {
  13. $scope.busy = luBusyBusiness.reset();
  14. $scope.appUtils = AppUtilsBusiness;
  15. $scope.isModal = $stateParams.isModal != null && $stateParams.isModal;
  16. $scope.defaultItem = $scope.business.initDbo({});
  17. $scope.title = null;
  18. $scope.item = null;
  19. if ($scope.onSubmit == undefined) {
  20. $scope.onSubmit = null;
  21. }
  22. if ($scope.onItemChanged == undefined) {
  23. $scope.onItemChanged = null;
  24. }
  25. $scope.watches = {};
  26. $scope.itemTracker = itemTracker.create(function(item)
  27. {
  28. $scope.item = item;
  29. $scope.setTitle();
  30. if ($scope.onItemChanged != null) {
  31. $scope.onItemChanged();
  32. }
  33. });
  34. $scope.hasChanged = function()
  35. {
  36. return $scope.itemTracker.hasChanged();
  37. };
  38. $scope.unwatchSaved = function(fieldId, fieldObj) {
  39. var k = fieldId + ',' + fieldObj;
  40. if ($scope.watches[k] != null) {
  41. $scope.watches[k]();
  42. delete $scope.watches[k];
  43. }
  44. };
  45. $scope.saveWatch = function(fieldId, fieldObj, unwatch) {
  46. var k = fieldId + ',' + fieldObj;
  47. $scope.watches[k] = unwatch;
  48. };
  49. $scope.unwatchId = function(fieldId, fieldObj) {
  50. $scope.unwatchSaved(fieldId, fieldObj);
  51. };
  52. $scope.watchId = function(fieldId, fieldObj)
  53. {
  54. $scope.unwatchId(fieldId, fieldObj);
  55. var unwatch = $scope.$watch(function()
  56. {
  57. return $scope.item == null || $scope.item[fieldObj] == null ? null : $scope.item[fieldObj].id;
  58. }, function (newValue, oldValue) {
  59. if (newValue != oldValue && $scope.item != null) {
  60. $scope.item[fieldId] = newValue;
  61. }
  62. });
  63. $scope.saveWatch(fieldId, fieldObj, unwatch);
  64. };
  65. $scope.unwatchIds = function(fieldIds, fieldObjs) {
  66. $scope.unwatchSaved(fieldIds, fieldObjs);
  67. };
  68. $scope.watchIds = function(fieldIds, fieldObjs)
  69. {
  70. $scope.unwatchIds(fieldIds, fieldObjs);
  71. var unwatch = $scope.$watchCollection(function()
  72. {
  73. return $scope.item == null || $scope.item[fieldObjs] == null ? null : $scope.item[fieldObjs];
  74. }, function (newValue, oldValue) {
  75. if ($scope.item == null != null && (newValue != null || oldValue != null)) {
  76. if (newValue == null) {
  77. $scope.item[fieldIds] = null;
  78. }
  79. else {
  80. $scope.item[fieldIds] = newValue.map(function(item)
  81. {
  82. return item.id;
  83. });
  84. }
  85. }
  86. });
  87. $scope.saveWatch(fieldIds, fieldObjs, unwatch);
  88. };
  89. $scope.removeListItem = function(list, index)
  90. {
  91. $scope.item[list].splice(index, 1);
  92. };
  93. $scope.editItem = function(item)
  94. {
  95. ItemsBusiness.editDialog(item, true).then(function(data) {}, function (error) {});
  96. };
  97. $scope.addItem = function(type, listObj)
  98. {
  99. ItemsBusiness.addDialog(type).then(function(data)
  100. {
  101. $scope.item[listObj].push(data);
  102. }, function(error) {});
  103. };
  104. $scope.pickItem = function(type, listObj)
  105. {
  106. ItemsBusiness.pickDialog(type).then(function(data)
  107. {
  108. $scope.item[listObj].push(data);
  109. }, function(error) {});
  110. };
  111. $scope.convertList = function(listObj, listIds)
  112. {
  113. $scope.item[listIds] = $scope.item[listObj].map(function(item)
  114. {
  115. return item.id;
  116. });
  117. };
  118. $scope.askRemoveOne = function (item) {
  119. ItemsBusiness.deleteDialog(item, $scope.itemType + '.edit').then(function(data)
  120. {
  121. $state.go($scope.itemType);
  122. }, function(error) {});
  123. };
  124. $scope.loadArray = function(ids, objects, loader)
  125. {
  126. var objs = [];
  127. for (var i = 0; i < $scope.item[ids].length; ++i) {
  128. (function(a) {
  129. var obj = $scope.item[objects].find(function (item) {
  130. return item.id == $scope.item[ids][a];
  131. });
  132. if (obj == null) {
  133. objs.push(null);
  134. loader($scope.item[ids][a]).then(function (data) {
  135. objs[a] = data;
  136. var idx = objs.findIndex(function (item) {
  137. return item == null;
  138. });
  139. if (idx == -1) {
  140. $scope.item[objects] = objs;
  141. $scope.itemTracker.setItem($scope.item);
  142. }
  143. }, function (error) {
  144. });
  145. }
  146. else {
  147. objs.push(obj);
  148. }
  149. })(i);
  150. }
  151. };
  152. $scope.cancel = function()
  153. {
  154. if ($scope.isModal) {
  155. $scope.itemTracker.mayAskExitConfirm().then(function()
  156. {
  157. $mdDialog.cancel();
  158. }, function (error) {});
  159. }
  160. else {
  161. $state.go($scope.itemType);
  162. }
  163. };
  164. $scope.setTitle = function()
  165. {
  166. if ($scope.item != null && $scope.item.id != null) {
  167. var text = $scope.item.toString();
  168. $scope.title = AppUtilsBusiness.tr($scope.itemType + '.edit.title', {text: text});
  169. if (!$scope.isModal) {
  170. AppUtilsBusiness.setTitle($scope.title);
  171. AppUtilsBusiness.setToolbarTitle(AppUtilsBusiness.tr($scope.itemType + '.edit.toolbarTitle', {text: text}));
  172. }
  173. }
  174. else {
  175. if ($scope.isModal) {
  176. $scope.title = AppUtilsBusiness.tr($scope.itemType + '.add.defaultTitle');
  177. }
  178. }
  179. };
  180. $scope.setupExitConfirm = function()
  181. {
  182. var onTransitionStartOff = $transitions.onStart({}, function ($transitions) {
  183. if ($scope.itemTracker.hasChanged()) {
  184. $scope.itemTracker.askExitConfirm().then(function () {
  185. onTransitionStartOff();
  186. var toState = $transitions.$to();
  187. var params = $transitions.params('to');
  188. var options = $transitions.options();
  189. $state.go(toState, params, options);
  190. }, function () {
  191. });
  192. return $q.reject(null);
  193. }
  194. onTransitionStartOff();
  195. });
  196. $scope.$on('$destroy', function () {
  197. onTransitionStartOff();
  198. });
  199. };
  200. $scope.submit = function()
  201. {
  202. $scope._submit($scope.item)
  203. };
  204. $scope._submit = function (item) {
  205. if ($scope.onSubmit != null) {
  206. $scope.onSubmit(item);
  207. }
  208. if (item.id == null) {
  209. $scope.business.addDbo(item, $scope.itemType + '.edit').then(function (data) {
  210. $scope.itemTracker.setItem(data);
  211. if ($scope.isModal) {
  212. $mdDialog.hide(data);
  213. }
  214. else {
  215. $state.go($scope.itemType);
  216. }
  217. }, function (error) {});
  218. }
  219. else {
  220. $scope.business.editSingleByIdDbo(item.id, item, $scope.itemType + '.edit').then(function (data) {
  221. $scope.itemTracker.setItem(data);
  222. if ($scope.isModal) {
  223. $mdDialog.hide(data);
  224. }
  225. }, function (error) {});
  226. }
  227. };
  228. $scope.init = function($stateParams, customInit)
  229. {
  230. if ($stateParams != null) {
  231. var item = $stateParams.item;
  232. if (item != null) {
  233. if (item._itemType == $scope.itemType || item._itemType == null) {
  234. $scope.itemTracker.setItem($scope.business.initDbo(item));
  235. }
  236. else if (customInit != null) {
  237. customInit(item);
  238. }
  239. }
  240. else if ($stateParams.id != null) {
  241. $scope.business.getSingleById($stateParams.id, $scope.itemType + '.edit').then(function(data)
  242. {
  243. $scope.itemTracker.setItem(data);
  244. }, function (error) {
  245. $scope.itemTracker.setItem(null);
  246. });
  247. }
  248. else {
  249. $scope.itemTracker.setItem(angular.copy($scope.defaultItem));
  250. }
  251. }
  252. else {
  253. $scope.itemTracker.setItem(angular.copy($scope.defaultItem));
  254. }
  255. };
  256. };
  257. return EditControllerBuilder;
  258. }]);
  259. })();