1// Copyright 2015 the V8 project authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5import {isIterable} from "./util.js" 6 7export class MySelection { 8 selection: any; 9 stringKey: (o: any) => string; 10 11 constructor(stringKeyFnc) { 12 this.selection = new Map(); 13 this.stringKey = stringKeyFnc; 14 } 15 16 isEmpty(): boolean { 17 return this.selection.size == 0; 18 } 19 20 clear(): void { 21 this.selection = new Map(); 22 } 23 24 select(s, isSelected) { 25 if (!isIterable(s)) { s = [s]; } 26 for (const i of s) { 27 if (!i) continue; 28 if (isSelected == undefined) { 29 isSelected = !this.selection.has(this.stringKey(i)); 30 } 31 if (isSelected) { 32 this.selection.set(this.stringKey(i), i); 33 } else { 34 this.selection.delete(this.stringKey(i)); 35 } 36 } 37 } 38 39 isSelected(i): boolean { 40 return this.selection.has(this.stringKey(i)); 41 } 42 43 isKeySelected(key: string): boolean { 44 return this.selection.has(key); 45 } 46 47 selectedKeys() { 48 var result = new Set(); 49 for (var i of this.selection.keys()) { 50 result.add(i); 51 } 52 return result; 53 } 54 55 detachSelection() { 56 var result = new Set(); 57 for (var i of this.selection.keys()) { 58 result.add(i); 59 } 60 this.clear(); 61 return result; 62 } 63 64 [Symbol.iterator]() { return this.selection.values() } 65} 66