salix/front/salix/components/descriptor/index.js

139 lines
3.1 KiB
JavaScript

import ngModule from '../../module';
import Component from 'core/lib/component';
import './style.scss';
import './quick-link';
/**
* Small card with basing entity information and actions.
*/
export default class Descriptor extends Component {
constructor($element, $, vnReport, vnEmail) {
super($element, $);
this.vnReport = vnReport;
this.vnEmail = vnEmail;
}
$postLink() {
super.$postLink();
const content = this.element.querySelector('vn-descriptor-content');
if (!content) throw new Error('Directive vnDescriptorContent not found');
angular.element(content)
.controller('vnDescriptorContent')
.descriptor = this;
}
get id() {
return this._id;
}
set id(value) {
this.load(value);
}
get entity() {
return this._entity;
}
set entity(value) {
this._entity = value;
this._id = value && value.id;
}
load(id) {
if (id == this._id)
return this.$q.resolve();
this._id = id;
if (!id) {
this.entity = null;
return this.$q.resolve();
}
return this.loadData();
}
/**
* Reloads the descriptor data. Should be implemented or overriden by child
* classes.
*/
loadData() {
throw new Error('DescriptorPopover::loadData() method not implemented');
}
/**
* Performs a cancellable request.
*
* @param {String} url The http path
* @param {Object} options The request options
* @return {Promise} Resolved with request response
*/
getData(url, options) {
if (this.canceler) this.canceler.resolve();
this.canceler = this.$q.defer();
this.entity = null;
options = Object.assign(options || {}, {
timeout: this.canceler.promise
});
return this.$http.get(url, options)
.finally(() => this.canceler = null);
}
}
Descriptor.$inject = ['$element', '$scope', 'vnReport', 'vnEmail'];
ngModule.vnComponent('vnDescriptor', {
controller: Descriptor,
bindings: {
entity: '<?',
id: '<?'
},
transclude: {
btnOne: '?btnOne',
btnTwo: '?btnTwo',
btnThree: '?btnThree',
btnFour: '?btnFour',
btnFive: '?btnFive'
}
});
export class DescriptorContent {
constructor($transclude, vnModules) {
this.$transclude = $transclude;
this.moduleMap = vnModules.getMap();
}
get summaryState() {
return `${this.baseState || this.module}.card.summary`;
}
get indexState() {
return this.baseState || `${this.module}.index`;
}
}
DescriptorContent.$inject = ['$transclude', 'vnModules'];
ngModule.vnComponent('vnDescriptorContent', {
template: require('./index.html'),
controller: DescriptorContent,
bindings: {
module: '@',
baseState: '@?',
description: '<',
descriptor: '<?',
summary: '<?'
},
transclude: {
body: 'slotBody',
before: '?slotBefore',
after: '?slotAfter',
menu: '?slotMenu',
dotMenu: '?slotDotMenu'
}
});