UNPKG

@elastic/eui

Version:

Elastic UI Component Library

149 lines (141 loc) 7.52 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SortableProperties = void 0; var _comparators = require("./comparators"); function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } function _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread(); } function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } } function _iterableToArray(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); } function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); } function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; } function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); } function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } } function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; } function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; } function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } /* * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one * or more contributor license agreements. Licensed under the Elastic License * 2.0 and the Server Side Public License, v 1; you may not use this file except * in compliance with, at your election, the Elastic License 2.0 or the Server * Side Public License, v 1. */ /** * @typedef {Object} SortableProperty * @property {string} sortableProperty.name - Name of the property. * @property {function} sortableProperty.getValue - A function that takes in an object and returns a value to sort * by. * @property {boolean} sortableProperty.isAscending - The direction of the last sort by this property. Used to preserve * past sort orders. */ /** * Stores sort information for a set of SortableProperties, including which property is currently being sorted on, as * well as the last sort order for each property. */ var SortableProperties = exports.SortableProperties = /*#__PURE__*/function () { /** * @param {Array<SortableProperty>} sortableProperties - a set of sortable properties. * @param {string} initialSortablePropertyName - Which sort property should be sorted on by default. */ function SortableProperties(sortableProperties, initialSortablePropertyName) { _classCallCheck(this, SortableProperties); _defineProperty(this, "sortableProperties", void 0); _defineProperty(this, "currentSortedProperty", void 0); this.sortableProperties = sortableProperties; /** * The current property that is being sorted on. * @type {SortableProperty} */ var currentSortedProperty = this.getSortablePropertyByName(initialSortablePropertyName); if (!currentSortedProperty) { throw new Error("No property with the name ".concat(initialSortablePropertyName)); } this.currentSortedProperty = currentSortedProperty; } /** * @returns {SortableProperty} The current property that is being sorted on. Undefined if no sort order is applied. */ return _createClass(SortableProperties, [{ key: "getSortedProperty", value: function getSortedProperty() { return this.currentSortedProperty; } /** * Sorts the items passed in and returns a newly sorted array. * @param items {Array.<Object>} * @returns {Array.<Object>} sorted array of items, based off the sort properties. */ }, { key: "sortItems", value: function sortItems(items) { var copy = _toConsumableArray(items); var comparator = _comparators.Comparators.value(this.getSortedProperty().getValue); if (!this.isCurrentSortAscending()) { comparator = _comparators.Comparators.reverse(comparator); } copy.sort(comparator); return copy; } /** * Returns the SortProperty with the given name, if found. * @param {String} propertyName * @returns {SortableProperty|undefined} */ }, { key: "getSortablePropertyByName", value: function getSortablePropertyByName(propertyName) { return this.sortableProperties.find(function (property) { return property.name === propertyName; }); } /** * Updates the sort property, potentially flipping the sort order based on whether the same * property was already being sorted. * @param propertyName {String} */ }, { key: "sortOn", value: function sortOn(propertyName) { var newSortedProperty = this.getSortablePropertyByName(propertyName); if (!newSortedProperty) { throw new Error("No property with the name ".concat(propertyName)); } var sortedProperty = this.getSortedProperty(); if (sortedProperty.name === newSortedProperty.name) { this.flipCurrentSortOrder(); } else { this.currentSortedProperty = newSortedProperty; } } /** * @returns {boolean} True if the current sortable property is sorted in ascending order. */ }, { key: "isCurrentSortAscending", value: function isCurrentSortAscending() { var sortedProperty = this.getSortedProperty(); return sortedProperty ? this.isAscendingByName(sortedProperty.name) : false; } /** * @param {string} propertyName * @returns {boolean} True if the given sort property is sorted in ascending order. */ }, { key: "isAscendingByName", value: function isAscendingByName(propertyName) { var sortedProperty = this.getSortablePropertyByName(propertyName); return sortedProperty ? sortedProperty.isAscending : false; } /** * Flips the current sorted property sort order. */ }, { key: "flipCurrentSortOrder", value: function flipCurrentSortOrder() { this.currentSortedProperty.isAscending = !this.currentSortedProperty.isAscending; } }]); }();