11cb0ef41Sopenharmony_ci// Copyright 2015 the V8 project authors. All rights reserved. 21cb0ef41Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be 31cb0ef41Sopenharmony_ci// found in the LICENSE file. 41cb0ef41Sopenharmony_ci 51cb0ef41Sopenharmony_ciimport { GNode } from "./node"; 61cb0ef41Sopenharmony_ci 71cb0ef41Sopenharmony_ciexport class MySelection { 81cb0ef41Sopenharmony_ci selection: any; 91cb0ef41Sopenharmony_ci stringKey: (o: any) => string; 101cb0ef41Sopenharmony_ci originStringKey: (node: GNode) => string; 111cb0ef41Sopenharmony_ci 121cb0ef41Sopenharmony_ci constructor(stringKeyFnc, originStringKeyFnc?) { 131cb0ef41Sopenharmony_ci this.selection = new Map(); 141cb0ef41Sopenharmony_ci this.stringKey = stringKeyFnc; 151cb0ef41Sopenharmony_ci this.originStringKey = originStringKeyFnc; 161cb0ef41Sopenharmony_ci } 171cb0ef41Sopenharmony_ci 181cb0ef41Sopenharmony_ci isEmpty(): boolean { 191cb0ef41Sopenharmony_ci return this.selection.size == 0; 201cb0ef41Sopenharmony_ci } 211cb0ef41Sopenharmony_ci 221cb0ef41Sopenharmony_ci clear(): void { 231cb0ef41Sopenharmony_ci this.selection = new Map(); 241cb0ef41Sopenharmony_ci } 251cb0ef41Sopenharmony_ci 261cb0ef41Sopenharmony_ci select(s: Iterable<any>, isSelected?: boolean) { 271cb0ef41Sopenharmony_ci for (const i of s) { 281cb0ef41Sopenharmony_ci if (i == undefined) continue; 291cb0ef41Sopenharmony_ci if (isSelected == undefined) { 301cb0ef41Sopenharmony_ci isSelected = !this.selection.has(this.stringKey(i)); 311cb0ef41Sopenharmony_ci } 321cb0ef41Sopenharmony_ci if (isSelected) { 331cb0ef41Sopenharmony_ci this.selection.set(this.stringKey(i), i); 341cb0ef41Sopenharmony_ci } else { 351cb0ef41Sopenharmony_ci this.selection.delete(this.stringKey(i)); 361cb0ef41Sopenharmony_ci } 371cb0ef41Sopenharmony_ci } 381cb0ef41Sopenharmony_ci } 391cb0ef41Sopenharmony_ci 401cb0ef41Sopenharmony_ci isSelected(i: any): boolean { 411cb0ef41Sopenharmony_ci return this.selection.has(this.stringKey(i)); 421cb0ef41Sopenharmony_ci } 431cb0ef41Sopenharmony_ci 441cb0ef41Sopenharmony_ci isKeySelected(key: string): boolean { 451cb0ef41Sopenharmony_ci return this.selection.has(key); 461cb0ef41Sopenharmony_ci } 471cb0ef41Sopenharmony_ci 481cb0ef41Sopenharmony_ci selectedKeys() { 491cb0ef41Sopenharmony_ci const result = new Set(); 501cb0ef41Sopenharmony_ci for (const i of this.selection.keys()) { 511cb0ef41Sopenharmony_ci result.add(i); 521cb0ef41Sopenharmony_ci } 531cb0ef41Sopenharmony_ci return result; 541cb0ef41Sopenharmony_ci } 551cb0ef41Sopenharmony_ci 561cb0ef41Sopenharmony_ci detachSelection() { 571cb0ef41Sopenharmony_ci const result = this.selection; 581cb0ef41Sopenharmony_ci this.clear(); 591cb0ef41Sopenharmony_ci return result; 601cb0ef41Sopenharmony_ci } 611cb0ef41Sopenharmony_ci 621cb0ef41Sopenharmony_ci [Symbol.iterator]() { return this.selection.values(); } 631cb0ef41Sopenharmony_ci} 64