import { includes, words, capitalize, extend } from 'lodash'; import template from './parameters.html'; import parameterSettingsTemplate from './parameter-settings.html'; function humanize(str) { return capitalize(words(str).join(' ')); } const ParameterSettingsComponent = { template: parameterSettingsTemplate, bindings: { resolve: '<', close: '&', dismiss: '&', }, controller($sce, Query) { 'ngInject'; this.trustAsHtml = html => $sce.trustAsHtml(html); this.parameter = this.resolve.parameter; this.isNewParameter = this.parameter.name === ''; this.shouldGenerateTitle = this.isNewParameter && this.parameter.title === ''; this.parameterAlreadyExists = name => includes(this.resolve.existingParameters, name); if (this.parameter.queryId) { Query.get({ id: this.parameter.queryId }, (query) => { this.queries = [query]; }); } this.searchQueries = (term) => { if (!term || term.length < 3) { return; } Query.query({ q: term }, (results) => { this.queries = results.results; }); }; this.updateTitle = () => { if (this.shouldGenerateTitle) { this.parameter.title = humanize(this.parameter.name); } }; }, }; function ParametersDirective($location, $uibModal) { return { restrict: 'E', transclude: true, scope: { parameters: '=', syncValues: '=?', editable: '=?', changed: '&onChange', }, template, link(scope) { // is this the correct location for this logic? if (scope.syncValues !== false) { scope.$watch( 'parameters', () => { if (scope.changed) { scope.changed({}); } const params = extend({}, $location.search()); scope.parameters.forEach((param) => { extend(params, param.toUrlParams()); }); $location.search(params); }, true, ); } scope.showParameterSettings = (param) => { $uibModal.open({ component: 'parameterSettings', resolve: { parameter: param, }, }); }; }, }; } export default function init(ngModule) { ngModule.directive('parameters', ParametersDirective); ngModule.component('parameterSettings', ParameterSettingsComponent); } init.init = true;