diff --git a/client/core/src/components/autocomplete/autocomplete.js b/client/core/src/components/autocomplete/autocomplete.js index 2d4e5facc..e7589313e 100755 --- a/client/core/src/components/autocomplete/autocomplete.js +++ b/client/core/src/components/autocomplete/autocomplete.js @@ -34,7 +34,6 @@ export default class Autocomplete extends Input { this.assignDropdownProps(); this.showField = this.$.dropDown.showField; this.valueField = this.$.dropDown.valueField; - this.linked = true; this.refreshSelection(); } @@ -43,8 +42,7 @@ export default class Autocomplete extends Input { } set model(value) { - this._model = value; - this.assignDropdownProps(); + this.dropDownAssign({model: value}); } get data() { @@ -52,8 +50,7 @@ export default class Autocomplete extends Input { } set data(value) { - this._data = value; - this.assignDropdownProps(); + this.dropDownAssign({data: value}); } get url() { @@ -61,8 +58,14 @@ export default class Autocomplete extends Input { } set url(value) { - this._url = value; - this.assignDropdownProps(); + this.dropDownAssign({url: value}); + } + + dropDownAssign(props) { + for (let prop in props) + this[`_${prop}`] = props[prop]; + if (this.$.dropDown) + Object.assign(this.$.dropDown, props); } /** @@ -101,54 +104,55 @@ export default class Autocomplete extends Input { } refreshSelection() { - if (this.selectionIsValid(this._selection)) + if (this._field == null) { + this.selection = null; + return; + } + + if (!(this.valueField && this.showField) + || this.selectionIsValid(this._selection)) return; - let value = this._field; - - if (value && this.linked) { - if (this.selectionIsValid(this.initialData)) { - this.selection = this.initialData; - return; - } - - if (this.$.dropDown) { - let data; - if (this.$.dropDown.model) - data = this.$.dropDown.model.orgData; - - if (data) - for (let i = 0; i < data.length; i++) - if (data[i][this.valueField] === value) { - this.selection = data[i]; - return; - } - - this.requestSelection(value); - } - } else - this.selection = null; + this.selection = this.fetchSelection(); } - requestSelection(value) { - if (!this.url) return; - let where = {}; + fetchSelection() { + if (this.selectionIsValid(this.initialData)) + return this.initialData; - if (this.multiple) - where[this.valueField] = {inq: this.field}; - else - where[this.valueField] = value; + if (!this.$.dropDown) + return null; - let filter = { - fields: this.$.dropDown.getFields(), - where: where - }; + let data; + if (this.$.dropDown.model) + data = this.$.dropDown.model.orgData; - let json = encodeURIComponent(JSON.stringify(filter)); - this.$http.get(`${this.url}?filter=${json}`).then( - json => this.onSelectionRequest(json.data), - () => this.onSelectionRequest(null) - ); + if (data) { + let selection = data.find(i => this.selectionIsValid(i)); + if (selection) return selection; + } + + if (this.url) { + let where = {}; + + if (this.multiple) + where[this.valueField] = {inq: this.field}; + else + where[this.valueField] = this._field; + + let filter = { + fields: this.$.dropDown.getFields(), + where: where + }; + + let json = encodeURIComponent(JSON.stringify(filter)); + this.$http.get(`${this.url}?filter=${json}`).then( + json => this.onSelectionRequest(json.data), + () => this.onSelectionRequest() + ); + } + + return null; } onSelectionRequest(data) { @@ -157,12 +161,8 @@ export default class Autocomplete extends Input { this.selection = data; else this.selection = data[0]; - } else { - let selection = {}; - selection[this.showField] = this._field; - selection[this.valueField] = this._field; - this.selection = selection; - } + } else + this.selection = null; } refreshDisplayed() { diff --git a/client/core/src/components/autocomplete/autocomplete.spec.js b/client/core/src/components/autocomplete/autocomplete.spec.js index adb4415f6..492e262ec 100644 --- a/client/core/src/components/autocomplete/autocomplete.spec.js +++ b/client/core/src/components/autocomplete/autocomplete.spec.js @@ -1,74 +1,58 @@ -import './autocomplete.js'; -import template from './autocomplete.html'; - describe('Component vnAutocomplete', () => { let $element; - let $scope; - let $httpBackend; - let controller; - + let $ctrl; let data = {id: 1, name: 'Bruce Wayne'}; - beforeEach(() => { - angular.mock.module('client'); - }); + beforeEach(ngModule('vnCore')); - beforeEach(angular.mock.inject((_$componentController_, $rootScope, _$httpBackend_, _$timeout_) => { - $scope = $rootScope.$new(); - $element = angular.element(`
${template}
`); - $httpBackend = _$httpBackend_; - $httpBackend.when('GET', /\/locale\/\w+\/[a-z]{2}\.json/).respond({}); - controller = _$componentController_('vnAutocomplete', {$element, $scope, $httpBackend, $transclude: null}); + beforeEach(angular.mock.inject(($compile, $rootScope) => { + $element = $compile(``)($rootScope); + $ctrl = $element.controller('vnAutocomplete'); })); - describe('url() setter', () => { - it(`should set url controllers property and call refreshSelection`, () => { - spyOn(controller, "refreshSelection"); - controller.url = "url"; + afterEach(() => { + $element.remove(); + }); - expect(controller.url).toEqual("url"); - expect(controller.refreshSelection).toHaveBeenCalledWith(); + describe('url() setter', () => { + it(`should set the url property`, () => { + $ctrl.url = '/TestModels'; + + expect($ctrl.url).toEqual('/TestModels'); }); }); describe('field() setter/getter', () => { - it(`should set field controllers property`, () => { - controller.field = data.id; + it(`should set the field property`, () => { + $ctrl.field = 'id'; - expect(controller.field).toEqual(data.id); + expect($ctrl.field).toEqual('id'); }); + }); + describe('selection property', () => { it(`should set selection finding an existing item in the initialData property`, () => { - controller.valueField = 'id'; - controller.showField = 'name'; - controller.initialData = data; - controller.field = data.id; + $ctrl.initialData = data; + $ctrl.field = data.id; - expect(controller.selection).toEqual(data); + expect($ctrl.selection).toEqual(data); }); it(`should set selection finding an existing item in the data property`, () => { - controller.valueField = 'id'; - controller.showField = 'name'; - controller.data = [data]; - controller.field = data.id; + $ctrl.data = [data]; + $ctrl.field = data.id; - expect(controller.selection).toEqual(data); + expect($ctrl.selection).toEqual(data); }); it(`should set selection to null when can't find an existing item in the data property`, () => { - controller.valueField = 'id'; - controller.showField = 'name'; - controller.field = data.id; + $ctrl.field = data.id; - expect(controller.selection).toEqual(null); + expect($ctrl.selection).toEqual(null); }); - it(`should perform a query if the item id isn't present in the data property`, () => { - controller.valueField = 'id'; - controller.showField = 'name'; - controller.url = 'localhost'; - controller.field = data.id; + it(`should perform a query if the item id isn't present in the data property`, inject($httpBackend => { + $ctrl.url = '/TestModels'; let filter = { fields: ['id', 'name'], @@ -76,10 +60,11 @@ describe('Component vnAutocomplete', () => { }; let json = encodeURIComponent(JSON.stringify(filter)); - $httpBackend.whenGET(`localhost?filter=${json}`).respond({}); - $httpBackend.expectGET(`localhost?filter=${json}`); - controller.field = data.id; + $httpBackend.whenGET(`${$ctrl.url}?filter=${json}`).respond([data]); + $ctrl.field = data.id; $httpBackend.flush(); - }); + + expect($ctrl.selection).toEqual(data); + })); }); }); diff --git a/client/core/src/components/drop-down/drop-down.js b/client/core/src/components/drop-down/drop-down.js index fefbbf5c4..9ac458f34 100755 --- a/client/core/src/components/drop-down/drop-down.js +++ b/client/core/src/components/drop-down/drop-down.js @@ -20,6 +20,7 @@ export default class DropDown extends Component { this.$filter = $filter; this.valueField = 'id'; + this.showField = 'name'; this._search = undefined; this._activeOption = -1; this.showLoadMore = true; diff --git a/client/core/src/components/drop-down/drop-down.spec.js b/client/core/src/components/drop-down/drop-down.spec.js index 8b6647c36..f3e84fcab 100644 --- a/client/core/src/components/drop-down/drop-down.spec.js +++ b/client/core/src/components/drop-down/drop-down.spec.js @@ -1,56 +1,32 @@ -import './drop-down.js'; -import template from './drop-down.html'; - describe('Component vnDropDown', () => { - let $componentController; - let $timeout; let $element; - let $scope; - let $httpBackend; - let $transitions; - let $q; - let $filter; - let controller; + let $ctrl; - beforeEach(() => { - angular.mock.module('client'); - }); + beforeEach(ngModule('vnCore')); - beforeEach(angular.mock.inject((_$componentController_, $rootScope, _$timeout_, _$httpBackend_, _$q_, _$filter_, _$transitions_) => { - $componentController = _$componentController_; - $element = angular.element(`
${template}
`); - $timeout = _$timeout_; - $transitions = _$transitions_; - $q = _$q_; - $filter = _$filter_; - $scope = $rootScope.$new(); - $httpBackend = _$httpBackend_; - $httpBackend.when('GET', /\/locale\/\w+\/[a-z]{2}\.json/).respond({}); - - let popoverTemplate = require('../popover/popover.html'); - let $popover = angular.element(`
${popoverTemplate}
`); - $scope.popover = $componentController('vnPopover', {$element: $popover, $scope, $timeout, $transitions}); - $scope.popover.$postLink(); - - $scope.model = $componentController('vnRestModel', {$httpBackend, $q, $filter}); - controller = $componentController('vnDropDown', {$element, $scope, $transclude: null, $timeout, $httpBackend, $translate: null}); - controller.$postLink(); - controller.parent = angular.element('')[0]; + beforeEach(inject(($compile, $rootScope, $document) => { + $element = $compile(``)($rootScope); + $document.find('body').append($element); + $ctrl = $element.controller('vnDropDown'); })); - describe('show() method', () => { - it(`should define controllers _show using the value received as argument`, () => { - controller.show(); + afterEach(() => { + $element.remove(); + }); - expect(controller.shown).toEqual(true); + describe('show() method', () => { + it(`should enable the show property`, () => { + $ctrl.show(); + + expect($ctrl.shown).toEqual(true); }); }); describe('hide() method', () => { - it(`should define controllers _show using the value received as argument`, () => { - controller.hide(); + it(`should disable the show property`, () => { + $ctrl.hide(); - expect(controller.shown).toEqual(false); + expect($ctrl.shown).toEqual(false); }); }); }); diff --git a/client/core/src/components/icon-menu/icon-menu.html b/client/core/src/components/icon-menu/icon-menu.html index 41bb78250..3a3f78218 100644 --- a/client/core/src/components/icon-menu/icon-menu.html +++ b/client/core/src/components/icon-menu/icon-menu.html @@ -1,5 +1,7 @@
-