0.3.2 • Published 6 days ago

switch-master v0.3.2

Weekly downloads
-
License
MIT
Repository
github
Last release
6 days ago

switch-master

switch-master blog

Switch Master, when you have many switch coupling logic, you need it!

开关管家,当你有许多开关逻辑耦合时,你需要它!

中文文档

Run Simple Demo

$ git clone https://github.com/SuperYesifang/switch-master.git
$ cd switch-master
$ npm run serve

Usage

new SwitchMaster(configs:SwitchOption[]):SwitchMaster

1. Use CDN

<script src="https://raw.githubusercontent.com/SuperYesifang/switch-master/master/dist/SwitchMaster.cdn.js"></script>
<script>
let master = new SwitchMaster([
  { name: "panel1", status: true },
  { name: "panel2", status: false },
  { name: "panel3", status: false },
  { name: "panel4", status: false }
]);
</script>

2. Use ESM

import SwitchMaster from "switch-master";

let master = SwitchMaster([
  { name: "panel1", status: true },
  { name: "panel2", status: false },
  { name: "panel3", status: false },
  { name: "panel4", status: false }
]);

switchOption

proptypedescription
idstringswitch's unique id. default SwitchMaster will auto create random id.
namestringswitch's name. default config.id
statusstringswitch's initial status. default false.

API

new Switch()

new Switch(config:SwitchOption):Switch

Instantiation a switch.

new SwitchMaster()

new SwitchMaster(configs:SwitchOption[]):SwitchMaster

Instantiation a switch master.

switch.open()

switch.open()

Set the status of switch to true.

switch.close()

switch.close()

Set the status of switch to false.

switch.toggle()

switch.toggle(status:undefined | boolean)

Toggle switch's status. support custom status.

switch.onChange()

switch.onChange(listener, lazy):removeListener

Add a status change listener to switch. lazy Whether to enable lazy listening, default: false.

@types

type listener = (status:boolean):any => {}
type removeListener = () => {}
import SwitchMaster, { Switch } from "switch-master";

let s = new Switch({
  id: "panelx",
  status: true
});

let master = new SwitchMaster([
  { name: "panel1", status: true },
  { name: "panel2", status: false },
  { name: "panel3", status: false },
  { name: "panel4", status: false }
]);

master.addSwitch(s);
console.log(master.switchs);

master.switchs

master.switchs:{[id:string]:Switch}

all switchs collection of the master.

master.initialStatus

master.initialStaus:{[id:string]:boolean}

all switch's initial status collection of master.

master.addSwtich()

master.addSwtich(s:Switch|Swtich[])

add swtich(s) to master.

master.removeSwitch()

master.removeSwitch(s:Switch|Switch[])

remove switch(s) from master.

master.removeSwitchById()

master.removeSwitchById(id:string|string[])

remove switch(s) from master by id(s).

master.removeSwitchByName()

master.removeSwitchByName(name:string|string[])

remove switch(s) from master by name(s).

master.getSwitchById()

master.getSwitchById(id:string|string[]):Switch|Switch[]

get switch(s) from master by id(s).

master.getSwitchByName()

master.getSwitchByName(name:string|string[]):Switch|Array(Switch|Switch[])

get switch(s) from master by name(s).

getNameById()

master.getNameById(id:string|string[]):string|string[]

get switch's name(s) from master by id(s).

getIdByName()

master.getIdByName(name:string|string[]):string|Array<string|string[]>

get switch's id(s) from master by name(s).

openById()

master.openById(id:string|string[])

open switch by id(s).

openByName()

master.openByName(name:string|string[])

open switch by name(s).

closeById()

master.closeById(id:string|string[])

close switch by id(s).

closeByName()

master.closeByName(name:string|string[])

close switch by name(s).

toggleById()

master.toggleById(config)

toggle switch by id config.

master.toggleById('Switch__e76cfa8771c4'); // id
master.toggleById({
  'Switch__e76cfa8771c4': true,
  'Switch__10476415b997': false
}); // id:status config
master.toggleById([
  'Switch__e76cfa8771c4',
  'Switch__10476415b997'
]); // ids
master.toggleById([
  'Switch__e76cfa8771c4',
  { 'Switch__10476415b997': true }
]); // ids and id:status configs

reset()

master.reset(id?)

reset switch status to initial status by id(s)?.

@types

type config = {
  [id:string]: boolean;
}

toggleByName()

master.toggleByName(config)

toggle switch by name config.

master.toggleByName('panel1'); // name
master.toggleByName({
  'panel1': true,
  'panel3': false
}); // name:status config
master.toggleByName([
  'panel1',
  'panel3'
]); // names
master.toggleByName([
  'panel1',
  { 'panel3': true }
]); // names and name:status configs

@types

type config = {
  [name:string]: boolean;
}
0.3.2

6 days ago

0.3.0

10 months ago

0.3.1

9 months ago

0.2.4

2 years ago

0.2.3

2 years ago

0.2.2

2 years ago

0.2.1

2 years ago

0.2.0

2 years ago

0.1.0

2 years ago