| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292 | 
							- /**
 -  * Created by robin on 12/15/16.
 -  */
 - 
 - (function () {
 -     'use strict';
 - 
 -     angular.module('appSdk')
 -         .factory('editControllerBuilder', ['luBusyBusiness', 'AppUtilsBusiness', '$transitions',
 -             'ItemsBusiness', 'itemTracker', '$state', '$q', '$mdDialog',
 -             function (luBusyBusiness, AppUtilsBusiness, $transitions,
 -                       ItemsBusiness, itemTracker, $state, $q, $mdDialog) {
 - 
 -                 var EditControllerBuilder = {};
 - 
 -                 EditControllerBuilder.create = function ($scope, $stateParams) {
 - 
 -                     $scope.busy = luBusyBusiness.reset();
 -                     $scope.appUtils = AppUtilsBusiness;
 - 
 -                     $scope.isModal = $stateParams.isModal != null && $stateParams.isModal;
 - 
 -                     $scope.defaultItem = $scope.business.initDbo({});
 -                     $scope.title = null;
 -                     $scope.item = null;
 - 
 -                     if ($scope.onSubmit == undefined) {
 -                         $scope.onSubmit = null;
 -                     }
 -                     if ($scope.onItemChanged == undefined) {
 -                         $scope.onItemChanged = null;
 -                     }
 - 
 -                     $scope.watches = {};
 - 
 -                     $scope.itemTracker = itemTracker.create(function(item)
 -                     {
 -                         $scope.item = item;
 -                         $scope.setTitle();
 -                         if ($scope.onItemChanged != null) {
 -                             $scope.onItemChanged();
 -                         }
 -                     });
 - 
 -                     $scope.hasChanged = function()
 -                     {
 -                         return $scope.itemTracker.hasChanged();
 -                     };
 - 
 -                     $scope.unwatchSaved = function(fieldId, fieldObj) {
 -                         var k = fieldId  + ',' + fieldObj;
 -                         if ($scope.watches[k] != null) {
 -                             $scope.watches[k]();
 -                             delete $scope.watches[k];
 -                         }
 -                     };
 - 
 -                     $scope.saveWatch = function(fieldId, fieldObj, unwatch) {
 -                         var k = fieldId  + ',' + fieldObj;
 -                         $scope.watches[k] = unwatch;
 -                     };
 - 
 -                     $scope.unwatchId = function(fieldId, fieldObj) {
 -                         $scope.unwatchSaved(fieldId, fieldObj);
 -                     };
 - 
 -                     $scope.watchId = function(fieldId, fieldObj)
 -                     {
 -                         $scope.unwatchId(fieldId, fieldObj);
 -                         var unwatch = $scope.$watch(function()
 -                         {
 -                             return $scope.item == null || $scope.item[fieldObj] == null ? null : $scope.item[fieldObj].id;
 -                         }, function (newValue, oldValue) {
 -                             if (newValue != oldValue && $scope.item != null) {
 -                                 $scope.item[fieldId] = newValue;
 -                             }
 -                         });
 -                         $scope.saveWatch(fieldId, fieldObj, unwatch);
 -                     };
 - 
 -                     $scope.unwatchIds = function(fieldIds, fieldObjs) {
 -                         $scope.unwatchSaved(fieldIds, fieldObjs);
 -                     };
 - 
 -                     $scope.watchIds = function(fieldIds, fieldObjs)
 -                     {
 -                         $scope.unwatchIds(fieldIds, fieldObjs);
 -                         var unwatch = $scope.$watchCollection(function()
 -                         {
 -                             return $scope.item == null || $scope.item[fieldObjs] == null ? null : $scope.item[fieldObjs];
 -                         }, function (newValue, oldValue) {
 -                             if ($scope.item == null != null && (newValue != null || oldValue != null)) {
 -                                 if (newValue == null) {
 -                                     $scope.item[fieldIds] = null;
 -                                 }
 -                                 else {
 -                                     $scope.item[fieldIds] = newValue.map(function(item)
 -                                     {
 -                                         return item.id;
 -                                     });
 -                                 }
 -                             }
 -                         });
 -                         $scope.saveWatch(fieldIds, fieldObjs, unwatch);
 -                     };
 - 
 -                     $scope.removeListItem = function(list, index)
 -                     {
 -                         $scope.item[list].splice(index, 1);
 -                     };
 - 
 -                     $scope.editItem = function(item)
 -                     {
 -                         ItemsBusiness.editDialog(item, true).then(function(data) {}, function (error) {});
 -                     };
 - 
 -                     $scope.addItem = function(type, listObj)
 -                     {
 -                         ItemsBusiness.addDialog(type).then(function(data)
 -                         {
 -                             $scope.item[listObj].push(data);
 -                         }, function(error) {});
 -                     };
 - 
 -                     $scope.pickItem = function(type, listObj)
 -                     {
 -                         ItemsBusiness.pickDialog(type).then(function(data)
 -                         {
 -                             $scope.item[listObj].push(data);
 -                         }, function(error) {});
 -                     };
 - 
 -                     $scope.convertList = function(listObj, listIds)
 -                     {
 -                         $scope.item[listIds] = $scope.item[listObj].map(function(item)
 -                         {
 -                             return item.id;
 -                         });
 -                     };
 - 
 -                     $scope.askRemoveOne = function (item) {
 -                         ItemsBusiness.deleteDialog(item, $scope.itemType + '.edit').then(function(data)
 -                         {
 -                             $state.go($scope.itemType);
 -                         }, function(error) {});
 -                     };
 - 
 -                     $scope.loadArray = function(ids, objects, loader)
 -                     {
 -                         var objs = [];
 -                         for (var i = 0; i < $scope.item[ids].length; ++i) {
 -                             (function(a) {
 -                                 var obj = $scope.item[objects].find(function (item) {
 -                                     return item.id == $scope.item[ids][a];
 -                                 });
 -                                 if (obj == null) {
 -                                     objs.push(null);
 -                                     loader($scope.item[ids][a]).then(function (data) {
 -                                         objs[a] = data;
 -                                         var idx = objs.findIndex(function (item) {
 -                                             return item == null;
 -                                         });
 -                                         if (idx == -1) {
 -                                             $scope.item[objects] = objs;
 -                                             $scope.itemTracker.setItem($scope.item);
 -                                         }
 -                                     }, function (error) {
 -                                     });
 -                                 }
 -                                 else {
 -                                     objs.push(obj);
 -                                 }
 -                             })(i);
 -                         }
 -                     };
 - 
 -                     $scope.cancel = function()
 -                     {
 -                         if ($scope.isModal) {
 -                             $scope.itemTracker.mayAskExitConfirm().then(function()
 -                             {
 -                                 $mdDialog.cancel();
 -                             }, function (error) {});
 -                         }
 -                         else {
 -                             $state.go($scope.itemType);
 -                         }
 -                     };
 - 
 -                     $scope.setTitle = function()
 -                     {
 -                         if ($scope.item != null && $scope.item.id != null) {
 -                             var text = $scope.item.toString();
 -                             $scope.title = AppUtilsBusiness.tr($scope.itemType + '.edit.title', {text: text});
 -                             if (!$scope.isModal) {
 -                                 AppUtilsBusiness.setTitle($scope.title);
 -                                 AppUtilsBusiness.setToolbarTitle(AppUtilsBusiness.tr($scope.itemType + '.edit.toolbarTitle', {text: text}));
 -                             }
 -                         }
 -                         else {
 -                             if ($scope.isModal) {
 -                                 $scope.title = AppUtilsBusiness.tr($scope.itemType + '.add.defaultTitle');
 -                             }
 -                         }
 -                     };
 - 
 -                     $scope.setupExitConfirm = function()
 -                     {
 -                         var onTransitionStartOff = $transitions.onStart({}, function ($transitions) {
 -                             if ($scope.itemTracker.hasChanged()) {
 -                                 $scope.itemTracker.askExitConfirm().then(function () {
 -                                     onTransitionStartOff();
 -                                     var toState = $transitions.$to();
 -                                     var params = $transitions.params('to');
 -                                     var options = $transitions.options();
 -                                     $state.go(toState, params, options);
 -                                 }, function () {
 -                                 });
 -                                 return $q.reject(null);
 -                             }
 -                             onTransitionStartOff();
 -                         });
 -                         $scope.$on('$destroy', function () {
 -                             onTransitionStartOff();
 -                         });
 -                     };
 - 
 -                     $scope.submit = function()
 -                     {
 -                         $scope._submit($scope.item)
 -                     };
 - 
 -                     $scope._submit = function (item) {
 -                         if ($scope.onSubmit != null) {
 -                             $scope.onSubmit(item);
 -                         }
 - 
 -                         if (item.id == null) {
 -                             $scope.business.addDbo(item, $scope.itemType + '.edit').then(function (data) {
 -                                 $scope.itemTracker.setItem(data);
 - 
 -                                 if ($scope.isModal) {
 -                                     $mdDialog.hide(data);
 -                                 }
 -                                 else {
 -                                     $state.go($scope.itemType);
 -                                 }
 -                             }, function (error) {});
 -                         }
 -                         else {
 -                             $scope.business.editSingleByIdDbo(item.id, item, $scope.itemType + '.edit').then(function (data) {
 -                                 $scope.itemTracker.setItem(data);
 -                                 if ($scope.isModal) {
 -                                     $mdDialog.hide(data);
 -                                 }
 -                             }, function (error) {});
 -                         }
 -                     };
 - 
 -                     $scope.init = function($stateParams, customInit)
 -                     {
 -                         if ($stateParams != null) {
 -                             var item = $stateParams.item;
 -                             if (item != null) {
 -                                 if (item._itemType == $scope.itemType || item._itemType == null) {
 -                                     $scope.itemTracker.setItem($scope.business.initDbo(item));
 -                                 }
 -                                 else if (customInit != null) {
 -                                     customInit(item);
 -                                 }
 -                             }
 -                             else if ($stateParams.id != null) {
 -                                 $scope.business.getSingleById($stateParams.id, $scope.itemType + '.edit').then(function(data)
 -                                 {
 -                                     $scope.itemTracker.setItem(data);
 -                                 }, function (error) {
 -                                     $scope.itemTracker.setItem(null);
 -                                 });
 -                             }
 -                             else {
 -                                 $scope.itemTracker.setItem(angular.copy($scope.defaultItem));
 -                             }
 -                         }
 -                         else {
 -                             $scope.itemTracker.setItem(angular.copy($scope.defaultItem));
 -                         }
 -                     };
 -                 };
 - 
 -                 return EditControllerBuilder;
 -             }]);
 - })();
 
 
  |