switch-master
v0.3.2
Published
Switch Master, when you have many switch coupling logic, you need it! 开关管家,当你有许多开关逻辑耦合时,你需要它!
Downloads
5
Maintainers
Readme
switch-master
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
| prop | type | description |
| ---------- | ------ | -------------------------------------------------------------------- |
| id
| string | switch's unique id. default SwitchMaster will auto create random id. |
| name
| string | switch's name. default config.id
|
| status
| string | switch'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;
}