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 { GNode } from "./node"; 6 7export class MySelection { 8 selection: any; 9 stringKey: (o: any) => string; 10 originStringKey: (node: GNode) => string; 11 12 constructor(stringKeyFnc, originStringKeyFnc?) { 13 this.selection = new Map(); 14 this.stringKey = stringKeyFnc; 15 this.originStringKey = originStringKeyFnc; 16 } 17 18 isEmpty(): boolean { 19 return this.selection.size == 0; 20 } 21 22 clear(): void { 23 this.selection = new Map(); 24 } 25 26 select(s: Iterable<any>, isSelected?: boolean) { 27 for (const i of s) { 28 if (i == undefined) continue; 29 if (isSelected == undefined) { 30 isSelected = !this.selection.has(this.stringKey(i)); 31 } 32 if (isSelected) { 33 this.selection.set(this.stringKey(i), i); 34 } else { 35 this.selection.delete(this.stringKey(i)); 36 } 37 } 38 } 39 40 isSelected(i: any): boolean { 41 return this.selection.has(this.stringKey(i)); 42 } 43 44 isKeySelected(key: string): boolean { 45 return this.selection.has(key); 46 } 47 48 selectedKeys() { 49 const result = new Set(); 50 for (const i of this.selection.keys()) { 51 result.add(i); 52 } 53 return result; 54 } 55 56 detachSelection() { 57 const result = this.selection; 58 this.clear(); 59 return result; 60 } 61 62 [Symbol.iterator]() { return this.selection.values(); } 63} 64