Skip to content

Instantly share code, notes, and snippets.

@zimmicz
Created July 26, 2018 12:16
Show Gist options
  • Save zimmicz/4fc76e2e78dec33df44dc4e642f9b4b1 to your computer and use it in GitHub Desktop.
Save zimmicz/4fc76e2e78dec33df44dc4e642f9b4b1 to your computer and use it in GitHub Desktop.
es6 controller class
class ConstructionObjectDetailUpdateController {
constructor(
SeriesService,
ModalService,
ChecklistService,
Restangular,
) {
this.seriesService = SeriesService;
this.modalService = ModalService;
this.checklistService = ChecklistService;
this.restangular = Restangular;
}
/**
* Shows the series setup dialog.
*
* @returns {void}
*/
showSeries() {
if (!this.series) {
this.series = this.DEFAULT_SERIES;
} else {
delete this.series;
}
}
/**
* Updates construction object.
*
* @returns {void}
*/
update() {
if (this.loading) {
return;
}
if (this.series) {
this.seriesService.updateSeries(this.series);
this.data.series = this.series.series;
} else {
this.data.series = null;
}
this.data.put({
loadCollections: [
'actingPersons',
'administrator',
],
}).then((res) => { this.closeModal(res); });
}
/**
* Opens a new modal window to update administrator.
*
* @returns {void}
*/
updateAdministrator() {
new this.modalService({
template: '<administrator-update close-modal="closeModal" administrator-id="administratorId"></administrator-update>',
scope: {
administratorId: this.data.administrator.id,
},
}).then((res) => {
if (!res) {
return;
}
res = this.restangular.stripRestangular(res);
res.actingPersons = this.uniqueMergeByKey(this.data.actingPersons, res.actingPersons, 'id');
_.extend(this.data.administrator, res);
});
}
/**
* Handles change of construction object administrator.
* If one of other subjects is chosen for an administrator, offers related acting persons.
*
* @returns {void}
*/
changeAdministrator() {
console.log(this);
this.actingPersons.empty();
if (this.data.administratorType && this.data.administratorType === 'A' && this.data.administrator) {
this.data.administrator.actingPersons = null;
this.loadingActingPersons = true;
this.restangular
.one(this.API_URL.administrators, this.data.administrator.id)
.all(this.API_URL.acting_persons)
.getList()
.then((res) => {
this.data.administrator.actingPersons = this.restangular.stripRestangular(res);
this.loadingActingPersons = false;
});
}
}
reloadAdministratorRegister(reloadFn) {
this.reloadAdministrators = reloadFn;
}
/**
* Handles change of construction object administrator type.
*
* @returns {void}
*/
changeAdministratorType() {
this.data.administrator = null;
this.actingPersons.empty();
}
/**
* Opens a new modal window to create a new administrator.
*
* @returns {void}
*/
createAdministrator() {
new this.modalService({
template: '<administrator-create close-modal="closeModal"></administrator-create>',
}).then((res) => {
if (res) {
this.data.administrator = this.restangular.stripRestangular(res);
this.changeAdministrator();
this.reloadAdministrators();
}
});
}
/**
* Merges unique items from two object arrays by key.
*
* @param {array} arr1 - array of objects
* @param {array} arr2 - array of objects
* @param {string} key - key to compare by
* @returns {array}
*/
uniqueMergeByKey(arr1, arr2, key) {
const include = item => (!item.cancelled && !item.roleContract) || this.actingPersons.isChecked(item);
return _.filter(_.uniqBy(_.union(arr1, arr2), key), include);
}
$onInit() {
this.actingPersons = null; // placeholder
this.administratorTypes = [
{
id: null,
name: 'Neurčen',
},
{
id: 'I',
name: 'Investor je správcem',
},
{
id: 'A',
name: 'Jiný subjekt',
},
];
this.data = null; // placeholder
this.loading = true;
this.loadingActingPersons = false;
this.series = null; // placeholder
this.API_URL = {
acting_persons: 'acting-persons',
administrators: 'administrators',
construction_objects: 'construction-objects',
};
this.DEFAULT_SERIES = {
series: {
cancelled: false,
available: true,
},
sections: [
{
type: 'text',
},
{
type: 'order',
restart: true,
show: false,
},
{
type: 'year',
short: true,
show: false,
},
],
};
this.reloadAdministrators = null; // placeholder
this.restangular
.one(this.API_URL.construction_objects, this.constructionObjectId)
.get({
loadCollections: [
'series',
'actingPersons',
'administrator',
'administration.actingPersons',
],
})
.then((res) => {
this.data = res;
this.actingPersons = new this.checklistService(this.data.actingPersons);
if (res.series) {
this.series = this.seriesService.parseSingleSeries(res.series);
}
if (res.administrator) {
this.data.administrator.actingPersons = this.uniqueMergeByKey(this.data.actingPersons, this.data.administrator.actingPersons, 'id');
}
this.loading = false;
});
}
}
ConstructionObjectDetailUpdateController.$inject = ['SeriesService', 'ModalService', 'ChecklistService', 'Restangular'];
const component = {
template: require('./construction-object-detail-update.html'),
bindings: {
constructionObjectId: '<',
closeModal: '<',
},
controller: ConstructionObjectDetailUpdateController,
};
export default component;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment