item Tags faltan test
This commit is contained in:
parent
443c888d73
commit
632a5209c1
|
@ -22,3 +22,4 @@ Preview: Vista previa
|
|||
Clone: Clonar
|
||||
Do you want to clone this item?: ¿Desea clonar este artículo?
|
||||
Yes, clone: Si, clonar
|
||||
Value: Valor
|
|
@ -1,5 +1,43 @@
|
|||
<vn-watcher
|
||||
vn-id="watcher"
|
||||
data="$ctrl.item"
|
||||
form = "form">
|
||||
</vn-watcher>
|
||||
<form name="form" ng-submit="$ctrl.submit()">
|
||||
<vn-card>
|
||||
<vn-vertical pad-large>
|
||||
<vn-title>Item tags</vn-title>
|
||||
<mg-ajax path="/item/api/Tags" options="mgIndex as tags"></mg-ajax>
|
||||
<vn-horizontal ng-repeat="itemTag in $ctrl.itemTags track by $index">
|
||||
<vn-autocomplete
|
||||
vn-one
|
||||
initial-data = "itemTag.tag"
|
||||
field = "itemTag.tagFk"
|
||||
data = "tags.model"
|
||||
show-field = "name"
|
||||
label = "Tag">
|
||||
</vn-autocomplete>
|
||||
<vn-textfield vn-three label="Value" model="itemTag.value"></vn-textfield>
|
||||
<vn-one pad-medium-top>
|
||||
<vn-icon
|
||||
pointer
|
||||
medium-grey
|
||||
icon="remove_circle_outline"
|
||||
ng-click="$ctrl.removeItemTag($index)">
|
||||
</vn-icon>
|
||||
<vn-icon
|
||||
pointer
|
||||
margin-medium-left
|
||||
orange
|
||||
icon="add_circle"
|
||||
ng-if = "itemTag.showAddIcon && tags.model.length > $ctrl.itemTags.length"
|
||||
ng-click="$ctrl.addItemTag()">
|
||||
</vn-icon>
|
||||
</vn-one>
|
||||
</vn-horizontal>
|
||||
</vn-vertical>
|
||||
</vn-card>
|
||||
<vn-button-bar>
|
||||
<vn-submit label="Save"></vn-submit>
|
||||
</vn-button-bar>
|
||||
</form>
|
|
@ -1,5 +1,128 @@
|
|||
import ngModule from '../module';
|
||||
|
||||
ngModule.component('vnItemTags', {
|
||||
template: require('./item-tags.html')
|
||||
class ItemTags {
|
||||
constructor($http, $scope, $translate, vnApp, params) {
|
||||
this.$http = $http;
|
||||
this.$scope = $scope;
|
||||
this.$translate = $translate;
|
||||
this.vnApp = vnApp;
|
||||
this.itemId = params.id;
|
||||
this.itemTags = [];
|
||||
this.itemTagsRemoved = [];
|
||||
this.oldItemTags = {};
|
||||
}
|
||||
|
||||
_setIconAdd() {
|
||||
if (this.itemTags.length) {
|
||||
this.itemTags.map(element => {
|
||||
element.showAddIcon = false;
|
||||
return true;
|
||||
});
|
||||
this.itemTags[this.itemTags.length - 1].showAddIcon = true;
|
||||
} else {
|
||||
this.addItemTag();
|
||||
}
|
||||
}
|
||||
_setDirtyForm() {
|
||||
if (this.$scope.form) {
|
||||
this.$scope.form.$setDirty();
|
||||
}
|
||||
}
|
||||
_unsetDirtyForm() {
|
||||
if (this.$scope.form) {
|
||||
this.$scope.form.$setPristine();
|
||||
}
|
||||
}
|
||||
|
||||
removeItemTag(index) {
|
||||
let item = this.itemTags[index];
|
||||
if (item) {
|
||||
this.itemTags.splice(index, 1);
|
||||
this._setIconAdd();
|
||||
if (item.id) {
|
||||
this.itemTagsRemoved.push(item.id);
|
||||
this._setDirtyForm();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
addItemTag() {
|
||||
this.itemTags.push({value: null, itemFk: this.item.id, tagFk: null, showAddIcon: true});
|
||||
this._setIconAdd();
|
||||
}
|
||||
|
||||
_setOlTags(itemTags) {
|
||||
itemTags.map(tag => {
|
||||
this.oldItemTags[tag.id] = Object.assign({}, tag);
|
||||
return tag;
|
||||
});
|
||||
this._setIconAdd();
|
||||
}
|
||||
|
||||
_getItemtags() {
|
||||
let filter = {
|
||||
where: {itemFk: this.itemId},
|
||||
order: "priority ASC",
|
||||
include: {relation: "tag"}
|
||||
};
|
||||
this.$http.get(`/item/api/ItemTags?filter=${JSON.stringify(filter)}`).then(response => {
|
||||
this.itemTags = response.data;
|
||||
this._setOlTags(response.data);
|
||||
});
|
||||
}
|
||||
|
||||
_equalItemTags(tagOld, tagNew) {
|
||||
return tagOld.tagFk === tagNew.tagFk && tagOld.value === tagNew.value;
|
||||
}
|
||||
|
||||
submit() {
|
||||
let codes = [];
|
||||
let repeatedItemTags = false;
|
||||
let canSubmit;
|
||||
let submitObj = {
|
||||
delete: this.itemTagsRemoved,
|
||||
create: [],
|
||||
update: []
|
||||
};
|
||||
for (let i = 0; i < this.itemTags.length; i++) {
|
||||
let itemTag = this.itemTags[i];
|
||||
let isNewItemTag = itemTag.id === undefined;
|
||||
|
||||
if (itemTag.tagFk && codes.indexOf(itemTag.tagFk) !== -1) {
|
||||
repeatedItemTags = true;
|
||||
break;
|
||||
}
|
||||
if (itemTag.tagFk) codes.push(itemTag.tagFk);
|
||||
|
||||
if (isNewItemTag && itemTag.tagFk) {
|
||||
submitObj.create.push(itemTag);
|
||||
} else if (!isNewItemTag && !this._equalItemTags(this.oldItemTags[itemTag.id], itemTag)) {
|
||||
submitObj.update.push(itemTag);
|
||||
}
|
||||
}
|
||||
|
||||
if (repeatedItemTags) {
|
||||
return this.vnApp.showMessage(this.$translate.instant('The barcode must be unique'));
|
||||
}
|
||||
|
||||
canSubmit = submitObj.update.length > 0 || submitObj.create.length > 0 || submitObj.delete.length > 0;
|
||||
|
||||
if (canSubmit) {
|
||||
return this.$http.post(`/item/api/ItemTags/crudItemTags`, submitObj).then(() => {
|
||||
this._getItemtags();
|
||||
this._unsetDirtyForm();
|
||||
});
|
||||
}
|
||||
this.vnApp.showMessage(this.$translate.instant('No changes to save'));
|
||||
}
|
||||
|
||||
$onInit() {
|
||||
this._getItemtags();
|
||||
}
|
||||
}
|
||||
ItemTags.$inject = ['$http', '$scope', '$translate', 'vnApp', '$stateParams'];
|
||||
|
||||
ngModule.component('vnItemTags', {
|
||||
template: require('./item-tags.html'),
|
||||
controller: ItemTags
|
||||
});
|
||||
|
|
|
@ -0,0 +1,36 @@
|
|||
module.exports = Self => {
|
||||
Self.remoteMethod('crudItemTags', {
|
||||
description: 'create, update or delete itemTags',
|
||||
accessType: 'WRITE',
|
||||
accepts: [
|
||||
{
|
||||
arg: 'itemtags',
|
||||
type: 'Object',
|
||||
require: true,
|
||||
description: 'object with itemTags to create, update or delete, Example: {create: [], update: [], delete: []}',
|
||||
http: {source: 'body'}
|
||||
}
|
||||
],
|
||||
http: {
|
||||
path: `/crudItemTags`,
|
||||
verb: 'post'
|
||||
}
|
||||
});
|
||||
|
||||
Self.crudItemTags = itemtags => {
|
||||
let promises = [];
|
||||
|
||||
if (itemtags.delete && itemtags.delete.length) {
|
||||
promises.push(Self.destroyAll({id: {inq: itemtags.delete}}));
|
||||
}
|
||||
if (itemtags.create.length) {
|
||||
promises.push(Self.create(itemtags.create));
|
||||
}
|
||||
if (itemtags.update.length) {
|
||||
itemtags.update.forEach(itemtag => {
|
||||
promises.push(Self.upsert(itemtag));
|
||||
});
|
||||
}
|
||||
return Promise.all(promises);
|
||||
};
|
||||
};
|
|
@ -0,0 +1,3 @@
|
|||
module.exports = function(Self) {
|
||||
require('../methods/item/crudItemTags.js')(Self);
|
||||
};
|
Loading…
Reference in New Issue