123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441 |
- /**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- var App = require('app');
- require('views/common/controls_view');
- /**
- * Common view for config widgets
- * @type {Em.View}
- */
- App.ConfigWidgetView = Em.View.extend(App.SupportsDependentConfigs, App.WidgetPopoverSupport, App.ConvertUnitWidgetViewMixin, App.ServiceConfigCalculateId, {
- /**
- * @type {App.ConfigProperty}
- */
- config: null,
- /**
- * Determines if user hover on widget-view
- * @type {boolean}
- */
- isHover: false,
- /**
- * Determines if widget controls should be disabled
- * @type {boolean}
- */
- disabled: false,
- /**
- * Determines if widget is editable
- * It true - show all control-elements (undo, override, finalize etc) for widget
- * If false - no widget control-elements will be shown
- * Bound from template
- * @type {boolean}
- */
- canEdit: true,
- canNotEdit: Em.computed.not('canEdit'),
- /**
- * Config label class attribute. Displays validation status of config.
- * @type {string}
- */
- configLabelClass: '',
- /**
- * Tab where current widget placed
- * Bound in the template
- * @type {App.Tab}
- */
- tab: null,
- /**
- * Section where current widget placed
- * Bound in the template
- * @type {App.Section}
- */
- section: null,
- /**
- * Subsection where current widget placed
- * Bound in the template
- * @type {App.SubSection}
- */
- subSection: null,
- /**
- * Determines if user can switch custom widget-view to the input-field
- * @type {boolean}
- */
- supportSwitchToCheckBox: false,
- /**
- * @type {boolean}
- */
- showPencil: function () {
- return this.get('supportSwitchToCheckBox') && !this.get('disabled');
- }.property('supportSwitchToCheckBox', 'disabled'),
- /**
- * Alias to <code>config.isOriginalSCP</code>
- * Should be used in the templates
- * Don't use original <code>config.isOriginalSCP</code> in the widget-templates!!!
- * @type {boolean}
- */
- isOriginalSCPBinding: 'config.isOriginalSCP',
- /**
- * Check if property validation failed for overridden property in case when its value is equal to parent
- * config property.
- * @type {boolean}
- */
- isOverrideEqualityError: function() {
- return this.get('config.parentSCP') && this.get('config.parentSCP.value') == this.get('config.value');
- }.property('config.isValid'),
- /**
- * Alias to <code>config.isComparison</code>
- * Should be used in the templates
- * Don't use original <code>config.isComparison</code> in the widget-templates!!!
- * @type {boolean}
- */
- isComparisonBinding: 'config.isComparison',
- classNameBindings:['isComparison:compare-mode', 'config.overrides.length:overridden-property'],
- issueMessage: '',
- issueView: Em.View.extend({
- tagName: 'i',
- classNames: ['icon-warning-sign'],
- classNameBindings: ['issueIconClass'],
- attributeBindings:['issueMessage:data-original-title'],
- /**
- * @type {App.ServiceConfigProperty}
- */
- config: null,
- /**
- * @type {string}
- */
- issueIconClass: '',
- /**
- * @type {string}
- */
- issueMessage: '',
- didInsertElement: function() {
- App.tooltip($(this.get('element')));
- this.errorLevelObserver();
- this.addObserver('issuedConfig.warnMessage', this, this.errorLevelObserver);
- this.addObserver('issuedConfig.errorMessage', this, this.errorLevelObserver);
- },
- willDestroyElement: function() {
- this.removeObserver('issuedConfig.warnMessage', this, this.errorLevelObserver);
- this.removeObserver('issuedConfig.errorMessage', this, this.errorLevelObserver);
- },
- /**
- *
- * @method errorLevelObserver
- */
- errorLevelObserver: function() {
- var messageLevel = this.get('issuedConfig.errorMessage') ? 'ERROR': this.get('issuedConfig.warnMessage') ? 'WARN' : 'NONE';
- var issue = {
- ERROR: {
- iconClass: '',
- message: this.get('issuedConfig.errorMessage'),
- configLabelClass: 'text-error'
- },
- WARN: {
- iconClass: 'warning',
- message: this.get('issuedConfig.warnMessage'),
- configLabelClass: 'text-warning'
- },
- NONE: {
- iconClass: 'hide',
- message: false,
- configLabelClass: ''
- }
- }[messageLevel];
- this.set('parentView.configLabelClass', issue.configLabelClass);
- this.set('issueIconClass', issue.iconClass);
- this.set('issueMessage', issue.message);
- this.set('parentView.issueMessage', issue.message);
- },
- /**
- * @type {App.ServiceConfigProperty}
- */
- issuedConfig: function() {
- var config = this.get('config');
- // check editable override
- if (!config.get('isEditable') && config.get('isOriginalSCP') && config.get('overrides.length') && config.get('overrides').someProperty('isEditable', true)) {
- config = config.get('overrides').findProperty('isEditable', true);
- } else if (config.get('isOriginalSCP') && config.get('isEditable')) {
- // use original config if it is not valid
- if (!config.get('isValid')) {
- return config;
- // scan overrides for non valid values and use it
- } else if (config.get('overrides.length') && config.get('overrides').someProperty('isValid', false)) {
- return config.get('overrides').findProperty('isValid', false);
- }
- }
- return config;
- }.property('config.isEditable', 'config.overrides.length')
- }),
- /**
- * Config name to display.
- * @type {String}
- */
- configLabel: function() {
- return this.get('config.stackConfigProperty.displayName') || this.get('config.displayName') || this.get('config.name');
- }.property('config.name', 'config.displayName'),
- /**
- * Error message computed in config property model
- * @type {String}
- */
- configErrorMessageBinding: 'config.errorMessage',
- /**
- * Determines if config-value was changed
- * @type {boolean}
- */
- valueIsChanged: function () {
- return !Em.isNone(this.get('config.savedValue')) && this.get('config.value') != this.get('config.savedValue');
- }.property('config.value', 'config.savedValue'),
- /**
- * Enable/disable widget state
- * @method toggleWidgetState
- */
- toggleWidgetState: function () {
- this.set('disabled', !this.get('config.isEditable'));
- }.observes('config.isEditable'),
- /**
- * Reset config-value to its default
- * @method restoreValue
- */
- restoreValue: function () {
- var self = this;
- this.set('config.value', this.get('config.savedValue'));
- this.sendRequestRorDependentConfigs(this.get('config')).done(function() {
- self.restoreDependentConfigs(self.get('config'));
- });
- if (this.get('config.supportsFinal')) {
- this.get('config').set('isFinal', this.get('config.savedIsFinal'));
- }
- Em.$('body > .tooltip').remove();
- },
- /**
- * set <code>recommendedValue<code> to config
- * and send request to change dependent configs
- * @method setRecommendedValue
- */
- setRecommendedValue: function() {
- var self = this;
- this.set('config.value', this.get('config.recommendedValue'));
- this.sendRequestRorDependentConfigs(this.get('config')).done(function() {
- if (self.get('config.value') === self.get('config.savedValue')) {
- self.restoreDependentConfigs(self.get('config'));
- }
- });
- if (this.get('config.supportsFinal')) {
- this.get('config').set('isFinal', this.get('config.recommendedIsFinal'));
- }
- Em.$('body > .tooltip').remove();
- },
- /**
- * Determines if override is allowed for <code>config</code>
- * @type {boolean}
- */
- overrideAllowed: function () {
- var config = this.get('config');
- if (!config) return false;
- return config.get('isOriginalSCP') && config.get('isPropertyOverridable') && !this.get('config.isComparison');
- }.property('config.isOriginalSCP', 'config.isPropertyOverridable', 'config.isComparison'),
- /**
- * Determines if undo is allowed for <code>config</code>
- * @type {boolean}
- */
- undoAllowed: function () {
- var config = this.get('config');
- if (!config) return false;
- if (!this.get('isOriginalSCP') || this.get('disabled')) return false;
- return !config.get('cantBeUndone') && config.get('isNotDefaultValue');
- }.property('config.cantBeUndone', 'config.isNotDefaultValue', 'isOriginalSCP', 'disabled'),
- /**
- * Determines if "final"-button should be shown
- * @type {boolean}
- */
- showFinalConfig: function () {
- var config = this.get('config');
- return config.get('isFinal') || (!config.get('isNotEditable') && this.get('isHover'));
- }.property('config.isFinal', 'config.isNotEditable', 'isHover'),
- /**
- *
- * @param {{context: App.ServiceConfigProperty}} event
- * @method toggleFinalFlag
- */
- toggleFinalFlag: function (event) {
- var configProperty = event.context;
- if (configProperty.get('isNotEditable')) {
- return;
- }
- configProperty.toggleProperty('isFinal');
- },
- /**
- * sync widget value with config value when dependent properties
- * have been loaded or changed
- * @method syncValueWithConfig
- */
- syncValueWithConfig: function() {
- this.setValue(this.get('config.value'));
- }.observes('controller.recommendationTimeStamp'),
- didInsertElement: function () {
- App.tooltip($(this.get('element')).find('span'));
- var self = this;
- var element = this.$();
- if (element) {
- element.hover(function() {
- self.set('isHover', true);
- }, function() {
- self.set('isHover', false);
- });
- }
- this.initIncompatibleWidgetAsTextBox();
- },
- /**
- * set widget value same as config value
- * useful for widgets that work with intermediate config value, not original
- * for now used in slider widget
- * @abstract
- */
- setValue: Em.K,
- /**
- * Config group bound property. Needed for correct render process in template.
- *
- * @returns {App.ConfigGroup|Boolean}
- */
- configGroup: function() {
- return !this.get('config.group') || this.get('config.group.isDefault') ? false : this.get('config.group');
- }.property('config.group.name'),
- /**
- * switcher to display config as widget or text field
- * @method toggleWidgetView
- */
- toggleWidgetView: function() {
- if (!this.get('isWidgetViewAllowed')) {
- return false;
- }
- if (this.get('config.showAsTextBox')) {
- this.textBoxToWidget();
- } else {
- this.widgetToTextBox();
- }
- },
- /**
- * switch display of config to text field
- * @method widgetToTextBox
- */
- widgetToTextBox: function() {
- this.set("config.showAsTextBox", true);
- },
- /**
- * switch display of config to widget
- * @method textBoxToWidget
- */
- textBoxToWidget: function() {
- if (this.isValueCompatibleWithWidget()) {
- this.setValue(this.get('config.value'));
- this.set("config.showAsTextBox", false);
- }
- },
- /**
- * check if config value can be converted to config widget value
- * IMPORTANT! Each config-widget that override this method should use <code>updateWarningsForCompatibilityWithWidget</code>
- * @returns {boolean}
- */
- isValueCompatibleWithWidget: function() {
- return (this.get('isOverrideEqualityError') && !this.get('config.isValid')) || this.get('config.isValid');
- },
- /**
- * Initialize widget with incompatible value as textbox
- */
- initIncompatibleWidgetAsTextBox : function() {
- if (!this.isValueCompatibleWithWidget()) {
- this.get('config').set('showAsTextBox', true);
- }
- },
- /**
- * Returns <code>true</code> if raw value can be used by widget or widget view is activated.
- * @returns {Boolean}
- */
- isWidgetViewAllowed: function() {
- if (!this.get('config.showAsTextBox')) {
- return true;
- }
- return this.isValueCompatibleWithWidget();
- }.property('config.value', 'config.isFinal', 'config.showAsTextBox'),
- /**
- * Used in <code>isValueCompatibleWithWidget</code>
- * Updates issue-parameters if config is in the raw-mode
- * @param {string} message empty string if value compatible with widget, error-message if value isn't compatible with widget
- * @method updateWarningsForCompatibilityWithWidget
- */
- updateWarningsForCompatibilityWithWidget: function (message) {
- this.setProperties({
- warnMessage: message,
- 'config.warnMessage': message,
- issueMessage: message,
- configLabelClass: message ? 'text-warning' : ''
- });
- }
- });
|