"use strict"; /** * Value object for panel descriptor. * * @author Igor Samulenko * @constructor */ function PanelVO() { // simple fields this.name = ''; this.type = ''; this.columnCount = 0; // complex fields this.fields = []; // derived fields this.parentScreenId = null; this.parentScreenTitle = ''; this.parentScreenName = ''; this.shortId = ''; this.dataSource = ''; this.addedFields = []; this.removedFields = []; this.sectionName = ''; // flags this.addFieldAllowed = true; } /** * Setup inheritance chain. */ PanelVO.prototype = Object.create(BaseVO.prototype); PanelVO.prototype.constructor = PanelVO; /** * Constants section. */ PanelVO.prototype.MAX_FIELDS = Number.MAX_VALUE; PanelVO.prototype.MAX_FIELDS_BY_PANELS = { titleBar: 5, titleBarPanel: 5 }; PanelVO.prototype.FIELD_RESTRICTIONS_BY_PANEL = { titleBar: function (field) { return field.isPriorityWidget() || !field.isWidget(); }, titleBarPanel: function (field) { return field.isPriorityWidget() || field.isChangeRiskBadgeWidget() || field.isChangeClassWidget() || !field.isWidget(); } }; /** * @override * @return {Array} */ PanelVO.prototype.getProps = function () { return BaseVO.prototype.getProps().concat('name', 'type', 'columnCount', 'fields'); }; /** * @override */ PanelVO.prototype.postBuild = function () { this.fields = _.sortBy(this.fields.map(function (item) { return new FieldVO().build(item); }), 'displayOrder'); this.shortId = this.name.substr(0, 1).toLowerCase() + this.name.substr(1).replace(' ', ''); processGroupFields(this.fields); }; /** * Returns true if panel contains specified field * @param {FieldVO} field * @returns {boolean} */ PanelVO.prototype.contains = function (field) { return this.fields.some(function (item) { return item.name === field.name; }); }; /** * Returns true if panel has available field slots * @returns {boolean} */ PanelVO.prototype.hasAvailableSlots = function () { var realFieldLength = 0; this.fields.forEach(function (field) { if (!field.isGroupField()) { realFieldLength++; } else { realFieldLength += field.members.length; } }); return realFieldLength < this.getPanelMaxFields(); }; /** * Add field to panel * @param {FieldVO} field to add */ PanelVO.prototype.addField = function (field) { if (field.id) { // add removed field back var removedIndex = this.removedFields.indexOf(field); if (removedIndex > -1) { this.removedFields.splice(removedIndex, 1); } else { this.addedFields.push(field); } } else { this.addedFields.push(field); } if (field.dataType === 'group') { this.fields.push(field); } else { if (_.findIndex(this.fields, { name: field.name }) === -1) { this.fields.push(field); } } this.addFieldAllowed = this.hasAvailableSlots(); }; /** * Removes field from panel * @param {FieldVO} field to remove */ PanelVO.prototype.removeField = function (field) { var index = this.fields.indexOf(field); if (index > -1) { this.fields.splice(index, 1); if (field.id) { if (!field.isNewField) { this.removedFields.push(field); } _.remove(this.addedFields, { name: field.name }); } else { _.remove(this.addedFields, { name: field.name }); } } this.addFieldAllowed = this.hasAvailableSlots(); }; /** * Removes group field from panel * @param {FieldVO} group field to remove */ PanelVO.prototype.removeFieldFromGroup = function (field) { if (!field.isNewField) { this.removedFields.push(field); } else { _.remove(this.addedFields, { name: field.name }); } this.addFieldAllowed = this.hasAvailableSlots(); }; /** * Removes widget member field. Should be called upon widget field removal * @param {FieldVO} Widget member field to remove */ PanelVO.prototype.removeWidgetMemberField = function (field) { if (field.id) { this.removedFields.push(field); } else { _.remove(this.addedFields, { name: field.name }); } this.addFieldAllowed = this.hasAvailableSlots(); }; /** * Inits fields (label and sealed properties) * @param {Object} fieldDictionary */ PanelVO.prototype.initFields = function (fieldDictionary) { var panelShortId = this.shortId; var newFields = []; if (panelShortId === "typeSpecific") { var classId = this.classId; _.each(this.fields, function (field) { for (var i = 0; i < field.extension.length; i++) { if (field.extension[i].classId === classId) { newFields.push(field); break; } } }); this.fields = newFields; } this.fields = _.reject(this.fields, function (field) { return !!field.groupMember; }); this.fields.forEach(function (field) { if (fieldDictionary[field.name]) { field.label = fieldDictionary[field.name].label; field.sealed = fieldDictionary[field.name].isSealed(); } }); }; /** * Returns true if all required field values are filled * * @returns {boolean} */ PanelVO.prototype.isValid = function () { return this.fields.filter(function (field) { return !field.isValid(); }).length === 0; }; /** * Returns true if panel has custom fields * * @returns {boolean} */ PanelVO.prototype.hasCustomFields = function () { return this.fields && this.fields.length; }; /** * Initialise value field names with diffFields * */ PanelVO.prototype.initValueFieldNames = function (valueFieldNames) { this.fields.forEach(function (field) { if (field.linkedFieldExist) { field.valueFieldName = valueFieldNames[field.valueField]; } field.members.forEach(function (gField) { if (gField.linkedFieldExist) { gField.valueFieldName = valueFieldNames[gField.valueField]; } }); }); }; PanelVO.prototype.getPanelMaxFields = function () { return this.MAX_FIELDS_BY_PANELS[this.name] || this.MAX_FIELDS; }; PanelVO.prototype.isFieldAvailableOnPanel = function (field) { var handler = this.FIELD_RESTRICTIONS_BY_PANEL[this.name]; return handler ? handler(field) : true; }; PanelVO.prototype.isHeaderSection = function () { return ['titleBar', 'titleBarPanel'].indexOf(this.name) !== -1; }; function processGroupFields(fields) { var groupFields = _.filter(fields, function (field) { return field.isGroupField(); }); _.forEach(groupFields, function (groupField) { groupField.members = _.sortBy(_.map(groupField.members, function (member) { var obj = _.find(fields, { name: member.name }); if (obj) { obj.current = member; } return obj || new FieldVO().build(member); }), 'displayOrder'); }); }