Skip to content

Instantly share code, notes, and snippets.

@developandbehappy
Created September 2, 2017 19:43
Show Gist options
  • Select an option

  • Save developandbehappy/f5d9b8c668e83d6635d39cb2428a56ab to your computer and use it in GitHub Desktop.

Select an option

Save developandbehappy/f5d9b8c668e83d6635d39cb2428a56ab to your computer and use it in GitHub Desktop.
refsapp.controller('EditRefItemCtrl', ['$scope', '$uibModalStack', 'PopupService', '$rootScope', 'RefsApiService', 'refItemList', 'isEdit', 'refId', '$filter', 'isCreate', function ($scope, $uibModalStack, PopupService, $rootScope, RefsApiService, refItemList, isEdit, refId, $filter, isCreate) {
$scope.isEdit = isEdit;
$scope.isCreate = isCreate;
$scope.refList = handlerRefList(refItemList);
$scope.isLoad = false;
$scope.modalNameAndButtons = {
modal: !$scope.isEdit ? 'Просмотр записи справочника' : $scope.isCreate ? 'Создание записи справочника' : 'Редактирование записи справочника'
};
$scope.addItemToVolume = function (arr) {
arr.collapsed = true;
arr.val.push({
value: '',
oldValue: '',
remove: false,
edit: true
});
};
$scope.removeMultiplyItem = function (item) {
item.edit = false;
item.removed = !item.removed;
};
$scope.saveMultiplyItemValue = function (item) {
item.oldValue = item.value;
item.edit = false;
};
$scope.loadAnotherRefById = function (id) {
$scope.isLoad = true;
RefsApiService.loadColumnData(false, {id: item.id}).then(function (columnRes) {
if (columnRes.hasError()) return false;
// $scope.currentRef.column = columnRes.getResponse();
// loadColumnListData({page: 1, limit: $rootScope.refListDataLimit}, {id: item.id});
RefsApiService.loadColumnDataProp(loadData, ref)
});
PopupService.loadAnotherRef().result.then(function () {
$scope.isLoad = false;
});
};
$scope.saveRecordRef = function () {
$scope.isLoad = true;
var refList = _.map($scope.refList, function (refItem) {
refItem = {
field_name: refItem.label,
value: _.isArray(refItem.val) ? handlerValueForServer(refItem.val) : refItem.type === 'date' ? $filter('date')(refItem.val, 'dd.MM.yyyy') : refItem.val
};
return refItem;
});
if (isCreate) {
RefsApiService.createRecord(refList, {
refId: refId
}).then(function (refRes) {
if (refRes.hasError()) return false;
$rootScope.$emit('update record of ref');
$scope.closePopup();
});
return false;
}
RefsApiService.saveRecordRef(refList, {
refId: refId,
recordId: refItemList.id
}).then(function (refRes) {
if (refRes.hasError()) return false;
$rootScope.$emit('update record of ref');
$scope.closePopup();
})
};
$scope.cancelMultiplyItemValue = function (item) {
if (!item.oldValue) {
item.removed = true;
return false;
}
item.value = item.oldValue;
item.edit = false;
};
function handlerValueForServer(val) {
return _.map(_.filter(val, function (itemVal) {
return !itemVal.removed;
}), 'value');
}
function handlerRefList(ref) {
var refList = [];
_.each(isCreate ? ref.data : ref, function (val, index) { // ref.data it's columns we need create empty input for create new record
if (index !== 'id' && index !== 'properties' && index !== 'rownumber') {
if (isCreate) {
if (val.field_name.toLowerCase() !== 'id' && val.field_name.toLowerCase() !== 'rownumber') {
refList.push({
label: val.field_name,
name: val.name,
multiple: val.multiple,
type: val.typeField,
link: val.linked_ref_id,
collapsed: false,
val: val.multiple ? [] : ''
});
}
} else {
refList.push({
label: index,
name: ref.properties.names[index],
multiple: ref.properties.multiple[index],
type: ref.properties.types[index],
link: ref.properties.link[index],
collapsed: false,
val: _.isArray(val) ? handlerValueForMultipleAttr(val) : getValByType(val, ref.properties.types[index])
});
}
}
});
return refList;
}
function getValByType(val, type) {
var allTypes = {
'number': function (int) {
return parseInt(int, 10)
},
'date': function (date) {
return new Date(date); // 10.10.2014
},
'checkbox': function (data) {
return data
},
'text': function (data) {
return data
}
};
return allTypes[type](val);
}
function handlerValueForMultipleAttr(val) {
return _.map(val, function (itemVal) {
itemVal = {
value: itemVal,
oldValue: itemVal,
remove: false,
edit: false
};
return itemVal;
});
}
$scope.closePopup = function () {
$uibModalStack.dismissAll();
};
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment