123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729 |
- 'use strict';
- define(['jquery', 'underscore', '../../lib/@waca/core-client/js/core-client/ui/core/Class', '../../apiHelpers/SlotAPIHelper', '../../widgets/livewidget/nls/StringResources', '../../util/DashboardFormatter', 'com/ibm/vida/vizbundles/extensions/decorations'], function ($, _, Class, SlotAPIHelper, StringResources, Formatter, decorations) {
- 'use strict';
-
- var VIPRDecoratable = Class.extend({
- init: function init(object, context) {
- this.context = context;
-
-
-
-
- this.indexes = context && context.index && {
- dataitem: context.index.dataitem,
- tuple: context.index.tuple,
- tupleitem: context.index.tupleitem
- } || {};
- this.decorations = object ? object.deco || {} : {};
- },
- hasDecoration: function hasDecoration(name) {
- return typeof this.decorations[name] !== 'undefined';
- },
- getDecoration: function getDecoration(name) {
- return this.decorations[name];
- },
-
- decorate: function decorate(name, value) {
-
- var decorationValue = value;
- if (name === 'lines') {
- decorationValue = this._createLinesDecoration(name, value);
- }
-
- if (!(name in this.decorations) || this.decorations[name] !== decorationValue) {
- this.decorations[name] = decorationValue;
- this.context.version++;
- }
- return decorationValue;
- },
- clearDecorations: function clearDecorations() {
- var _this = this;
- var deleted = false;
- _.each(_.keys(this.decorations), function (name) {
- delete _this.decorations[name];
- deleted = true;
- });
- if (deleted) {
- this.context.version++;
- }
- },
- _createLinesDecoration: function _createLinesDecoration(name, value) {
- var existingLinesDecorations = this.getDecoration(name);
- var linesDecorations = [];
- var baseLine = void 0;
- var grabber = void 0;
- var vane = void 0;
- _.each(existingLinesDecorations, function (lineDecoration) {
- if (lineDecoration instanceof decorations.BaseLine) {
- baseLine = lineDecoration;
- } else if (lineDecoration instanceof decorations.Grabber) {
- grabber = lineDecoration;
- } else if (lineDecoration instanceof decorations.Vane) {
- vane = lineDecoration;
- }
- });
-
- var isReDecorate = Array.isArray(value);
- if (isReDecorate) {
- linesDecorations = value;
- } else if (value && !isReDecorate) {
- if (value.baseLine) {
- baseLine = baseLine ? baseLine : this._createBaseline('baseLine');
- if (typeof value.baseLine.position !== 'undefined') {
- baseLine.setPosition(value.baseLine.position);
- }
- if (value.baseLine.color) {
- baseLine.setColor(value.baseLine.color);
- }
- baseLine.setShowInLegend(false || value.baseLine.setShowInLegend);
- linesDecorations.push(baseLine);
- }
- if (value.grabber && baseLine) {
- grabber = grabber ? grabber : this._createGrabber('grabber');
- grabber.setPositionType(value.grabber.positionType);
- baseLine.setGrabber(grabber);
- linesDecorations.push(grabber);
- }
-
- if (value.vane && baseLine) {
- vane = vane ? vane : this._createVane('vane');
- vane.setCaption(value.vane.caption);
- baseLine.setVane(vane);
- linesDecorations.push(vane);
- }
- }
- return linesDecorations;
- },
- _createBaseline: function _createBaseline() {
- var baseLineId = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : _.uniqueId('baseLine');
- return new decorations.BaseLine(baseLineId);
- },
- _createGrabber: function _createGrabber() {
- var grabberId = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : _.uniqueId('grabber');
- return new decorations.Grabber(grabberId);
- },
- _createVane: function _createVane() {
- var vaneId = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : _.uniqueId('vane');
- return new decorations.Vane(vaneId);
- }
- });
-
- var VIPRFormatter = Class.extend({
- init: function init(slotIndex, dataItemIndex, context) {
- this.slot = context.dataset.slotAPIs[slotIndex];
- this.dataItemIndex = dataItemIndex;
- this._format = this.slot.getDataItemList()[dataItemIndex].getFormat();
- },
- format: function format(value) {
- return Formatter.format(value, this._format);
- }
- });
-
- var VIPRItemClass = VIPRDecoratable.extend({
- init: function init(itemClass, context) {
- VIPRItemClass.inherited('init', this, [itemClass, context]);
- this.itemClass = itemClass;
- this.context = context;
- },
- getUniqueName: function getUniqueName() {
- return this.itemClass.u;
- },
- getCaption: function getCaption() {
- var summaryStr = void 0;
- if (!this.context.hideAggTypeInAxisLabels) {
- summaryStr = this.itemClass.aggregate && this.itemClass.aggregate !== 'none' ? ' (' + StringResources.get(this.itemClass.aggregate) + ')' : null;
- }
- var caption = this.itemClass.d ? this.itemClass.d : this.itemClass.u;
- return summaryStr ? caption + summaryStr : caption;
- }
- });
-
- var VIPRItem = VIPRDecoratable.extend({
- init: function init(item, tuple, context, content) {
- VIPRItem.inherited('init', this, [item, context, content]);
- this.item = item;
- this.context = context;
- this.index = context.index.tupleitem;
- this.content = content;
- this.datapoints = tuple.datapoints;
- this.recalculateFormattedValue();
- },
-
- recalculateFormattedValue: function recalculateFormattedValue() {
- var formatter = new VIPRFormatter(this.context.index.dataitem, this.context.index.tupleitem, this.context);
- if (this.item.d === undefined || this.item.d === null) {
- this.formattedValue = formatter.format(null);
- this.unformattedValue = this.formattedValue;
- } else if (this.item.aggregate) {
- var aggregateTypeVisible = !!(!this.content || this.content.getPropertyValue('widget.legend.aggregateTypeVisible'));
- if (aggregateTypeVisible) {
- this.formattedValue = formatter.format(StringResources.get('multiMeasureLabel', { 'measureName': this.item.d.toString(), 'aggregate': StringResources.get(this.item.aggregate.toString()) }));
- this.unformattedValue = this.item.d + this.item.aggregate;
- } else {
- this.formattedValue = formatter.format(StringResources.get('measureLabel', { 'measureName': this.item.d.toString() }));
- this.unformattedValue = this.item.d + this.item.aggregate;
- }
- } else {
- this.formattedValue = formatter.format(this.item.d.toString());
- this.unformattedValue = this.item.d;
- }
- },
- getUniqueName: function getUniqueName() {
- return this.item.u === undefined || this.item.u === null ? this.item.u : this.item.u.toString();
- },
- getCaption: function getCaption() {
- if (this.item.aggregate) {
- var currentAggregationPropertyValue = !this.content || this.content.getPropertyValue('widget.legend.aggregateTypeVisible');
- if (currentAggregationPropertyValue != this.aggregationPropertyValue) {
- this.recalculateFormattedValue();
- this.aggregationPropertyValue = currentAggregationPropertyValue;
- }
- }
- if (!this.formattedValue) {
- this.recalculateFormattedValue();
- }
- return this.formattedValue;
- },
- getParent: function getParent() {
- return this.item.p;
- },
- decorate: function decorate(action, value) {
- if (this.datapoints.length > 0) {
- this.context.version++;
-
- _.each(this.datapoints, function (dp) {
- dp.decorate(action, value);
- });
- }
- }
- });
- var VIPRItem_CaptionAsUnid = VIPRItem.extend({
- getUniqueName: function getUniqueName() {
- return this.getCaption();
- }
- });
-
- var VIPRItemClassSet = VIPRDecoratable.extend({
- init: function init(header, context) {
- VIPRItemClassSet.inherited('init', this, [header, context]);
- this.header = header;
- this.context = context;
- this.itemClasses = [];
- },
- getItemClassCount: function getItemClassCount() {
- return this.header.length;
- },
- getItemClass: function getItemClass(index) {
- if (this.itemClasses[index] === undefined) {
- this.itemClasses[index] = new VIPRItemClass(this.header[index], this.context);
- }
- return this.itemClasses[index];
- }
- });
-
- var VIPRTuple = VIPRDecoratable.extend({
- init: function init(tuple, context, content) {
- VIPRTuple.inherited('init', this, [tuple, context, content]);
- this.tuple = tuple;
- this.context = context;
- this.datapoints = [];
- this.items = [];
- this.dataset = context.dataset;
- this.dataItemIndex = this.context.index.dataitem;
- this.itemClassSetIndex = this.context.index.itemClassSet;
- this.content = content;
- },
- getUniqueName: function getUniqueName() {
- return this.getItem(0).getUniqueName();
- },
-
-
-
- getReference: function getReference() {
- return this.getItem(0).getUniqueName();
- },
- getCaption: function getCaption() {
- return this.getItem(0).getCaption();
- },
- getParent: function getParent() {
- return this.getItem(0).getParent();
- },
- getItemClassSetIndex: function getItemClassSetIndex() {
-
-
- return this.itemClassSetIndex;
- },
- getItem: function getItem(index) {
- if (this.items[index] === undefined) {
-
- this.context.index.tupleitem = index;
- this.context.dataset = this.dataset;
- var isCaptionAsUniqueName = false;
- var dataItemIndex = this.context.index.dataitem;
- if (dataItemIndex >= 0) {
- var oSlotAPI = this.context.dataset.slotAPIs[dataItemIndex];
- isCaptionAsUniqueName = oSlotAPI.isCaptionAsUniqueName && oSlotAPI.isCaptionAsUniqueName();
- }
- var classOfVIPRItem = isCaptionAsUniqueName ? VIPRItem_CaptionAsUnid : VIPRItem;
- this.items[index] = new classOfVIPRItem(this.tuple[index], this, this.context, this.content);
- }
- return this.items[index];
- },
- getItemCount: function getItemCount() {
- return this.tuple ? this.tuple.length : 0;
- },
- addDataPoint: function addDataPoint(datapoint) {
- if (this.datapoints.indexOf(datapoint) === -1) {
- this.datapoints.push(datapoint);
- }
- },
- decorate: function decorate(action, value) {
- VIPRTuple.inherited('decorate', this, [action, value]);
- if (this.datapoints.length > 0) {
- this.context.version++;
-
- _.each(this.datapoints, function (dp) {
- dp.decorate(action, value);
- });
- }
- }
- });
-
- var VIPRCatDataItem = VIPRDecoratable.extend({
- init: function init(dataItem, context, content) {
- VIPRCatDataItem.inherited('init', this, [dataItem._dataItem, context]);
- this.dataItem = dataItem;
- this.context = context;
- this.index = this.context.index.dataitem;
- this.dataset = this.context.dataset;
- this.tuples = [];
- this.itemClassSets = [];
- this.content = content;
- _.each(this.dataItem.getTupleHeaders(), function (entry) {
- this.itemClassSets.push(new VIPRItemClassSet(entry, this.context));
- }.bind(this));
- },
- getUniqueName: function getUniqueName() {},
- postProcessing: function postProcessing() {},
- getCaption: function getCaption() {},
- getType: function getType() {
- return 'cat';
- },
- getTupleCount: function getTupleCount() {
- return this.dataItem ? this.dataItem.getTupleCount() : 0;
- },
- getTuple: function getTuple(index) {
- if (this.tuples[index] === undefined) {
- this.context.dataset = this.dataset;
- this.context.index = {
- dataitem: this.index,
- tuple: index,
- itemClassSet: this.dataItem.getTupleItemClassSetIndex(index)
- };
- this.tuples[index] = new VIPRTuple(this.dataItem.getTuple(index), this.context, this.content);
- }
- return this.tuples[index];
- },
- getDataWindowStart: function getDataWindowStart() {
- return 0;
- },
- getDataWindowEnd: function getDataWindowEnd() {
- return this.getTupleCount();
- },
- getItemClassSetCount: function getItemClassSetCount() {
- return this.itemClassSets.length;
- },
- getItemClassSet: function getItemClassSet(index) {
- return this.itemClassSets[index || 0];
- }
- });
- var VIPRCatContDataItem = VIPRCatDataItem.extend({
- init: function init() {
- VIPRCatContDataItem.inherited('init', this, arguments);
- this.formatter = new VIPRFormatter(this.context.index.dataitem, 0, this.context);
- this.postProcessing();
- },
- getType: function getType() {
- return 'cont';
- },
- getFormatter: function getFormatter() {
- return this.formatter;
- },
- postProcessing: function postProcessing() {
- var index = this.context.index.dataitem;
- var subType = this.dataset.slotAPIs[index].getSubType();
- if (subType && subType === 'latitude' || subType === 'longitude') {
- _.each(this.dataItem._dataItem.items, function (item) {
- if (item.t[0].hasOwnProperty('d') && item.t[0].hasOwnProperty('u') && item.t[0].d !== item.t[0].u) {
- item.t[0].u = item.t[0].d;
- }
- });
- }
- },
- getItemClass: function getItemClass() {
- return null;
- },
- getDomain: function getDomain() {
- return null;
- }
- });
-
- var VIPRContDataItem = VIPRDecoratable.extend({
- init: function init(dataItem, context) {
- VIPRContDataItem.inherited('init', this, [dataItem._dataItem, context]);
- this.dataItem = dataItem;
- this.context = context;
- this.queryResult = context && context.queryResult;
-
-
-
-
- this.formatter = new VIPRFormatter(this.context.index.dataitem, 0, this.context);
-
- this.itemClass = new VIPRItemClass(this.dataItem.getTupleHeaders()[0][0], this.context);
- this.domain = this._createDomain();
- },
-
- _createMultiMeasureDomain: function _createMultiMeasureDomain() {
- var domain = null;
- var topBottomMappings = this.queryResult && this.queryResult.getTopBottomMappings();
- var keys = _.keys(topBottomMappings);
- var overAllMin;
- var overAllMax;
-
- if (keys.length) {
- keys.forEach(function (key) {
- var minValue = topBottomMappings[key].bottomResult.useValue;
- var maxValue = topBottomMappings[key].topResult.useValue;
- overAllMin = overAllMin ? Math.min(overAllMin, minValue) : minValue;
- overAllMax = overAllMax ? Math.max(overAllMax, maxValue) : maxValue;
- });
- domain = {
- min: overAllMin,
- max: overAllMax
- };
- }
- return domain;
- },
-
- _createNonMultiMeasureDomain: function _createNonMultiMeasureDomain() {
- var domain = null;
- var topBottomMappings = this.queryResult && this.queryResult.getTopBottomMappings();
-
- var topBottomMapItemName = this.itemClass.itemClass.u + this.itemClass.itemClass.aggregate;
- if (topBottomMapItemName && topBottomMappings) {
- var topBottom = topBottomMappings[topBottomMapItemName];
- if (topBottom) {
- domain = {
- min: topBottom.bottomResult.useValue,
- max: topBottom.topResult.useValue
- };
- }
- }
- return domain;
- },
-
- _createDomain: function _createDomain() {
- var domain = null;
- if (this.itemClass && this.itemClass.itemClass) {
- if (this.itemClass.itemClass.u === '_multiMeasuresValue') {
- domain = this._createMultiMeasureDomain();
- } else {
- domain = this._createNonMultiMeasureDomain();
- }
- }
- return domain;
- },
- getUniqueName: function getUniqueName() {
- return this.itemClass.getUniqueName();
- },
- getCaption: function getCaption() {
- return this.itemClass.getCaption();
- },
- getType: function getType() {
- return 'cont';
- },
- getDomain: function getDomain() /*aggregation TODO for stacked charts*/{
- return this._createDomain();
- },
- getFormatter: function getFormatter() {
- return this.formatter;
- },
- getItemClass: function getItemClass() {
- return this.itemClass;
- }
- });
-
- function asIValue(valueObj, formatter, index) {
-
-
- if (_.isNumber(valueObj)) {
- valueObj = { v: valueObj };
- }
- if (typeof valueObj.getValue === 'undefined') {
- valueObj.getIndex = function () {
- return index;
- };
- valueObj.getValue = function () {
- return this.v;
- };
- valueObj.getValueType = function () {
- if (this.v === null) {
- return 'missing';
- }
- return 'numeric';
- };
- var caption = formatter.format(valueObj.v);
- valueObj.getCaption = function () {
- return caption;
- };
- }
- return valueObj;
- }
-
- function asIDataPoint(datapoint, dataset, context) {
- if (typeof datapoint.context === 'undefined') {
- datapoint.dataset = dataset;
- datapoint.getTupleIndex = function (index) {
- var tupleIndex = this.pt[index];
-
- this.dataset.getDataItem(index).getTuple(tupleIndex).addDataPoint(this);
- return tupleIndex;
- }.bind(datapoint);
-
- datapoint.getDataPointKey = function () {
- return this.pt ? _.filter(this.pt, function (pt) {
- return typeof pt === 'number';
- }).toString() : '';
- }.bind(datapoint);
- var getValue = typeof datapoint.getValue === 'function' ? datapoint.getValue : undefined;
- datapoint.getValue = function (index) {
- var valueObj = getValue ? getValue(index) : this.pt[index];
- var bIsUseCategoryAsValue = this.dataset.slotAPIs[index].getDefinition().useCategoryAsValue;
- if (bIsUseCategoryAsValue) {
- var tuple = this.dataset.resultData.getResultDataItem(index).getTuple(this.pt[index]);
- valueObj = {
- 'v': tuple[0].u
- };
- }
- var formatter = this.dataset.getDataItem(index).getFormatter();
- return asIValue(valueObj, formatter, index);
- }.bind(datapoint);
-
- if (!datapoint.hasDecoration || !datapoint.getDecoration || !datapoint.decorate) {
- datapoint._decorations = new VIPRDecoratable(datapoint, context);
- datapoint.hasDecoration = datapoint._decorations.hasDecoration.bind(datapoint._decorations);
- datapoint.getDecoration = datapoint._decorations.getDecoration.bind(datapoint._decorations);
- datapoint.clearDecorations = datapoint._decorations.clearDecorations.bind(datapoint._decorations);
- datapoint.decorate = function (action, value) {
-
- value = typeof value === 'undefined' ? true : value;
- this._decorations.decorate(action, value);
- }.bind(datapoint);
- }
- }
- return datapoint;
- }
-
- var VIPRDataPointIterator = Class.extend({
- init: function init(dataset, context) {
- this.dataset = dataset;
- this.resultData = dataset.resultData;
- this.slotAPIs = dataset.slotAPIs;
- this.datapoints = this.resultData.getDatapoints();
- this.context = context;
- this.index = -1;
- },
- nextValue: function nextValue() {
- if (this.index < this.datapoints.length - 1) {
- return asIDataPoint(this.datapoints[++this.index], this.dataset, this.context);
- }
- return null;
- },
- getTupleItems: function getTupleItems() {
- var items = [];
- for (var i = 0; i < this.datapoints[this.index].pt.length; i++) {
- var value = this.resultData.getCellValue(this.index, i);
- if ($.isArray(value)) {
- items = items.concat(value);
- }
- }
- return items;
- }
- });
- var VIPRDataSet = VIPRDecoratable.extend({
- init: function init(resultData, slotAPIs, queryResult, context, content, datasetId) {
- VIPRDataSet.inherited('init', this, []);
- this.resultData = resultData;
- this.slotAPIs = slotAPIs;
-
- this.dataItems = [];
- var aResultDataItems = this.resultData.getResultDataItems();
- this.context = context;
- this.context.queryResult = queryResult;
- this.context.dataset = this;
- this.content = content;
- this.id = datasetId;
- var numberOfDataItems = aResultDataItems ? aResultDataItems.length : 0;
- for (var index = 0; index < numberOfDataItems; index++) {
- var dataItem = aResultDataItems[index];
- if (this.slotAPIs[index]) {
-
- var slotType = this.slotAPIs[index].getDataItemList()[0].getType();
- var VIPRDataItem = slotType === 'attribute' ? VIPRCatDataItem : VIPRContDataItem;
-
- if (this.slotAPIs[index].getId().indexOf('latlongLocations.lat') !== -1 || this.slotAPIs[index].getId().indexOf('latlongLocations.long') !== -1) {
- VIPRDataItem = VIPRCatContDataItem;
- }
- this.context.index = {
- dataitem: index
- };
- this.dataItems[index] = new VIPRDataItem(dataItem, this.context, content);
- }
- }
- },
-
- getDataItem: function getDataItem(index) {
- return index < this.dataItems.length ? this.dataItems[index] : null;
- },
- getDataItemCount: function getDataItemCount() {
- return this.resultData.getResultDataItemCount();
- },
- getDataPointIterator: function getDataPointIterator() {
- return new VIPRDataPointIterator(this, this.context);
- }
- });
- var VIPRData = VIPRDecoratable.extend({
-
- init: function init(queryResults, visualization, hideAggTypeInAxisLabels, content) {
- VIPRData.inherited('init', this, []);
- this.content = content;
- this.context = {
- version: 0,
- VIPRData: this,
- hideAggTypeInAxisLabels: hideAggTypeInAxisLabels
- };
- this._aVIPRDataSets = [];
- var aDataSets = visualization.getDefinition().getDatasetList();
- _.each(aDataSets, function (dataset) {
- var aQueryResult = _.find(queryResults, function (entry) {
- return entry.getDataViewId() === dataset.id;
- });
- if (aQueryResult) {
- this._aVIPRDataSets.push(new VIPRDataSet(aQueryResult.getFacetData(), SlotAPIHelper.getMappedSlotListByDataset(visualization, aQueryResult.getDataViewId()), aQueryResult, this.context, this.content, dataset.id));
- } else {
- this._aVIPRDataSets.push(null);
- }
- }.bind(this));
- },
- getDataSetAt: function getDataSetAt(index) {
- return this._aVIPRDataSets[index];
- },
- getDataSetCount: function getDataSetCount() {
- return this._aVIPRDataSets.length;
- },
- getDecorationsVersion: function getDecorationsVersion() {
- return this.context.version;
- }
- });
- return VIPRData;
- });
|