GitLab at IIASA

crosstalk.js 47.9 KiB
Newer Older
Xinxin Yang's avatar
Xinxin Yang committed
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474

},{"./filter":2,"./input":6}],10:[function(require,module,exports){
"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.SelectionHandle = undefined;

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _events = require("./events");

var _events2 = _interopRequireDefault(_events);

var _group = require("./group");

var _group2 = _interopRequireDefault(_group);

var _util = require("./util");

var util = _interopRequireWildcard(_util);

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

/**
 * Use this class to read and write (and listen for changes to) the selection
 * for a Crosstalk group. This is intended to be used for linked brushing.
 *
 * If two (or more) `SelectionHandle` instances in the same webpage share the
 * same group name, they will share the same state. Setting the selection using
 * one `SelectionHandle` instance will result in the `value` property instantly
 * changing across the others, and `"change"` event listeners on all instances
 * (including the one that initiated the sending) will fire.
 *
 * @param {string} [group] - The name of the Crosstalk group, or if none,
 *   null or undefined (or any other falsy value). This can be changed later
 *   via the [SelectionHandle#setGroup](#setGroup) method.
 * @param {Object} [extraInfo] - An object whose properties will be copied to
 *   the event object whenever an event is emitted.
 */
var SelectionHandle = exports.SelectionHandle = function () {
  function SelectionHandle() {
    var group = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
    var extraInfo = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;

    _classCallCheck(this, SelectionHandle);

    this._eventRelay = new _events2.default();
    this._emitter = new util.SubscriptionTracker(this._eventRelay);

    // Name of the group we're currently tracking, if any. Can change over time.
    this._group = null;
    // The Var we're currently tracking, if any. Can change over time.
    this._var = null;
    // The event handler subscription we currently have on var.on("change").
    this._varOnChangeSub = null;

    this._extraInfo = util.extend({ sender: this }, extraInfo);

    this.setGroup(group);
  }

  /**
   * Changes the Crosstalk group membership of this SelectionHandle. The group
   * being switched away from (if any) will not have its selection value
   * modified as a result of calling `setGroup`, even if this handle was the
   * most recent handle to set the selection of the group.
   *
   * The group being switched to (if any) will also not have its selection value
   * modified as a result of calling `setGroup`. If you want to set the
   * selection value of the new group, call `set` explicitly.
   *
   * @param {string} group - The name of the Crosstalk group, or null (or
   *   undefined) to clear the group.
   */


  _createClass(SelectionHandle, [{
    key: "setGroup",
    value: function setGroup(group) {
      var _this = this;

      // If group is unchanged, do nothing
      if (this._group === group) return;
      // Treat null, undefined, and other falsy values the same
      if (!this._group && !group) return;

      if (this._var) {
        this._var.off("change", this._varOnChangeSub);
        this._var = null;
        this._varOnChangeSub = null;
      }

      this._group = group;

      if (group) {
        this._var = (0, _group2.default)(group).var("selection");
        var sub = this._var.on("change", function (e) {
          _this._eventRelay.trigger("change", e, _this);
        });
        this._varOnChangeSub = sub;
      }
    }

    /**
     * Retrieves the current selection for the group represented by this
     * `SelectionHandle`.
     *
     * - If no selection is active, then this value will be falsy.
     * - If a selection is active, but no data points are selected, then this
     *   value will be an empty array.
     * - If a selection is active, and data points are selected, then the keys
     *   of the selected data points will be present in the array.
     */

  }, {
    key: "_mergeExtraInfo",


    /**
     * Combines the given `extraInfo` (if any) with the handle's default
     * `_extraInfo` (if any).
     * @private
     */
    value: function _mergeExtraInfo(extraInfo) {
      // Important incidental effect: shallow clone is returned
      return util.extend({}, this._extraInfo ? this._extraInfo : null, extraInfo ? extraInfo : null);
    }

    /**
     * Overwrites the current selection for the group, and raises the `"change"`
     * event among all of the group's '`SelectionHandle` instances (including
     * this one).
     *
     * @fires SelectionHandle#change
     * @param {string[]} selectedKeys - Falsy, empty array, or array of keys (see
     *   {@link SelectionHandle#value}).
     * @param {Object} [extraInfo] - Extra properties to be included on the event
     *   object that's passed to listeners (in addition to any options that were
     *   passed into the `SelectionHandle` constructor).
     */

  }, {
    key: "set",
    value: function set(selectedKeys, extraInfo) {
      if (this._var) this._var.set(selectedKeys, this._mergeExtraInfo(extraInfo));
    }

    /**
     * Overwrites the current selection for the group, and raises the `"change"`
     * event among all of the group's '`SelectionHandle` instances (including
     * this one).
     *
     * @fires SelectionHandle#change
     * @param {Object} [extraInfo] - Extra properties to be included on the event
     *   object that's passed to listeners (in addition to any that were passed
     *   into the `SelectionHandle` constructor).
     */

  }, {
    key: "clear",
    value: function clear(extraInfo) {
      if (this._var) this.set(void 0, this._mergeExtraInfo(extraInfo));
    }

    /**
     * Subscribes to events on this `SelectionHandle`.
     *
     * @param {string} eventType - Indicates the type of events to listen to.
     *   Currently, only `"change"` is supported.
     * @param {SelectionHandle~listener} listener - The callback function that
     *   will be invoked when the event occurs.
     * @return {string} - A token to pass to {@link SelectionHandle#off} to cancel
     *   this subscription.
     */

  }, {
    key: "on",
    value: function on(eventType, listener) {
      return this._emitter.on(eventType, listener);
    }

    /**
     * Cancels event subscriptions created by {@link SelectionHandle#on}.
     *
     * @param {string} eventType - The type of event to unsubscribe.
     * @param {string|SelectionHandle~listener} listener - Either the callback
     *   function previously passed into {@link SelectionHandle#on}, or the
     *   string that was returned from {@link SelectionHandle#on}.
     */

  }, {
    key: "off",
    value: function off(eventType, listener) {
      return this._emitter.off(eventType, listener);
    }

    /**
     * Shuts down the `SelectionHandle` object.
     *
     * Removes all event listeners that were added through this handle.
     */

  }, {
    key: "close",
    value: function close() {
      this._emitter.removeAllListeners();
      this.setGroup(null);
    }
  }, {
    key: "value",
    get: function get() {
      return this._var ? this._var.get() : null;
    }
  }]);

  return SelectionHandle;
}();

/**
 * @callback SelectionHandle~listener
 * @param {Object} event - An object containing details of the event. For
 *   `"change"` events, this includes the properties `value` (the new
 *   value of the selection, or `undefined` if no selection is active),
 *   `oldValue` (the previous value of the selection), and `sender` (the
 *   `SelectionHandle` instance that made the change).
 */

/**
 * @event SelectionHandle#change
 * @type {object}
 * @property {object} value - The new value of the selection, or `undefined`
 *   if no selection is active.
 * @property {object} oldValue - The previous value of the selection.
 * @property {SelectionHandle} sender - The `SelectionHandle` instance that
 *   changed the value.
 */

},{"./events":1,"./group":4,"./util":11}],11:[function(require,module,exports){
"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };

exports.extend = extend;
exports.checkSorted = checkSorted;
exports.diffSortedLists = diffSortedLists;
exports.dataframeToD3 = dataframeToD3;

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function extend(target) {
  for (var _len = arguments.length, sources = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
    sources[_key - 1] = arguments[_key];
  }

  for (var i = 0; i < sources.length; i++) {
    var src = sources[i];
    if (typeof src === "undefined" || src === null) continue;

    for (var key in src) {
      if (src.hasOwnProperty(key)) {
        target[key] = src[key];
      }
    }
  }
  return target;
}

function checkSorted(list) {
  for (var i = 1; i < list.length; i++) {
    if (list[i] <= list[i - 1]) {
      throw new Error("List is not sorted or contains duplicate");
    }
  }
}

function diffSortedLists(a, b) {
  var i_a = 0;
  var i_b = 0;

  if (!a) a = [];
  if (!b) b = [];

  var a_only = [];
  var b_only = [];

  checkSorted(a);
  checkSorted(b);

  while (i_a < a.length && i_b < b.length) {
    if (a[i_a] === b[i_b]) {
      i_a++;
      i_b++;
    } else if (a[i_a] < b[i_b]) {
      a_only.push(a[i_a++]);
    } else {
      b_only.push(b[i_b++]);
    }
  }

  if (i_a < a.length) a_only = a_only.concat(a.slice(i_a));
  if (i_b < b.length) b_only = b_only.concat(b.slice(i_b));
  return {
    removed: a_only,
    added: b_only
  };
}

// Convert from wide: { colA: [1,2,3], colB: [4,5,6], ... }
// to long: [ {colA: 1, colB: 4}, {colA: 2, colB: 5}, ... ]
function dataframeToD3(df) {
  var names = [];
  var length = void 0;
  for (var name in df) {
    if (df.hasOwnProperty(name)) names.push(name);
    if (_typeof(df[name]) !== "object" || typeof df[name].length === "undefined") {
      throw new Error("All fields must be arrays");
    } else if (typeof length !== "undefined" && length !== df[name].length) {
      throw new Error("All fields must be arrays of the same length");
    }
    length = df[name].length;
  }
  var results = [];
  var item = void 0;
  for (var row = 0; row < length; row++) {
    item = {};
    for (var col = 0; col < names.length; col++) {
      item[names[col]] = df[names[col]][row];
    }
    results.push(item);
  }
  return results;
}

/**
 * Keeps track of all event listener additions/removals and lets all active
 * listeners be removed with a single operation.
 *
 * @private
 */

var SubscriptionTracker = exports.SubscriptionTracker = function () {
  function SubscriptionTracker(emitter) {
    _classCallCheck(this, SubscriptionTracker);

    this._emitter = emitter;
    this._subs = {};
  }

  _createClass(SubscriptionTracker, [{
    key: "on",
    value: function on(eventType, listener) {
      var sub = this._emitter.on(eventType, listener);
      this._subs[sub] = eventType;
      return sub;
    }
  }, {
    key: "off",
    value: function off(eventType, listener) {
      var sub = this._emitter.off(eventType, listener);
      if (sub) {
        delete this._subs[sub];
      }
      return sub;
    }
  }, {
    key: "removeAllListeners",
    value: function removeAllListeners() {
      var _this = this;

      var current_subs = this._subs;
      this._subs = {};
      Object.keys(current_subs).forEach(function (sub) {
        _this._emitter.off(current_subs[sub], sub);
      });
    }
  }]);

  return SubscriptionTracker;
}();

},{}],12:[function(require,module,exports){
(function (global){
"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _events = require("./events");

var _events2 = _interopRequireDefault(_events);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

var Var = function () {
  function Var(group, name, /*optional*/value) {
    _classCallCheck(this, Var);

    this._group = group;
    this._name = name;
    this._value = value;
    this._events = new _events2.default();
  }

  _createClass(Var, [{
    key: "get",
    value: function get() {
      return this._value;
    }
  }, {
    key: "set",
    value: function set(value, /*optional*/event) {
      if (this._value === value) {
        // Do nothing; the value hasn't changed
        return;
      }
      var oldValue = this._value;
      this._value = value;
      // Alert JavaScript listeners that the value has changed
      var evt = {};
      if (event && (typeof event === "undefined" ? "undefined" : _typeof(event)) === "object") {
        for (var k in event) {
          if (event.hasOwnProperty(k)) evt[k] = event[k];
        }
      }
      evt.oldValue = oldValue;
      evt.value = value;
      this._events.trigger("change", evt, this);

      // TODO: Make this extensible, to let arbitrary back-ends know that
      // something has changed
      if (global.Shiny && global.Shiny.onInputChange) {
        global.Shiny.onInputChange(".clientValue-" + (this._group.name !== null ? this._group.name + "-" : "") + this._name, typeof value === "undefined" ? null : value);
      }
    }
  }, {
    key: "on",
    value: function on(eventType, listener) {
      return this._events.on(eventType, listener);
    }
  }, {
    key: "off",
    value: function off(eventType, listener) {
      return this._events.off(eventType, listener);
    }
  }]);

  return Var;
}();

exports.default = Var;

}).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})

},{"./events":1}]},{},[5])
//# sourceMappingURL=crosstalk.js.map