UNPKG

fabric8-planner

Version:
549 lines 23.4 kB
var __extends = (this && this.__extends) || (function () { var extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __assign = (this && this.__assign) || Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; import { Injectable } from '@angular/core'; import { createEntityAdapter } from '@ngrx/entity'; // MemoizedSelector is needed even if it's not being used in this file // Else you get this error // Exported variable 'workItemSelector' has or is using name 'MemoizedSelector' // from external module "@ngrx/store/src/selector" but cannot be named. import { createFeatureSelector, createSelector, select, Store } from '@ngrx/store'; import { orderBy } from 'lodash'; import { combineLatest } from 'rxjs'; import { filter, map, switchMap } from 'rxjs/operators'; import { AreaQuery } from './area.model'; import { cleanObject, modelService, switchModel } from './common.model'; import { IterationQuery } from './iteration.model'; import { LabelMapper, LabelQuery } from './label.model'; import { plannerSelector } from './space'; import { UserQuery } from './user'; import { WorkItemTypeMapper, WorkItemTypeQuery } from './work-item-type'; var WorkItem = /** @class */ (function (_super) { __extends(WorkItem, _super); function WorkItem() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.attributes = {}; return _this; } return WorkItem; }(modelService)); export { WorkItem }; var WorkItemRelations = /** @class */ (function () { function WorkItemRelations() { } return WorkItemRelations; }()); export { WorkItemRelations }; var RelationalData = /** @class */ (function () { function RelationalData() { } return RelationalData; }()); export { RelationalData }; var LinkDict = /** @class */ (function () { function LinkDict() { } return LinkDict; }()); export { LinkDict }; var workItemAdapter = createEntityAdapter(); var _a = workItemAdapter.getSelectors(), selectIds = _a.selectIds, selectEntities = _a.selectEntities, selectAll = _a.selectAll, selectTotal = _a.selectTotal; var WorkItemMapper = /** @class */ (function () { function WorkItemMapper() { this.wiTypeMapper = new WorkItemTypeMapper(); this.labelMapper = new LabelMapper(); this.serviceToUiMapTree = [{ fromPath: ['id'], toPath: ['id'] }, { fromPath: ['attributes', 'system.title'], toPath: ['title'] }, { fromPath: ['attributes', 'system.number'], toPath: ['number'] }, { fromPath: ['attributes', 'system.order'], toPath: ['order'] }, { fromPath: ['attributes', 'system.created_at'], toPath: ['createdAt'] }, { fromPath: ['attributes', 'system.updated_at'], toPath: ['updatedAt'] }, { fromPath: ['attributes', 'system.state'], toPath: ['state'] }, { fromPath: ['attributes', 'system.description.markup'], toPath: ['descriptionMarkup'] }, { fromPath: ['attributes', 'system.description.rendered'], toPath: ['descriptionRendered'] }, { fromPath: ['attributes', 'system.description'], toPath: ['description'] }, { fromPath: ['attributes', 'version'], toPath: ['version'] }, { fromPath: ['links', 'self'], toPath: ['link'] }, { fromPath: ['relationships', 'workItemLinks', 'links', 'related'], toPath: ['WILinkUrl'] }, { fromPath: ['relationships', 'area', 'data', 'id'], toPath: ['areaId'] }, { fromPath: ['relationships', 'creator', 'data', 'id'], toPath: ['creator'] }, { fromPath: ['relationships', 'iteration', 'data', 'id'], toPath: ['iterationId'] }, { fromPath: ['relationships', 'baseType', 'data', 'id'], toPath: ['type'] }, { fromPath: ['relationships', 'comments', 'links', 'related'], toPath: ['commentLink'] }, { fromPath: ['relationships', 'events', 'links', 'related'], toPath: ['eventLink'] }, { fromPath: ['relationships', 'assignees', 'data'], toPath: ['assignees'], toFunction: function (assignees) { if (!assignees) { return []; } return assignees.map(function (assignee) { return assignee.id; }); } }, { fromPath: ['relationships', 'labels', 'data'], toPath: ['labels'], toFunction: function (labels) { if (!labels) { return []; } return labels.map(function (label) { return label.id; }); } }, { toPath: ['children'], toValue: [] }, { fromPath: ['relationships', 'children', 'meta', 'hasChildren'], toPath: ['hasChildren'] }, { fromPath: ['relationships', 'parent', 'data', 'id'], toPath: ['parentID'] }, { fromPath: ['relationships', 'children', 'links', 'related'], toPath: ['childrenLink'] }, { fromPath: ['relationships', 'children', 'meta', 'hasChildren'], toPath: ['treeStatus'], toFunction: function (hasChildren) { if (!hasChildren) { return 'disabled'; } else { return 'collapsed'; } } }, { toPath: ['childrenLoaded'], toValue: false }, { toPath: ['bold'], toValue: false }, { toPath: ['editable'], toValue: false }, { fromPath: ['relationships', 'boardcolumns', 'data'], toPath: ['columnIds'], toFunction: function (data) { return Array.isArray(data) ? data.map(function (col) { return col.id; }) : null; } } ]; this.uiToServiceMapTree = [{ toPath: ['id'], fromPath: ['id'] }, { fromPath: ['title'], toPath: ['attributes', 'system.title'] }, { fromPath: ['number'], toPath: ['attributes', 'system.number'] }, { fromPath: ['order'], toPath: ['attributes', 'system.order'] }, { fromPath: ['createdAt'], toPath: ['attributes', 'system.created_at'] }, { fromPath: ['updatedAt'], toPath: ['attributes', 'system.updated_at'] }, { fromPath: ['state'], toPath: ['attributes', 'system.state'] }, { fromPath: ['descriptionRendered'], toPath: ['attributes', 'system.description.rendered'] }, { fromPath: ['description'], toPath: ['attributes', 'system.description'] }, { fromPath: ['description'], toPath: ['attributes', 'system.description.markup'], toFunction: function (val) { if (val) { return 'Markdown'; } return null; } }, { fromPath: ['version'], toPath: ['attributes', 'version'] }, { fromPath: ['link'], toPath: ['links', 'self'] }, { fromPath: ['WILinkUrl'], toPath: ['relationships', 'workItemLinks', 'links', 'related'] }, { fromPath: ['areaId'], toPath: ['relationships', 'area', 'data', 'id'] }, { toPath: ['relationships', 'area', 'data', 'type'], toValue: 'areas' }, { fromPath: ['creator'], toPath: ['relationships', 'creator', 'data', 'id'] }, { toPath: ['relationships', 'creator', 'data', 'type'], toValue: 'identities' }, { fromPath: ['iterationId'], toPath: ['relationships', 'iteration', 'data'], toFunction: function (id) { return { id: id, type: 'iterations' }; } }, { fromPath: ['type'], toPath: ['relationships', 'baseType', 'data', 'id'] }, { toPath: ['relationships', 'baseType', 'data', 'type'], toValue: 'workitemtypes' }, { fromPath: ['commentLink'], toPath: ['relationships', 'comments', 'links', 'related'] }, { fromPath: ['eventLink'], toPath: ['relationships', 'events', 'links', 'related'] }, { fromPath: ['assignees'], toPath: ['relationships', 'assignees', 'data'], toFunction: function (assignees) { if (!assignees) { return null; } return assignees.map(function (assigneeId) { return { id: assigneeId, type: 'identities' }; }); } }, { fromPath: ['labels'], toPath: ['relationships', 'labels', 'data'], toFunction: function (labels) { var _this = this; if (!labels) { return null; } return labels.map(function (label) { return cleanObject(_this.labelMapper.toServiceModel({ id: label }), ['attributes', 'links', 'relationships']); }); }.bind(this) }, { toPath: ['relationships', 'boardcolumns', 'data'], fromPath: ['columnIds'], toFunction: function (data) { if (!data) { return null; } return data.map(function (id) { return { id: id, type: 'boardcolumns' }; }); } }, { fromPath: ['hasChildren'], toPath: ['relationships', 'children', 'meta', 'hasChildren'] }, { fromPath: ['parentID'], toPath: ['relationships', 'parent', 'data', 'id'] }, { fromPath: ['childrenLink'], toPath: ['relationships', 'children', 'links', 'related'] }, { toPath: ['type'], toValue: 'workitems' } ]; } WorkItemMapper.prototype.toDynamicUIModel = function (arg, dynamicFields) { var serviceToDyanmicUiMapTree = []; for (var i = 0; i < dynamicFields.length; i++) { serviceToDyanmicUiMapTree.push({ toPath: ['dynamicfields', dynamicFields[i]], fromPath: ['attributes', dynamicFields[i]] }); } return switchModel(arg, serviceToDyanmicUiMapTree); }; WorkItemMapper.prototype.toDyanmicServiceModel = function (arg, dynamicFields) { var dynamicUiToServiceMapTree = []; for (var i = 0; i < dynamicFields.length; i++) { dynamicUiToServiceMapTree.push({ toPath: ['attributes', dynamicFields[i]], fromPath: ['dynamicfields', dynamicFields[i]] }); } var serviceModel = switchModel(arg, dynamicUiToServiceMapTree); return cleanObject(serviceModel); }; WorkItemMapper.prototype.toUIModel = function (arg) { return switchModel(arg, this.serviceToUiMapTree); }; WorkItemMapper.prototype.toServiceModel = function (arg) { var serviceModel = switchModel(arg, this.uiToServiceMapTree); // Removing relationship part of iteration if (serviceModel.relationships.iteration.data !== null) { serviceModel.relationships.iteration.data = cleanObject(serviceModel.relationships.iteration.data, ['relationships']); } // Removing attributes from assignees if (serviceModel.relationships.assignees.data !== null) { serviceModel.relationships.assignees.data = serviceModel.relationships.assignees.data.map(function (a) { return cleanObject(a, ['attributes']); }); } // Removing relationship part of baseType if (serviceModel.relationships.baseType.data !== null) { serviceModel.relationships.baseType.data = cleanObject(serviceModel.relationships.baseType.data, ['relationships']); } return cleanObject(serviceModel); }; WorkItemMapper.prototype.cleanModel = function (arg, keysToRemove) { if (keysToRemove === void 0) { keysToRemove = []; } return cleanObject(arg, keysToRemove); }; return WorkItemMapper; }()); export { WorkItemMapper }; export var workItemSelector = createSelector(plannerSelector, // TODO // This is a HACK till fabric8-ui removes the unnecessary planner imports // it should just be // state => state.workItems function (state) { return state ? state.workItems : { entities: {}, ids: [] }; }); // should never be exported var workItemEntities = createSelector(workItemSelector, selectEntities); export var getAllWorkItemSelector = createSelector(workItemSelector, selectAll); export var workItemDetailSelector = createFeatureSelector('detailPage'); export var workItemInDetailSelector = createSelector(workItemDetailSelector, function (state) { return state.workItem; }); var WorkItemQuery = /** @class */ (function () { function WorkItemQuery(store, userQuery, iterationQuery, areaQuery, labelQuery, workItemTypeQuery) { this.store = store; this.userQuery = userQuery; this.iterationQuery = iterationQuery; this.areaQuery = areaQuery; this.labelQuery = labelQuery; this.workItemTypeQuery = workItemTypeQuery; this.workItemSource = this.store.pipe(select(getAllWorkItemSelector)); } WorkItemQuery.prototype.resolveWorkItem = function (workItem) { return __assign({}, workItem, { typeObs: this.workItemTypeQuery.getWorkItemTypeWithChildrenById(workItem.type), creatorObs: this.userQuery.getUserObservableById(workItem.creator), assigneesObs: this.userQuery.getUserObservablesByIds(workItem.assignees), iterationObs: this.iterationQuery.getIterationObservableById(workItem.iterationId), areaObs: this.areaQuery.getAreaObservableById(workItem.areaId), labelsObs: this.labelQuery.getLabelObservablesByIds(workItem.labels) }); }; WorkItemQuery.prototype.getWorkItems = function () { var _this = this; return this.workItemSource.pipe(map(function (workItems) { return workItems.map(_this.resolveWorkItem.bind(_this)); })); }; WorkItemQuery.prototype.getWorkItem = function (number) { return this.store.pipe(select(workItemInDetailSelector), filter(function (item) { return item !== null; }), map(this.resolveWorkItem.bind(this)), switchMap(this.setWorkItemsEditable.bind(this))); }; /** * @description set property workItem.editable: true * IF loggedInUser is a Collaborator or creator of WorkItem * @param WorkItemUI || @param WorkItemUI[] * @return Observable<WorkItemUI> || @return Observable<WorkItemUI[]> */ WorkItemQuery.prototype.setWorkItemsEditable = function (workItems) { var items = Array.isArray(workItems) ? workItems : [workItems]; return combineLatest(this.userQuery.getLoggedInUser, this.userQuery.getCollaboratorIds).pipe(map(function (_a) { var loggdInuser = _a[0], collabIDs = _a[1]; return items.map(function (item) { var allAllowedIds = loggdInuser ? collabIDs.concat([item.creator]) : []; return __assign({}, item, { editable: allAllowedIds.indexOf(loggdInuser.id) > -1 }); }); }), map(function (items) { return Array.isArray(workItems) ? items : items[0]; })); }; /** * This function returns an observable for the the selector component * with iteration data and the selected iteration flagged * This data can be used in work item detail page for the * iteration selector dropdown. * @param number */ WorkItemQuery.prototype.getIterationsForWorkItem = function (number) { var _this = this; return this.getWorkItem(number) .pipe(filter(function (w) { return !!w; }), switchMap(function (workitem) { return _this.iterationQuery.getIterations().pipe(map(function (iterations) { return orderBy(iterations, 'name', 'asc').map(function (i) { return { key: i.id, value: (i.resolvedParentPath != '/' ? i.resolvedParentPath : '') + '/' + i.name, selected: i.id === workitem.iterationId, cssLabelClass: undefined }; }); })); })); }; /** * This function returns an observable for the the selector component * with area data and the selected area flagged * This data can be used in work item detail page for the * area selector dropdown. * @param number */ WorkItemQuery.prototype.getAreasForWorkItem = function (number) { var _this = this; return this.getWorkItem(number) .pipe(filter(function (w) { return !!w; }), switchMap(function (workItem) { return _this.areaQuery.getAreas().pipe(map(function (areas) { return areas.map(function (area) { return { key: area.id, value: (area.parentPathResolved != '/' ? area.parentPathResolved : '') + '/' + area.name, selected: area.id === workItem.areaId, cssLabelClass: undefined }; }); })); })); }; /** * This function returns an observable for the the selector component * with work item type data and the selected iteration flagged * This data can be used in work item detail page for the * work item type selector dropdown. * @param number */ WorkItemQuery.prototype.getTypesForWorkItem = function (number) { var _this = this; return this.getWorkItem(number).pipe(filter(function (w) { return !!w; }), switchMap(function (workItem) { return _this.workItemTypeQuery.getWorkItemTypes().pipe(map(function (types) { return types.map(function (t) { return { key: t.id, value: t.name, selected: t.id === workItem.type, icon: t.icon ? t.icon : '' }; }); })); })); }; /** * This function returns an observable for the the selector component * with work item state data and the selected iteration flagged * This data can be used in work item detail page for the * work item states selector dropdown. * @param number */ WorkItemQuery.prototype.getStatesForWorkItem = function (number) { var _this = this; return this.getWorkItem(number).pipe(filter(function (w) { return !!w; }), switchMap(function (workItem) { return _this.workItemTypeQuery.getWorkItemTypes().pipe(map(function (types) { return types.find(function (type) { return type.id === workItem.type; }); }), map(function (type) { if (!!!type) { return []; } return type.fields['system.state'].type.values.map(function (s) { return { key: s, value: s, selected: s === workItem.state }; }); })); })); }; Object.defineProperty(WorkItemQuery.prototype, "getWorkItemEntities", { get: function () { return this.store.pipe(select(workItemEntities)); }, enumerable: true, configurable: true }); WorkItemQuery.prototype.getWorkItemsByIds = function (ids) { var _this = this; var selector = createSelector(workItemEntities, function (state) { return ids.length > 0 ? ids.map(function (i) { return state[i]; }) // Sometime .filter(function (item) { return !!item; }) .map(function (item) { return _this.resolveWorkItem(item); }) .sort(function (a, b) { return b.order - a.order; }) : []; }); return this.store.pipe(select(selector), switchMap(this.setWorkItemsEditable.bind(this))); }; WorkItemQuery.prototype.getWorkItemWithId = function (id) { var _this = this; var selector = createSelector(workItemEntities, function (state) { return _this.resolveWorkItem(state[id]); }); return this.store.pipe(select(selector)); }; WorkItemQuery.decorators = [ { type: Injectable }, ]; /** @nocollapse */ WorkItemQuery.ctorParameters = function () { return [ { type: Store, }, { type: UserQuery, }, { type: IterationQuery, }, { type: AreaQuery, }, { type: LabelQuery, }, { type: WorkItemTypeQuery, }, ]; }; return WorkItemQuery; }()); export { WorkItemQuery }; //# sourceMappingURL=work-item.js.map