Refactoring, started Vuex bindings, modified Vue bindings (hvue), global state for webclient

This commit is contained in:
MaMe82 2018-07-27 03:07:23 +02:00
parent 03df7ebcc1
commit 1373345ef0
23 changed files with 2682 additions and 618 deletions

View File

@ -71,7 +71,7 @@ func main() {
go func() {
for {
service.EvMgr.Emit(service.ConstructEventLog("test source", i%5, "message " +strconv.Itoa(i) + ": " + textfill))
time.Sleep(time.Millisecond *500)
time.Sleep(time.Millisecond *100)
i++
}
}()

View File

@ -37,17 +37,7 @@ func (s *server) EventListen(eReq *pb.EventRequest, eStream pb.P4WNP1_EventListe
select {
case ev := <- rcv.EventQueue:
fmt.Printf("Event dequed to send: %+v\n", ev)
/*
if ev.Type == eReq.ListenType || eReq.ListenType == common.EVT_ANY {
//send Event to stream
err = eStream.Send(ev)
if err != nil {
rcv.Cancel()
log.Println(err)
return err
}
}
*/
//send Event to stream
err = eStream.Send(ev)
if err != nil {

View File

@ -4,21 +4,14 @@ import (
"../common"
pb "../proto/gopherjs"
"errors"
"github.com/gopherjs/gopherjs/js"
)
type EventLog struct {
*js.Object
EvLogSource string `js:"source"`
EvLogLevel int `js:"level"`
EvLogMessage string `js:"message"`
EvLogTime string `js:"time"`
}
func DeconstructEventLog(gRPCEv *pb.Event) (res *EventLog, err error) {
func DeconstructEventLog(gRPCEv *pb.Event) (res *jsEventLog, err error) {
if gRPCEv.Type != common.EVT_LOG { return nil,errors.New("No log event")}
res = &EventLog{Object:O()}
res = &jsEventLog{Object:O()}
switch vT := gRPCEv.Values[0].Val.(type) {
case *pb.EventValue_Tstring:
res.EvLogSource = vT.Tstring

113
web_client/globalState.go Normal file
View File

@ -0,0 +1,113 @@
package main
import (
"./mvuex"
"github.com/gopherjs/gopherjs/js"
"time"
"context"
pb "../proto/gopherjs"
)
const (
initHIDScript = `layout('us'); // US keyboard layout
typingSpeed(100,150) // Wait 100ms between key strokes + an additional random value between 0ms and 150ms (natural)
waitLEDRepeat(NUM); // Wait till NUM LED of target changes frequently multiple times (doesn't work on OSX)
press("GUI r");
delay(500);
type("notepad\n")
delay(1000);
for (var i = 0; i < 3; i++) {
type("Hello from P4wnP1 run " + i + " !\n");
type("Moving mouse right ...");
moveStepped(500,0);
type("and left\n");
moveStepped(-500,0);
}
type("Let's type fast !!!!!!!!!!!!!!!\n")
typingSpeed(0,0);
for (var i = 3; i < 10; i++) {
type("Hello from P4wnP1 run " + i + " !\n");
type("Moving mouse right ...");
moveStepped(500,0);
type("and left\n");
moveStepped(-500,0);
}`
)
type GlobalState struct {
*js.Object
Title string `js:"title"`
CurrentHIDScriptSource string `js:"currentHIDScriptSource"`
CurrentGadgetSettings *jsGadgetSettings `js:"currentGadgetSettings"`
Counter int `js:"count"`
Text string `js:"text"`
}
func UpdateGadgetSettingsFromDeployed(jsGS *jsGadgetSettings) {
//gs := vue.GetVM(c).Get("gadgetSettings")
println("UpdateGadgetSettingsFromDeployed called")
ctx,cancel := context.WithTimeout(context.Background(), time.Second*3)
defer cancel()
deployedGs, err := Client.Client.GetDeployedGadgetSetting(ctx, &pb.Empty{})
if err != nil { println(err); return } // ToDo: change to alert with parsed status
jsGS.fromGS(deployedGs)
return
}
func createGlobalStateStruct() interface{} {
state := GlobalState{Object:O()}
state.Title = "P4wnP1 by MaMe82"
state.CurrentHIDScriptSource = initHIDScript
state.CurrentGadgetSettings = NewUSBGadgetSettings()
UpdateGadgetSettingsFromDeployed(state.CurrentGadgetSettings)
state.Counter = 1337
state.Text = "Hi there says MaMe82"
return state
}
func initMVuex() {
state := createGlobalStateStruct()
store := mvuex.NewStore(
mvuex.State(state),
mvuex.Mutation("increment", func (store *mvuex.Store, state *GlobalState, add int) {
state.Counter += add
return
}),
mvuex.Mutation("decrement", func (store *mvuex.Store, state *GlobalState) {
state.Counter--
return
}),
mvuex.Mutation("setText", func (store *mvuex.Store, state *GlobalState, newText string) {
state.Text = newText
return
}),
mvuex.Mutation("setCurrentHIDScriptSource", func (store *mvuex.Store, state *GlobalState, newText string) {
state.CurrentHIDScriptSource = newText
return
}),
mvuex.Mutation("setCurrentGadgetSettings", func (store *mvuex.Store, state *GlobalState, newSettings *jsGadgetSettings) {
state.CurrentGadgetSettings = newSettings
return
}),
mvuex.Mutation("setCurrentGadgetSettingsFromDeployed", func (store *mvuex.Store, state *GlobalState) {
//ToDo: check if this is valid for synchronous run, has to be dispatched to action otherwise
println("Store: commit setCurrentGadgetSettingsFromDeployed")
go UpdateGadgetSettingsFromDeployed(state.CurrentGadgetSettings)
return
}),
)
// propagate Vuex store to global scope to allow injecting it to Vue by setting the "store" option
js.Global.Set("store", store)
}
func InitGlobalState() {
initMVuex()
}

View File

@ -2,7 +2,7 @@ package main
import (
"github.com/gopherjs/gopherjs/js"
"github.com/HuckRidgeSW/hvue"
"github.com/mame82/hvue"
)
//https://github.com/cnu4/vue-codemirror-lite/blob/master/codemirror.vue
@ -81,7 +81,6 @@ func initCodeMirror(vm *hvue.VM) {
//propagate up change
vm.Emit("change", newVal)
vm.Emit("input", newVal)
})
}

View File

@ -2,7 +2,7 @@ package main
import (
"github.com/gopherjs/gopherjs/js"
"github.com/HuckRidgeSW/hvue"
"github.com/mame82/hvue"
)
type CompEthernetAddressesData2 struct {

View File

@ -2,25 +2,32 @@ package main
import (
"github.com/gopherjs/gopherjs/js"
"github.com/HuckRidgeSW/hvue"
"github.com/mame82/hvue"
"strconv"
"google.golang.org/grpc/status"
)
type CompHIDScriptData struct {
*js.Object
ScriptContent string `js:"scriptContent"`
//ScriptContent string `js:"scriptContent"`
}
func (data *CompHIDScriptData) SendAndRun(vm *hvue.VM) {
md5 := StringToMD5(data.ScriptContent) //Calculate MD5 hexstring of current script content
sourceCode := vm.Get("scriptContent").String()
md5 := StringToMD5(sourceCode) //Calculate MD5 hexstring of current script content
//js.Global.Call("alert", md5)
go func() {
timeout := uint32(0)
err := UploadHIDScript(md5, data.ScriptContent)
err := UploadHIDScript(md5, sourceCode)
if err != nil { Alert("Error uploading script: " + err.Error()); return }
job,err := RunHIDScript(md5, timeout)
if err != nil { Alert("Error starting script as background job: " + err.Error()); return }
if err != nil {
println(status.Convert(err))
Alert("Error starting script as background job: " + err.Error())
return
}
Alert("Script started as background job: " + strconv.Itoa(int(job.Id)))
}()
}
@ -29,7 +36,7 @@ func newCompHIDScriptData(vm *hvue.VM) interface{} {
newVM := &CompHIDScriptData{
Object: js.Global.Get("Object").New(),
}
newVM.ScriptContent = "layout('us');\ntype('hello');"
//newVM.ScriptContent = "layout('us');\ntype('hello');"
return newVM
}
@ -39,6 +46,14 @@ func InitCompHIDScript() {
hvue.Template(compHIDScriptTemplate),
hvue.DataFunc(newCompHIDScriptData),
hvue.MethodsOf(&CompHIDScriptData{}),
hvue.ComputedWithGetSet(
"scriptContent",
func(vm *hvue.VM) interface{} {
return vm.Store.Get("state").Get("currentHIDScriptSource")
},
func(vm *hvue.VM, newValue *js.Object) {
vm.Store.Call("commit", "setCurrentHIDScriptSource", newValue)
}),
)
}

View File

@ -2,7 +2,7 @@ package main
import (
"github.com/gopherjs/gopherjs/js"
"github.com/HuckRidgeSW/hvue"
"github.com/mame82/hvue"
pb "../proto/gopherjs"
"../common"
"io"

101
web_client/hvueCompState.go Normal file
View File

@ -0,0 +1,101 @@
package main
import (
"github.com/gopherjs/gopherjs/js"
"github.com/mame82/hvue"
)
type CompStateData struct {
*js.Object
}
func newCompStateData(vm *hvue.VM) interface{} {
newVM := &CompToggleSwitchData{
Object: O(),
}
return newVM
}
/*
type StoreState struct {
*js.Object
Counter int `js:"count"`
Text string `js:"text"`
}
func createState() interface{} {
state := StoreState{Object:O()}
state.Counter = 1337
state.Text = "Hi there"
return state
}
*/
func InitCompState() {
/*
state := createState()
store := mvuex.NewStore(
mvuex.State(state),
mvuex.Mutation("increment", func (store *mvuex.Store, state *StoreState, add int) {
state.Counter += add
return
}),
mvuex.Mutation("decrement", func (store *mvuex.Store, state *StoreState) {
state.Counter--
return
}),
mvuex.Mutation("setText", func (store *mvuex.Store, state *StoreState, newText string) {
state.Text = newText
return
}),
)
js.Global.Set("store", store)
*/
hvue.NewComponent(
"state",
hvue.Template(compStateTemplate),
hvue.DataFunc(newCompStateData),
hvue.Computed("count", func(vm *hvue.VM) interface{} {
return js.Global.Get("store").Get("state").Get("count")
}),
hvue.ComputedWithGetSet("text",
func(vm *hvue.VM) interface{} {
return js.Global.Get("store").Get("state").Get("text")
},
func(vm *hvue.VM, newValue *js.Object) {
js.Global.Get("store").Call("commit", "setText", newValue)
}),
hvue.Method("increment", func(vm *hvue.VM, count *js.Object) {
// normal way to access the store.commit() function
js.Global.Get("store").Call("commit", "increment", count)
//Quick way to access the commit function, possible as we have the Go instance of "store" in scope
//store.Commit("increment", count)
}),
hvue.Method("decrement", func(vm *hvue.VM) {
// normal way to access the store.commit() function
js.Global.Get("store").Call("commit", "decrement")
//Quick way to access the commit function, possible as we have the Go instance of "store" in scope
//store.Commit("decrement")
}),
)
}
const (
compStateTemplate = `
<div>
<p>{{ count }}</p>
<p>{{ text }}</p>
<input v-model="text"></input>
<p>
<button @click="increment(1,2,3)">+</button>
<button @click="increment(2)">+2</button>
<button @click="decrement">-</button>
</p>
</div>
`
)

View File

@ -2,7 +2,7 @@ package main
import (
"github.com/gopherjs/gopherjs/js"
"github.com/HuckRidgeSW/hvue"
"github.com/mame82/hvue"
)
type CompTabData struct {
@ -43,11 +43,11 @@ func InitCompTab() {
hvue.Computed("_isTab", func(vm *hvue.VM) interface{} {
return true
}),
/*
hvue.Computed("index", func(vm *hvue.VM) interface{} {
return 0
hvue.Computed("hasOverride", func(vm *hvue.VM) interface{} {
return vm.Slots.Get("override").Bool()
}),
*/
hvue.Mounted(func(vm *hvue.VM) {
vm.Set("isActive", vm.Get("selected")) //propagate "selected" property over to "isActive" from data
}),
@ -58,9 +58,10 @@ func InitCompTab() {
const (
compTabTemplate = `
<div v-if="isActive">
<slot></slot>
</div>
<div v-if="isActive">
<slot></slot>
</div>
`
)

View File

@ -2,7 +2,7 @@ package main
import (
"github.com/gopherjs/gopherjs/js"
"github.com/HuckRidgeSW/hvue"
"github.com/mame82/hvue"
)
type CompTabsData struct {

View File

@ -2,7 +2,7 @@ package main
import (
"github.com/gopherjs/gopherjs/js"
"github.com/HuckRidgeSW/hvue"
"github.com/mame82/hvue"
)
type CompToggleSwitchData struct {

View File

@ -6,149 +6,31 @@ import (
"time"
"context"
"google.golang.org/grpc/status"
"github.com/HuckRidgeSW/hvue"
"github.com/mame82/hvue"
)
type VGadgetSettings struct {
*js.Object
Enabled bool `js:"Enabled"`
Vid string `js:"Vid"`
Pid string `js:"Pid"`
Manufacturer string `js:"Manufacturer"`
Product string `js:"Product"`
Serial string `js:"Serial"`
Use_CDC_ECM bool `js:"Use_CDC_ECM"`
Use_RNDIS bool `js:"Use_RNDIS"`
Use_HID_KEYBOARD bool `js:"Use_HID_KEYBOARD"`
Use_HID_MOUSE bool `js:"Use_HID_MOUSE"`
Use_HID_RAW bool `js:"Use_HID_RAW"`
Use_UMS bool `js:"Use_UMS"`
Use_SERIAL bool `js:"Use_SERIAL"`
RndisSettings *VGadgetSettingsEthernet `js:"RndisSettings"`
CdcEcmSettings *VGadgetSettingsEthernet `js:"CdcEcmSettings"`
UmsSettings *VGadgetSettingsUMS `js:"UmsSettings"`
}
type VGadgetSettingsEthernet struct {
*js.Object
HostAddr string `js:"HostAddr"`
DevAddr string `js:"DevAddr"`
}
type VGadgetSettingsUMS struct {
*js.Object
Cdrom bool `js:"Cdrom"`
File string `js:"File"`
}
func (vGS VGadgetSettings) toGS() (gs *pb.GadgetSettings) {
return &pb.GadgetSettings{
Serial: vGS.Serial,
Use_SERIAL: vGS.Use_SERIAL,
Use_UMS: vGS.Use_UMS,
Use_HID_RAW: vGS.Use_HID_RAW,
Use_HID_MOUSE: vGS.Use_HID_MOUSE,
Use_HID_KEYBOARD: vGS.Use_HID_KEYBOARD,
Use_RNDIS: vGS.Use_RNDIS,
Use_CDC_ECM: vGS.Use_CDC_ECM,
Product: vGS.Product,
Manufacturer: vGS.Manufacturer,
Vid: vGS.Vid,
Pid: vGS.Pid,
Enabled: vGS.Enabled,
UmsSettings: &pb.GadgetSettingsUMS{
Cdrom: vGS.UmsSettings.Cdrom,
File: vGS.UmsSettings.File,
},
CdcEcmSettings: &pb.GadgetSettingsEthernet{
DevAddr: vGS.CdcEcmSettings.DevAddr,
HostAddr: vGS.CdcEcmSettings.HostAddr,
},
RndisSettings: &pb.GadgetSettingsEthernet{
DevAddr: vGS.RndisSettings.DevAddr,
HostAddr: vGS.RndisSettings.HostAddr,
},
}
}
func (vGS *VGadgetSettings) fromGS(gs *pb.GadgetSettings) {
println(gs)
vGS.Enabled = gs.Enabled
vGS.Vid = gs.Vid
vGS.Pid = gs.Pid
vGS.Manufacturer = gs.Manufacturer
vGS.Product = gs.Product
vGS.Serial = gs.Serial
vGS.Use_CDC_ECM = gs.Use_CDC_ECM
vGS.Use_RNDIS = gs.Use_RNDIS
vGS.Use_HID_KEYBOARD = gs.Use_HID_KEYBOARD
vGS.Use_HID_MOUSE = gs.Use_HID_MOUSE
vGS.Use_HID_RAW = gs.Use_HID_RAW
vGS.Use_UMS = gs.Use_UMS
vGS.Use_SERIAL = gs.Use_SERIAL
vGS.RndisSettings = &VGadgetSettingsEthernet{
Object: js.Global.Get("Object").New(),
}
if gs.RndisSettings != nil {
vGS.RndisSettings.HostAddr = gs.RndisSettings.HostAddr
vGS.RndisSettings.DevAddr = gs.RndisSettings.DevAddr
}
vGS.CdcEcmSettings = &VGadgetSettingsEthernet{
Object: js.Global.Get("Object").New(),
}
if gs.CdcEcmSettings != nil {
vGS.CdcEcmSettings.HostAddr = gs.CdcEcmSettings.HostAddr
vGS.CdcEcmSettings.DevAddr = gs.CdcEcmSettings.DevAddr
}
vGS.UmsSettings = &VGadgetSettingsUMS{
Object: js.Global.Get("Object").New(),
}
if gs.UmsSettings != nil {
vGS.UmsSettings.File = gs.UmsSettings.File
vGS.UmsSettings.Cdrom = gs.UmsSettings.Cdrom
}
}
// Note: internalize wouldn't work on this, as the nested structs don't translate back
type CompUSBSettingsData struct {
*js.Object
GadgetSettings *VGadgetSettings `js:"gadgetSettings"`
DeployPending bool `js:"deployPending"`
CdcEcmDetails bool `js:"cdcEcmDetails"`
RndisDetails bool `js:"rndisDetails"`
GadgetSettings *jsGadgetSettings `js:"gadgetSettings"`
DeployPending bool `js:"deployPending"`
CdcEcmDetails bool `js:"cdcEcmDetails"`
RndisDetails bool `js:"rndisDetails"`
}
func (c *CompUSBSettingsData) UpdateToDeployedGadgetSettings(vm *hvue.VM) {
//gs := vue.GetVM(c).Get("gadgetSettings")
println("Trying to fetch deployed GadgetSettings")
go func() {
ctx,cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
deployedGs, err := Client.Client.GetDeployedGadgetSetting(ctx, &pb.Empty{})
if err != nil { println(err); return }
newGs := &VGadgetSettings{
Object: js.Global.Get("Object").New(),
}
newGs.fromGS(deployedGs)
c.GadgetSettings = newGs
}()
//ToDo: Reimplement with Action on global state
func (c *CompUSBSettingsData) UpdateFromDeployedGadgetSettings(vm *hvue.VM) {
vm.Store.Call("commit", "setCurrentGadgetSettingsFromDeployed")
}
//ToDo: Reimplement with actions on global state
func (c *CompUSBSettingsData) ApplyGadgetSettings(vm *hvue.VM) {
//println("Trying to deploy GadgetSettings: " + fmt.Sprintf("%+v",c.GadgetSettings.toGS()))
println("Trying to deploy GadgetSettings...")
gs:=c.GadgetSettings.toGS()
//gs:=c.GadgetSettings.toGS()
gs := jsGadgetSettings{Object: vm.Store.Get("state").Get("currentGadgetSettings")}.toGS()
go func() {
c.DeployPending = true
defer func() {c.DeployPending = false}()
@ -161,7 +43,7 @@ func (c *CompUSBSettingsData) ApplyGadgetSettings(vm *hvue.VM) {
if err != nil {
js.Global.Call("alert", "Error setting given gadget settings: " + status.Convert(err).Message())
println(err)
c.UpdateToDeployedGadgetSettings(vm)
c.UpdateFromDeployedGadgetSettings(vm)
return
}
println("New GadgetSettings have been set")
@ -173,14 +55,14 @@ func (c *CompUSBSettingsData) ApplyGadgetSettings(vm *hvue.VM) {
if err != nil {
js.Global.Call("alert", "Error deploying gadget settings: " + status.Convert(err).Message())
println(err)
c.UpdateToDeployedGadgetSettings(vm)
c.UpdateFromDeployedGadgetSettings(vm)
return
}
println("New GadgetSettings have been deployed")
js.Global.Call("alert", "New USB gadget settings deployed ")
newGs := &VGadgetSettings{
newGs := &jsGadgetSettings{
Object: js.Global.Get("Object").New(),
}
newGs.fromGS(deployedGs)
@ -195,6 +77,11 @@ func InitCompUSBSettings() {
hvue.Template(compUSBSettingsTemplate),
hvue.DataFunc(newCompUSBSettingsData),
hvue.MethodsOf(&CompUSBSettingsData{}),
hvue.Computed(
"currentGadgetSettings",
func(vm *hvue.VM) interface{} {
return vm.Store.Get("state").Get("currentGadgetSettings")
}),
)
}
@ -204,13 +91,9 @@ func newCompUSBSettingsData(vm *hvue.VM) interface{} {
Object: js.Global.Get("Object").New(),
}
cc.GadgetSettings = &VGadgetSettings{
Object: js.Global.Get("Object").New(),
}
cc.GadgetSettings.fromGS(&pb.GadgetSettings{}) //start with empty settings, but create nested structs
cc.GadgetSettings = NewUSBGadgetSettings()
cc.UpdateToDeployedGadgetSettings(vm)
cc.UpdateFromDeployedGadgetSettings(vm)
cc.DeployPending = false
cc.RndisDetails = false
cc.CdcEcmDetails = false
@ -226,68 +109,69 @@ const (
<table>
<tr>
<td>USB gadget settings</td>
<td><button @click="ApplyGadgetSettings" :disabled="deployPending">Apply</button></td>
<td><button @click="ApplyGadgetSettings" :disabled="deployPending">Apply</button>
<button @click="UpdateFromDeployedGadgetSettings">Deployed</button></td>
</tr>
<tr>
<td>Gadget enabled</td>
<td><toggle-switch v-model="gadgetSettings.Enabled"></toggle-switch></td>
<td><toggle-switch v-model="currentGadgetSettings.Enabled"></toggle-switch></td>
</tr>
<tr>
<td>Vendor ID</td>
<td><input v-model="gadgetSettings.Vid"/></td>
<td><input v-model="currentGadgetSettings.Vid"/></td>
</tr>
<tr>
<td>Product ID</td>
<td><input v-model="gadgetSettings.Pid"/></td>
<td><input v-model="currentGadgetSettings.Pid"/></td>
</tr>
<tr>
<td>Manufacturer Name</td>
<td><input v-model="gadgetSettings.Manufacturer"/></td>
<td><input v-model="currentGadgetSettings.Manufacturer"/></td>
</tr>
<tr>
<td>Product Name</td>
<td><input v-model="gadgetSettings.Product"/></td>
<td><input v-model="currentGadgetSettings.Product"/></td>
</tr>
<tr>
<td>Serial number</td>
<td><input v-model="gadgetSettings.Serial"/></td>
<td><input v-model="currentGadgetSettings.Serial"/></td>
</tr>
<tr>
<td>CDC ECM</td>
<td><toggle-switch v-model="gadgetSettings.Use_CDC_ECM"></toggle-switch></td>
<td><toggle-switch v-model="currentGadgetSettings.Use_CDC_ECM"></toggle-switch></td>
</tr>
<tr v-if="gadgetSettings.Use_CDC_ECM">
<tr v-if="currentGadgetSettings.Use_CDC_ECM">
<td></td>
<td><ethernet-addresses v-bind:settings="gadgetSettings.CdcEcmSettings" @hostAddrChange="gadgetSettings.CdcEcmSettings.HostAddr=$event" @devAddrChange="gadgetSettings.CdcEcmSettings.DevAddr=$event"></ethernet-addresses></td>
<td><ethernet-addresses v-bind:settings="currentGadgetSettings.CdcEcmSettings" @hostAddrChange="currentGadgetSettings.CdcEcmSettings.HostAddr=$event" @devAddrChange="currentGadgetSettings.CdcEcmSettings.DevAddr=$event"></ethernet-addresses></td>
</tr>
<tr>
<td>RNDIS</td>
<td><toggle-switch v-model="gadgetSettings.Use_RNDIS"></toggle-switch></td>
<td><input type="checkbox" v-if="gadgetSettings.Use_RNDIS" v-model="rndisDetails"></td>
<td><toggle-switch v-model="currentGadgetSettings.Use_RNDIS"></toggle-switch></td>
<td><input type="checkbox" v-if="currentGadgetSettings.Use_RNDIS" v-model="rndisDetails"></td>
</tr>
<tr v-if="rndisDetails">
<td></td>
<td><ethernet-addresses v-bind:settings="gadgetSettings.RndisSettings" @hostAddrChange="gadgetSettings.RndisSettings.HostAddr=$event" @devAddrChange="gadgetSettings.RndisSettings.DevAddr=$event"></ethernet-addresses></td>
<td><ethernet-addresses v-bind:settings="currentGadgetSettings.RndisSettings" @hostAddrChange="currentGadgetSettings.RndisSettings.HostAddr=$event" @devAddrChange="currentGadgetSettings.RndisSettings.DevAddr=$event"></ethernet-addresses></td>
</tr>
<tr>
<td>HID Keyboard</td>
<td><toggle-switch v-model="gadgetSettings.Use_HID_KEYBOARD"></toggle-switch></td>
<td><toggle-switch v-model="currentGadgetSettings.Use_HID_KEYBOARD"></toggle-switch></td>
</tr>
<tr>
<td>HID Mouse</td>
<td><toggle-switch v-model="gadgetSettings.Use_HID_MOUSE"></toggle-switch></td>
<td><toggle-switch v-model="currentGadgetSettings.Use_HID_MOUSE"></toggle-switch></td>
</tr>
<tr>
<td>HID Raw</td>
<td><toggle-switch v-model="gadgetSettings.Use_HID_RAW"></toggle-switch></td>
<td><toggle-switch v-model="currentGadgetSettings.Use_HID_RAW"></toggle-switch></td>
</tr>
<tr>
<td>Serial</td>
<td><toggle-switch v-model="gadgetSettings.Use_SERIAL"></toggle-switch></td>
<td><toggle-switch v-model="currentGadgetSettings.Use_SERIAL"></toggle-switch></td>
</tr>
<tr>
<td>Mass Storage</td>
<td><toggle-switch v-model="gadgetSettings.Use_UMS"></toggle-switch></td>
<td><toggle-switch v-model="currentGadgetSettings.Use_UMS"></toggle-switch></td>
</tr>
</table>
</div>

134
web_client/jsTypeDefs.go Normal file
View File

@ -0,0 +1,134 @@
package main
import (
pb "../proto/gopherjs"
"github.com/gopherjs/gopherjs/js"
)
/* USB Gadget types corresponding to gRPC messages */
type jsGadgetSettings struct {
*js.Object
Enabled bool `js:"Enabled"`
Vid string `js:"Vid"`
Pid string `js:"Pid"`
Manufacturer string `js:"Manufacturer"`
Product string `js:"Product"`
Serial string `js:"Serial"`
Use_CDC_ECM bool `js:"Use_CDC_ECM"`
Use_RNDIS bool `js:"Use_RNDIS"`
Use_HID_KEYBOARD bool `js:"Use_HID_KEYBOARD"`
Use_HID_MOUSE bool `js:"Use_HID_MOUSE"`
Use_HID_RAW bool `js:"Use_HID_RAW"`
Use_UMS bool `js:"Use_UMS"`
Use_SERIAL bool `js:"Use_SERIAL"`
RndisSettings *VGadgetSettingsEthernet `js:"RndisSettings"`
CdcEcmSettings *VGadgetSettingsEthernet `js:"CdcEcmSettings"`
UmsSettings *VGadgetSettingsUMS `js:"UmsSettings"`
}
type VGadgetSettingsEthernet struct {
*js.Object
HostAddr string `js:"HostAddr"`
DevAddr string `js:"DevAddr"`
}
type VGadgetSettingsUMS struct {
*js.Object
Cdrom bool `js:"Cdrom"`
File string `js:"File"`
}
func (jsGS jsGadgetSettings) toGS() (gs *pb.GadgetSettings) {
return &pb.GadgetSettings{
Serial: jsGS.Serial,
Use_SERIAL: jsGS.Use_SERIAL,
Use_UMS: jsGS.Use_UMS,
Use_HID_RAW: jsGS.Use_HID_RAW,
Use_HID_MOUSE: jsGS.Use_HID_MOUSE,
Use_HID_KEYBOARD: jsGS.Use_HID_KEYBOARD,
Use_RNDIS: jsGS.Use_RNDIS,
Use_CDC_ECM: jsGS.Use_CDC_ECM,
Product: jsGS.Product,
Manufacturer: jsGS.Manufacturer,
Vid: jsGS.Vid,
Pid: jsGS.Pid,
Enabled: jsGS.Enabled,
UmsSettings: &pb.GadgetSettingsUMS{
Cdrom: jsGS.UmsSettings.Cdrom,
File: jsGS.UmsSettings.File,
},
CdcEcmSettings: &pb.GadgetSettingsEthernet{
DevAddr: jsGS.CdcEcmSettings.DevAddr,
HostAddr: jsGS.CdcEcmSettings.HostAddr,
},
RndisSettings: &pb.GadgetSettingsEthernet{
DevAddr: jsGS.RndisSettings.DevAddr,
HostAddr: jsGS.RndisSettings.HostAddr,
},
}
}
func (jsGS *jsGadgetSettings) fromGS(gs *pb.GadgetSettings) {
println(gs)
jsGS.Enabled = gs.Enabled
jsGS.Vid = gs.Vid
jsGS.Pid = gs.Pid
jsGS.Manufacturer = gs.Manufacturer
jsGS.Product = gs.Product
jsGS.Serial = gs.Serial
jsGS.Use_CDC_ECM = gs.Use_CDC_ECM
jsGS.Use_RNDIS = gs.Use_RNDIS
jsGS.Use_HID_KEYBOARD = gs.Use_HID_KEYBOARD
jsGS.Use_HID_MOUSE = gs.Use_HID_MOUSE
jsGS.Use_HID_RAW = gs.Use_HID_RAW
jsGS.Use_UMS = gs.Use_UMS
jsGS.Use_SERIAL = gs.Use_SERIAL
jsGS.RndisSettings = &VGadgetSettingsEthernet{
Object: O(),
}
if gs.RndisSettings != nil {
jsGS.RndisSettings.HostAddr = gs.RndisSettings.HostAddr
jsGS.RndisSettings.DevAddr = gs.RndisSettings.DevAddr
}
jsGS.CdcEcmSettings = &VGadgetSettingsEthernet{
Object: O(),
}
if gs.CdcEcmSettings != nil {
jsGS.CdcEcmSettings.HostAddr = gs.CdcEcmSettings.HostAddr
jsGS.CdcEcmSettings.DevAddr = gs.CdcEcmSettings.DevAddr
}
jsGS.UmsSettings = &VGadgetSettingsUMS{
Object: O(),
}
if gs.UmsSettings != nil {
jsGS.UmsSettings.File = gs.UmsSettings.File
jsGS.UmsSettings.Cdrom = gs.UmsSettings.Cdrom
}
}
func NewUSBGadgetSettings() *jsGadgetSettings {
gs := &jsGadgetSettings{
Object: O(),
}
gs.fromGS(&pb.GadgetSettings{}) //start with empty settings, but create nested structs
return gs
}
/** Events **/
//Log event
type jsEventLog struct {
*js.Object
EvLogSource string `js:"source"`
EvLogLevel int `js:"level"`
EvLogMessage string `js:"message"`
EvLogTime string `js:"time"`
}

View File

@ -4,7 +4,7 @@ import (
pb "../proto/gopherjs"
"honnef.co/go/js/dom"
"github.com/gopherjs/gopherjs/js"
"github.com/HuckRidgeSW/hvue"
"github.com/mame82/hvue"
)
var (
@ -67,6 +67,8 @@ func main() {
}
*/
InitGlobalState() //sets Vuex store in JS window.store
InitCompEthernetAddresses2()
InitCompToggleSwitch()
InitCompUSBSettings()
@ -75,6 +77,7 @@ func main() {
InitCompCodeEditor()
InitCompHIDScript()
InitCompLogger()
InitCompState()
vm := hvue.NewVM(
hvue.El("#app"),
//add "testString" to data
@ -91,7 +94,12 @@ func main() {
"console",
func(vm *hvue.VM) interface{} {
return js.Global.Get("console")
}))
}),
hvue.Computed("state", func(vm *hvue.VM) interface{} {
return vm.Store.Get("state") //works only with Vuex store option added
}),
hvue.Store(), //include Vuex store in global scope, using own hvue fork, see here: https://github.com/HuckRidgeSW/hvue/pull/6
)
js.Global.Set("vm",vm)
}

View File

@ -0,0 +1,83 @@
package mvuex
//ToDo: check for Vuex in js.Global scope and panic if missing
import (
"github.com/gopherjs/gopherjs/js"
"reflect"
)
func o() *js.Object { return js.Global.Get("Object").New() } //Helper to create *js.Object
func castToType(targetType reflect.Type, sourceVal *js.Object) (result reflect.Value, err error) {
switch kind := targetType.Kind(); kind {
case reflect.Int:
//try to convert sourceVal to int before generating reflect.Value
result = reflect.ValueOf(sourceVal.Int())
case reflect.Int8:
result = reflect.ValueOf(int8(sourceVal.Int64()))
case reflect.Int16:
result = reflect.ValueOf(int16(sourceVal.Int64()))
case reflect.Int32:
result = reflect.ValueOf(int32(sourceVal.Int64()))
case reflect.Int64:
result = reflect.ValueOf(sourceVal.Int64())
case reflect.Float64:
result = reflect.ValueOf(sourceVal.Float())
case reflect.Float32:
result = reflect.ValueOf(float32(sourceVal.Float()))
case reflect.Bool:
result = reflect.ValueOf(sourceVal.Bool())
case reflect.Uint:
result = reflect.ValueOf(uint(sourceVal.Uint64()))
case reflect.Uint64:
result = reflect.ValueOf(sourceVal.Uint64())
case reflect.Uint32:
result = reflect.ValueOf(uint32(sourceVal.Uint64()))
case reflect.Uint16:
result = reflect.ValueOf(uint16(sourceVal.Uint64()))
case reflect.Uint8:
result = reflect.ValueOf(uint8(sourceVal.Uint64()))
case reflect.Uintptr:
result = reflect.ValueOf(sourceVal.Unsafe())
case reflect.String:
result = reflect.ValueOf(sourceVal.String())
case reflect.Struct:
//WE ASSUME THAT THE FIRST FIELD OF THE STRUCT IS OF TYPE *js.Object
//create a pointer to a new instance of this struct
pStructInstance := reflect.New(targetType)
//check if first field is *js.Object
field0 := pStructInstance.Elem().Field(0)
if field0.Kind() != reflect.Ptr || field0.Elem().Kind() != kindJsObjectType {
return result, eFirstFieldIsntPtrJsObject
}
//Assign the sourceValue to the first field of the struct, which is assume to be *js.Object
pStructInstance.Elem().Field(0).Set(reflect.ValueOf(sourceVal))
result = pStructInstance.Elem()
case reflect.Ptr:
//follow pointer one level
derefType := targetType.Elem()
//recursive call
derefVal,err := castToType(derefType, sourceVal)
if err != nil { return result, err}
//println("dereferenced Value of type ", derefType.Kind().String(), ": ", derefVal)
//create a pointer to the dereferenced value after it has been created itself
result = derefVal.Addr()
case reflect.Interface:
result = reflect.ValueOf(sourceVal.Interface())
default:
// ToDo: func parsing
println("No conversion for following type implemented", kind.String() , " from ", sourceVal)
}
return result, nil
}

166
web_client/mvuex/store.go Normal file
View File

@ -0,0 +1,166 @@
package mvuex
import (
"github.com/gopherjs/gopherjs/js"
"reflect"
"errors"
)
var (
eTooFewMutationArgs = errors.New("Mutation function has too few arguments (min 2)")
eTooManyMutationArgs = errors.New("Mutation function has too many arguments (max 3)")
eTooFewMutationArgsOnCall = errors.New("Mutation function called with too few arguments from JavaScrip")
eWrongFirstMutationArg = errors.New("Mutation function has to have *Store as first argument type")
eWrongSecondMutationArg = errors.New("The second argument of the mutation function has to be a pointer to a struct of the type used for state")
eFirstFieldIsntPtrJsObject = errors.New("The first field of the struct has to be of type *js.Object")
kindStoreType = reflect.TypeOf(Store{}).Kind()
kindJsObjectType = reflect.TypeOf(js.Object{}).Kind()
)
type StoreOption func(*Config)
type Store struct {
*js.Object
Getters *js.Object `js:"getters"`
Commit func(...interface{}) *js.Object `js:"commit"`
Dispatch func(...interface{}) *js.Object `js:"dispatch"`
Strict bool `js:"strict"`
}
// Config is the config object for NewStore.
type Config struct {
*js.Object
State *js.Object `js:"state"`
Mutations *js.Object `js:"mutations"`
stateValue reflect.Value
}
// Option sets the options specified.
func (c *Config) Option(opts ...StoreOption) {
for _, opt := range opts {
opt(c)
}
}
func State(value interface{}) StoreOption {
return func(c *Config) {
if c.State != js.Undefined {
//if state has been defined before
panic("Cannot use mvuex.Sate together with any other State* options")
}
// ToDo: check if given value is a struct, with first field being of type *js.Object, panic otherwise
c.Object.Set("state", value)
}
}
func wrapGoMutationFunc(reflectedGoFunc reflect.Value ) (jsFunc *js.Object, err error) {
// A mutationfunction is assumed to have this prototype
// func(
// store *Store, //first argument is of type store
// ptrToStateStruct struct{}, //ptr to a struct with same type as the struct provided as state
// additionalArgs ...interface{} //optional arguments for the mutation (Go types)
// )
numGoArgs := reflectedGoFunc.Type().NumIn() //Number of arguments of the Go target method
if numGoArgs < 2 {
return nil, eTooFewMutationArgs
}
if numGoArgs > 3 {
return nil, eTooManyMutationArgs
}
if goArg0 := reflectedGoFunc.Type().In(0); goArg0.Kind() != reflect.Ptr || goArg0.Elem().Kind() != kindStoreType {
return nil, eWrongFirstMutationArg
}
if goArg1 := reflectedGoFunc.Type().In(1); goArg1.Kind() != reflect.Ptr || goArg1.Elem().Kind() != reflect.Struct {
return nil, eWrongSecondMutationArg
}
// Here we know, the goFunc has at least two args, with first arg being *Store type
// and second arg being a custom data struct.
// The JavaScript call received, should provide two args at minimum:
// - arg 0: state data instance
// - arg 1..n: arguments handed in when the respective mutation function is called via commit, in case the
// mutation function is called without arguments, arg 1 is of type "undefined"
// Additionally, the "this" argument provides the store instance, which we hand to the Go function as first argument
// following two lines moved out of the inner function to avoid rerunning
goCallArgTargetTypes := make([]reflect.Type, numGoArgs)
goCallArgsTargetValues := make([]reflect.Value,numGoArgs) //create call args slice, containing the store arg
for i := 0; i < reflectedGoFunc.Type().NumIn(); i++ {
goCallArgTargetTypes[i] = reflectedGoFunc.Type().In(i)
}
jsFunc = js.MakeFunc(func(this *js.Object, arguments []*js.Object) interface{} {
//check if js provides enough args
if len(arguments) < numGoArgs - 1 {
panic(eTooFewMutationArgsOnCall.Error())
}
//Note: All the logic in MakeFunc ends up in the final JS function and reruns every tim the function is triggered
storeVal,err := castToType(goCallArgTargetTypes[0], this) //cast 'this' to type of first function arg (type = *Store)
if err != nil { panic("Error converting JavaScript provided argument for mutation function to *Store: " + err.Error()) }
goCallArgsTargetValues[0] = storeVal
//add the remaining args
for idx,jsArg := range arguments {
//If the target function in Go has less arguments than we got provided from JavaScript, we gonna ignore the rest
targetIdx := idx+1 //offset by one, as we started with *Store as first arg for the Go function
if targetIdx >= numGoArgs { break }
//get method argument type at this poistion
goTargetArgT := goCallArgTargetTypes[targetIdx]
castedArg, err := castToType(goTargetArgT, jsArg)
if err != nil { panic("Error converting JS object to " + goTargetArgT.Kind().String()) }
goCallArgsTargetValues[targetIdx] = castedArg
}
results := reflectedGoFunc.Call(goCallArgsTargetValues)
return results
})
return jsFunc, nil
}
func Mutation(name string, goFunc interface{}) StoreOption {
return func(c *Config) {
println("Creating MUTATION FUNC")
if c.Mutations == js.Undefined { c.Mutations = o() }
reflectedGoFunc := reflect.ValueOf(goFunc)
if reflectedGoFunc.Kind() != reflect.Func { //check if the provided interface is a go function
panic("Mutation " + name + " is not a func")
}
//try to convert the provided function to a JavaScript function usable as Mutation
jsFunc, err := wrapGoMutationFunc(reflectedGoFunc)
if err != nil {panic("Error exposing the mutation function '"+ name + "' to JavaScript: " + err.Error())}
c.Mutations.Set(name, jsFunc)
//c.Mutations.Set(name, makeMethod(name, false, reflectGoFunc.Type(), reflectGoFunc))
}
}
func NewStore(opts ...StoreOption) *Store {
c := &Config{Object: o()}
c.Option(opts...)
store := &Store{Object: js.Global.Get("Vuex").Get("Store").New(c)}
return store
}

7
web_client/rbuild.sh Executable file
View File

@ -0,0 +1,7 @@
#!/bin/bash
# dependencies for the web app
gopherjs build -o ../www/webapp.js #main.go
scp ../www/webapp* pi@raspberrypi.local:~/P4wnP1_go/www/
scp ../www/index.html pi@raspberrypi.local:~/P4wnP1_go/www/
scp ../www/p4wnp1.css pi@raspberrypi.local:~/P4wnP1_go/www/

View File

@ -69,7 +69,7 @@ func (rpc *Rpc) StopEventListening() {
func NewRpcClient(addr string) Rpc {
rcl := Rpc{}
rcl.Mutex = &sync.Mutex{}
cl := pb.NewP4WNP1Client(addr, grpcweb.ForceWebsocket)
cl := pb.NewP4WNP1Client(addr, grpcweb.WithDefaultCallOptions(grpcweb.ForceWebsocketTransport()))
rcl.Client = cl
return rcl
}

View File

@ -3,13 +3,8 @@
<title>P4wnP1 by MaMe82</title>
<link rel="stylesheet" type="text/css" href="p4wnp1.css">
<script src="vue.js"></script>
<style type="text/css">
.script-input {
width: 80%;
height: 80%;
}
<script src="vuex.js"></script>
</style>
@ -22,28 +17,54 @@
<link rel="stylesheet" href="codemirror/addon/hint/show-hint.css">
<script src="javascript-p4wnp1-hint.js"></script>
<style type="text/css">
.CodeMirror {
height: 60%;
}
</style>
</head>
<body>
<h1>P4wnP1 service - USB settings</h1>
<div id="app">
<div id="app">
<h1>{{ state.title }}</h1>
<p>{{ state.text }}</p>
<!--
<table>
<tr>
<td><state></state></td>
<td><state></state></td>
</tr>
</table>
-->
<tabs>
<tab header="USB">
<keep-alive>
<usb-settings></usb-settings>
</keep-alive>
</tab>
<tab header="HID Script" :selected="true">
<hid-script></hid-script>
<hid-script v-model="testString"></hid-script>
</tab>
<tab header="Logger" >
<logger :max-entries="7"></logger>
</tab>
<tab header="bar">blaaaa
<textarea cols="80" rows="50"></textarea>
<tab header="bar">
<table>
<tr>
<td>
<usb-settings></usb-settings>
</td>
<td>
<usb-settings></usb-settings>
</td>
</tr>
</table>
</tab>
</tabs>
</div>
<script type="text/javascript" src="webapp.js"></script>

938
www/vuex.js Normal file
View File

@ -0,0 +1,938 @@
/**
* vuex v3.0.1
* (c) 2017 Evan You
* @license MIT
*/
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global.Vuex = factory());
}(this, (function () { 'use strict';
var applyMixin = function (Vue) {
var version = Number(Vue.version.split('.')[0]);
if (version >= 2) {
Vue.mixin({ beforeCreate: vuexInit });
} else {
// override init and inject vuex init procedure
// for 1.x backwards compatibility.
var _init = Vue.prototype._init;
Vue.prototype._init = function (options) {
if ( options === void 0 ) options = {};
options.init = options.init
? [vuexInit].concat(options.init)
: vuexInit;
_init.call(this, options);
};
}
/**
* Vuex init hook, injected into each instances init hooks list.
*/
function vuexInit () {
var options = this.$options;
// store injection
if (options.store) {
this.$store = typeof options.store === 'function'
? options.store()
: options.store;
} else if (options.parent && options.parent.$store) {
this.$store = options.parent.$store;
}
}
};
var devtoolHook =
typeof window !== 'undefined' &&
window.__VUE_DEVTOOLS_GLOBAL_HOOK__;
function devtoolPlugin (store) {
if (!devtoolHook) { return }
store._devtoolHook = devtoolHook;
devtoolHook.emit('vuex:init', store);
devtoolHook.on('vuex:travel-to-state', function (targetState) {
store.replaceState(targetState);
});
store.subscribe(function (mutation, state) {
devtoolHook.emit('vuex:mutation', mutation, state);
});
}
/**
* Get the first item that pass the test
* by second argument function
*
* @param {Array} list
* @param {Function} f
* @return {*}
*/
/**
* Deep copy the given object considering circular structure.
* This function caches all nested objects and its copies.
* If it detects circular structure, use cached copy to avoid infinite loop.
*
* @param {*} obj
* @param {Array<Object>} cache
* @return {*}
*/
/**
* forEach for object
*/
function forEachValue (obj, fn) {
Object.keys(obj).forEach(function (key) { return fn(obj[key], key); });
}
function isObject (obj) {
return obj !== null && typeof obj === 'object'
}
function isPromise (val) {
return val && typeof val.then === 'function'
}
function assert (condition, msg) {
if (!condition) { throw new Error(("[vuex] " + msg)) }
}
var Module = function Module (rawModule, runtime) {
this.runtime = runtime;
this._children = Object.create(null);
this._rawModule = rawModule;
var rawState = rawModule.state;
this.state = (typeof rawState === 'function' ? rawState() : rawState) || {};
};
var prototypeAccessors$1 = { namespaced: { configurable: true } };
prototypeAccessors$1.namespaced.get = function () {
return !!this._rawModule.namespaced
};
Module.prototype.addChild = function addChild (key, module) {
this._children[key] = module;
};
Module.prototype.removeChild = function removeChild (key) {
delete this._children[key];
};
Module.prototype.getChild = function getChild (key) {
return this._children[key]
};
Module.prototype.update = function update (rawModule) {
this._rawModule.namespaced = rawModule.namespaced;
if (rawModule.actions) {
this._rawModule.actions = rawModule.actions;
}
if (rawModule.mutations) {
this._rawModule.mutations = rawModule.mutations;
}
if (rawModule.getters) {
this._rawModule.getters = rawModule.getters;
}
};
Module.prototype.forEachChild = function forEachChild (fn) {
forEachValue(this._children, fn);
};
Module.prototype.forEachGetter = function forEachGetter (fn) {
if (this._rawModule.getters) {
forEachValue(this._rawModule.getters, fn);
}
};
Module.prototype.forEachAction = function forEachAction (fn) {
if (this._rawModule.actions) {
forEachValue(this._rawModule.actions, fn);
}
};
Module.prototype.forEachMutation = function forEachMutation (fn) {
if (this._rawModule.mutations) {
forEachValue(this._rawModule.mutations, fn);
}
};
Object.defineProperties( Module.prototype, prototypeAccessors$1 );
var ModuleCollection = function ModuleCollection (rawRootModule) {
// register root module (Vuex.Store options)
this.register([], rawRootModule, false);
};
ModuleCollection.prototype.get = function get (path) {
return path.reduce(function (module, key) {
return module.getChild(key)
}, this.root)
};
ModuleCollection.prototype.getNamespace = function getNamespace (path) {
var module = this.root;
return path.reduce(function (namespace, key) {
module = module.getChild(key);
return namespace + (module.namespaced ? key + '/' : '')
}, '')
};
ModuleCollection.prototype.update = function update$1 (rawRootModule) {
update([], this.root, rawRootModule);
};
ModuleCollection.prototype.register = function register (path, rawModule, runtime) {
var this$1 = this;
if ( runtime === void 0 ) runtime = true;
{
assertRawModule(path, rawModule);
}
var newModule = new Module(rawModule, runtime);
if (path.length === 0) {
this.root = newModule;
} else {
var parent = this.get(path.slice(0, -1));
parent.addChild(path[path.length - 1], newModule);
}
// register nested modules
if (rawModule.modules) {
forEachValue(rawModule.modules, function (rawChildModule, key) {
this$1.register(path.concat(key), rawChildModule, runtime);
});
}
};
ModuleCollection.prototype.unregister = function unregister (path) {
var parent = this.get(path.slice(0, -1));
var key = path[path.length - 1];
if (!parent.getChild(key).runtime) { return }
parent.removeChild(key);
};
function update (path, targetModule, newModule) {
{
assertRawModule(path, newModule);
}
// update target module
targetModule.update(newModule);
// update nested modules
if (newModule.modules) {
for (var key in newModule.modules) {
if (!targetModule.getChild(key)) {
{
console.warn(
"[vuex] trying to add a new module '" + key + "' on hot reloading, " +
'manual reload is needed'
);
}
return
}
update(
path.concat(key),
targetModule.getChild(key),
newModule.modules[key]
);
}
}
}
var functionAssert = {
assert: function (value) { return typeof value === 'function'; },
expected: 'function'
};
var objectAssert = {
assert: function (value) { return typeof value === 'function' ||
(typeof value === 'object' && typeof value.handler === 'function'); },
expected: 'function or object with "handler" function'
};
var assertTypes = {
getters: functionAssert,
mutations: functionAssert,
actions: objectAssert
};
function assertRawModule (path, rawModule) {
Object.keys(assertTypes).forEach(function (key) {
if (!rawModule[key]) { return }
var assertOptions = assertTypes[key];
forEachValue(rawModule[key], function (value, type) {
assert(
assertOptions.assert(value),
makeAssertionMessage(path, key, type, value, assertOptions.expected)
);
});
});
}
function makeAssertionMessage (path, key, type, value, expected) {
var buf = key + " should be " + expected + " but \"" + key + "." + type + "\"";
if (path.length > 0) {
buf += " in module \"" + (path.join('.')) + "\"";
}
buf += " is " + (JSON.stringify(value)) + ".";
return buf
}
var Vue; // bind on install
var Store = function Store (options) {
var this$1 = this;
if ( options === void 0 ) options = {};
// Auto install if it is not done yet and `window` has `Vue`.
// To allow users to avoid auto-installation in some cases,
// this code should be placed here. See #731
if (!Vue && typeof window !== 'undefined' && window.Vue) {
install(window.Vue);
}
{
assert(Vue, "must call Vue.use(Vuex) before creating a store instance.");
assert(typeof Promise !== 'undefined', "vuex requires a Promise polyfill in this browser.");
assert(this instanceof Store, "Store must be called with the new operator.");
}
var plugins = options.plugins; if ( plugins === void 0 ) plugins = [];
var strict = options.strict; if ( strict === void 0 ) strict = false;
var state = options.state; if ( state === void 0 ) state = {};
if (typeof state === 'function') {
state = state() || {};
}
// store internal state
this._committing = false;
this._actions = Object.create(null);
this._actionSubscribers = [];
this._mutations = Object.create(null);
this._wrappedGetters = Object.create(null);
this._modules = new ModuleCollection(options);
this._modulesNamespaceMap = Object.create(null);
this._subscribers = [];
this._watcherVM = new Vue();
// bind commit and dispatch to self
var store = this;
var ref = this;
var dispatch = ref.dispatch;
var commit = ref.commit;
this.dispatch = function boundDispatch (type, payload) {
return dispatch.call(store, type, payload)
};
this.commit = function boundCommit (type, payload, options) {
return commit.call(store, type, payload, options)
};
// strict mode
this.strict = strict;
// init root module.
// this also recursively registers all sub-modules
// and collects all module getters inside this._wrappedGetters
installModule(this, state, [], this._modules.root);
// initialize the store vm, which is responsible for the reactivity
// (also registers _wrappedGetters as computed properties)
resetStoreVM(this, state);
// apply plugins
plugins.forEach(function (plugin) { return plugin(this$1); });
if (Vue.config.devtools) {
devtoolPlugin(this);
}
};
var prototypeAccessors = { state: { configurable: true } };
prototypeAccessors.state.get = function () {
return this._vm._data.$$state
};
prototypeAccessors.state.set = function (v) {
{
assert(false, "Use store.replaceState() to explicit replace store state.");
}
};
Store.prototype.commit = function commit (_type, _payload, _options) {
var this$1 = this;
// check object-style commit
var ref = unifyObjectStyle(_type, _payload, _options);
var type = ref.type;
var payload = ref.payload;
var options = ref.options;
var mutation = { type: type, payload: payload };
var entry = this._mutations[type];
if (!entry) {
{
console.error(("[vuex] unknown mutation type: " + type));
}
return
}
this._withCommit(function () {
entry.forEach(function commitIterator (handler) {
handler(payload);
});
});
this._subscribers.forEach(function (sub) { return sub(mutation, this$1.state); });
if (
"development" !== 'production' &&
options && options.silent
) {
console.warn(
"[vuex] mutation type: " + type + ". Silent option has been removed. " +
'Use the filter functionality in the vue-devtools'
);
}
};
Store.prototype.dispatch = function dispatch (_type, _payload) {
var this$1 = this;
// check object-style dispatch
var ref = unifyObjectStyle(_type, _payload);
var type = ref.type;
var payload = ref.payload;
var action = { type: type, payload: payload };
var entry = this._actions[type];
if (!entry) {
{
console.error(("[vuex] unknown action type: " + type));
}
return
}
this._actionSubscribers.forEach(function (sub) { return sub(action, this$1.state); });
return entry.length > 1
? Promise.all(entry.map(function (handler) { return handler(payload); }))
: entry[0](payload)
};
Store.prototype.subscribe = function subscribe (fn) {
return genericSubscribe(fn, this._subscribers)
};
Store.prototype.subscribeAction = function subscribeAction (fn) {
return genericSubscribe(fn, this._actionSubscribers)
};
Store.prototype.watch = function watch (getter, cb, options) {
var this$1 = this;
{
assert(typeof getter === 'function', "store.watch only accepts a function.");
}
return this._watcherVM.$watch(function () { return getter(this$1.state, this$1.getters); }, cb, options)
};
Store.prototype.replaceState = function replaceState (state) {
var this$1 = this;
this._withCommit(function () {
this$1._vm._data.$$state = state;
});
};
Store.prototype.registerModule = function registerModule (path, rawModule, options) {
if ( options === void 0 ) options = {};
if (typeof path === 'string') { path = [path]; }
{
assert(Array.isArray(path), "module path must be a string or an Array.");
assert(path.length > 0, 'cannot register the root module by using registerModule.');
}
this._modules.register(path, rawModule);
installModule(this, this.state, path, this._modules.get(path), options.preserveState);
// reset store to update getters...
resetStoreVM(this, this.state);
};
Store.prototype.unregisterModule = function unregisterModule (path) {
var this$1 = this;
if (typeof path === 'string') { path = [path]; }
{
assert(Array.isArray(path), "module path must be a string or an Array.");
}
this._modules.unregister(path);
this._withCommit(function () {
var parentState = getNestedState(this$1.state, path.slice(0, -1));
Vue.delete(parentState, path[path.length - 1]);
});
resetStore(this);
};
Store.prototype.hotUpdate = function hotUpdate (newOptions) {
this._modules.update(newOptions);
resetStore(this, true);
};
Store.prototype._withCommit = function _withCommit (fn) {
var committing = this._committing;
this._committing = true;
fn();
this._committing = committing;
};
Object.defineProperties( Store.prototype, prototypeAccessors );
function genericSubscribe (fn, subs) {
if (subs.indexOf(fn) < 0) {
subs.push(fn);
}
return function () {
var i = subs.indexOf(fn);
if (i > -1) {
subs.splice(i, 1);
}
}
}
function resetStore (store, hot) {
store._actions = Object.create(null);
store._mutations = Object.create(null);
store._wrappedGetters = Object.create(null);
store._modulesNamespaceMap = Object.create(null);
var state = store.state;
// init all modules
installModule(store, state, [], store._modules.root, true);
// reset vm
resetStoreVM(store, state, hot);
}
function resetStoreVM (store, state, hot) {
var oldVm = store._vm;
// bind store public getters
store.getters = {};
var wrappedGetters = store._wrappedGetters;
var computed = {};
forEachValue(wrappedGetters, function (fn, key) {
// use computed to leverage its lazy-caching mechanism
computed[key] = function () { return fn(store); };
Object.defineProperty(store.getters, key, {
get: function () { return store._vm[key]; },
enumerable: true // for local getters
});
});
// use a Vue instance to store the state tree
// suppress warnings just in case the user has added
// some funky global mixins
var silent = Vue.config.silent;
Vue.config.silent = true;
store._vm = new Vue({
data: {
$$state: state
},
computed: computed
});
Vue.config.silent = silent;
// enable strict mode for new vm
if (store.strict) {
enableStrictMode(store);
}
if (oldVm) {
if (hot) {
// dispatch changes in all subscribed watchers
// to force getter re-evaluation for hot reloading.
store._withCommit(function () {
oldVm._data.$$state = null;
});
}
Vue.nextTick(function () { return oldVm.$destroy(); });
}
}
function installModule (store, rootState, path, module, hot) {
var isRoot = !path.length;
var namespace = store._modules.getNamespace(path);
// register in namespace map
if (module.namespaced) {
store._modulesNamespaceMap[namespace] = module;
}
// set state
if (!isRoot && !hot) {
var parentState = getNestedState(rootState, path.slice(0, -1));
var moduleName = path[path.length - 1];
store._withCommit(function () {
Vue.set(parentState, moduleName, module.state);
});
}
var local = module.context = makeLocalContext(store, namespace, path);
module.forEachMutation(function (mutation, key) {
var namespacedType = namespace + key;
registerMutation(store, namespacedType, mutation, local);
});
module.forEachAction(function (action, key) {
var type = action.root ? key : namespace + key;
var handler = action.handler || action;
registerAction(store, type, handler, local);
});
module.forEachGetter(function (getter, key) {
var namespacedType = namespace + key;
registerGetter(store, namespacedType, getter, local);
});
module.forEachChild(function (child, key) {
installModule(store, rootState, path.concat(key), child, hot);
});
}
/**
* make localized dispatch, commit, getters and state
* if there is no namespace, just use root ones
*/
function makeLocalContext (store, namespace, path) {
var noNamespace = namespace === '';
var local = {
dispatch: noNamespace ? store.dispatch : function (_type, _payload, _options) {
var args = unifyObjectStyle(_type, _payload, _options);
var payload = args.payload;
var options = args.options;
var type = args.type;
if (!options || !options.root) {
type = namespace + type;
if ("development" !== 'production' && !store._actions[type]) {
console.error(("[vuex] unknown local action type: " + (args.type) + ", global type: " + type));
return
}
}
return store.dispatch(type, payload)
},
commit: noNamespace ? store.commit : function (_type, _payload, _options) {
var args = unifyObjectStyle(_type, _payload, _options);
var payload = args.payload;
var options = args.options;
var type = args.type;
if (!options || !options.root) {
type = namespace + type;
if ("development" !== 'production' && !store._mutations[type]) {
console.error(("[vuex] unknown local mutation type: " + (args.type) + ", global type: " + type));
return
}
}
store.commit(type, payload, options);
}
};
// getters and state object must be gotten lazily
// because they will be changed by vm update
Object.defineProperties(local, {
getters: {
get: noNamespace
? function () { return store.getters; }
: function () { return makeLocalGetters(store, namespace); }
},
state: {
get: function () { return getNestedState(store.state, path); }
}
});
return local
}
function makeLocalGetters (store, namespace) {
var gettersProxy = {};
var splitPos = namespace.length;
Object.keys(store.getters).forEach(function (type) {
// skip if the target getter is not match this namespace
if (type.slice(0, splitPos) !== namespace) { return }
// extract local getter type
var localType = type.slice(splitPos);
// Add a port to the getters proxy.
// Define as getter property because
// we do not want to evaluate the getters in this time.
Object.defineProperty(gettersProxy, localType, {
get: function () { return store.getters[type]; },
enumerable: true
});
});
return gettersProxy
}
function registerMutation (store, type, handler, local) {
var entry = store._mutations[type] || (store._mutations[type] = []);
entry.push(function wrappedMutationHandler (payload) {
handler.call(store, local.state, payload);
});
}
function registerAction (store, type, handler, local) {
var entry = store._actions[type] || (store._actions[type] = []);
entry.push(function wrappedActionHandler (payload, cb) {
var res = handler.call(store, {
dispatch: local.dispatch,
commit: local.commit,
getters: local.getters,
state: local.state,
rootGetters: store.getters,
rootState: store.state
}, payload, cb);
if (!isPromise(res)) {
res = Promise.resolve(res);
}
if (store._devtoolHook) {
return res.catch(function (err) {
store._devtoolHook.emit('vuex:error', err);
throw err
})
} else {
return res
}
});
}
function registerGetter (store, type, rawGetter, local) {
if (store._wrappedGetters[type]) {
{
console.error(("[vuex] duplicate getter key: " + type));
}
return
}
store._wrappedGetters[type] = function wrappedGetter (store) {
return rawGetter(
local.state, // local state
local.getters, // local getters
store.state, // root state
store.getters // root getters
)
};
}
function enableStrictMode (store) {
store._vm.$watch(function () { return this._data.$$state }, function () {
{
assert(store._committing, "Do not mutate vuex store state outside mutation handlers.");
}
}, { deep: true, sync: true });
}
function getNestedState (state, path) {
return path.length
? path.reduce(function (state, key) { return state[key]; }, state)
: state
}
function unifyObjectStyle (type, payload, options) {
if (isObject(type) && type.type) {
options = payload;
payload = type;
type = type.type;
}
{
assert(typeof type === 'string', ("Expects string as the type, but found " + (typeof type) + "."));
}
return { type: type, payload: payload, options: options }
}
function install (_Vue) {
if (Vue && _Vue === Vue) {
{
console.error(
'[vuex] already installed. Vue.use(Vuex) should be called only once.'
);
}
return
}
Vue = _Vue;
applyMixin(Vue);
}
var mapState = normalizeNamespace(function (namespace, states) {
var res = {};
normalizeMap(states).forEach(function (ref) {
var key = ref.key;
var val = ref.val;
res[key] = function mappedState () {
var state = this.$store.state;
var getters = this.$store.getters;
if (namespace) {
var module = getModuleByNamespace(this.$store, 'mapState', namespace);
if (!module) {
return
}
state = module.context.state;
getters = module.context.getters;
}
return typeof val === 'function'
? val.call(this, state, getters)
: state[val]
};
// mark vuex getter for devtools
res[key].vuex = true;
});
return res
});
var mapMutations = normalizeNamespace(function (namespace, mutations) {
var res = {};
normalizeMap(mutations).forEach(function (ref) {
var key = ref.key;
var val = ref.val;
res[key] = function mappedMutation () {
var args = [], len = arguments.length;
while ( len-- ) args[ len ] = arguments[ len ];
var commit = this.$store.commit;
if (namespace) {
var module = getModuleByNamespace(this.$store, 'mapMutations', namespace);
if (!module) {
return
}
commit = module.context.commit;
}
return typeof val === 'function'
? val.apply(this, [commit].concat(args))
: commit.apply(this.$store, [val].concat(args))
};
});
return res
});
var mapGetters = normalizeNamespace(function (namespace, getters) {
var res = {};
normalizeMap(getters).forEach(function (ref) {
var key = ref.key;
var val = ref.val;
val = namespace + val;
res[key] = function mappedGetter () {
if (namespace && !getModuleByNamespace(this.$store, 'mapGetters', namespace)) {
return
}
if ("development" !== 'production' && !(val in this.$store.getters)) {
console.error(("[vuex] unknown getter: " + val));
return
}
return this.$store.getters[val]
};
// mark vuex getter for devtools
res[key].vuex = true;
});
return res
});
var mapActions = normalizeNamespace(function (namespace, actions) {
var res = {};
normalizeMap(actions).forEach(function (ref) {
var key = ref.key;
var val = ref.val;
res[key] = function mappedAction () {
var args = [], len = arguments.length;
while ( len-- ) args[ len ] = arguments[ len ];
var dispatch = this.$store.dispatch;
if (namespace) {
var module = getModuleByNamespace(this.$store, 'mapActions', namespace);
if (!module) {
return
}
dispatch = module.context.dispatch;
}
return typeof val === 'function'
? val.apply(this, [dispatch].concat(args))
: dispatch.apply(this.$store, [val].concat(args))
};
});
return res
});
var createNamespacedHelpers = function (namespace) { return ({
mapState: mapState.bind(null, namespace),
mapGetters: mapGetters.bind(null, namespace),
mapMutations: mapMutations.bind(null, namespace),
mapActions: mapActions.bind(null, namespace)
}); };
function normalizeMap (map) {
return Array.isArray(map)
? map.map(function (key) { return ({ key: key, val: key }); })
: Object.keys(map).map(function (key) { return ({ key: key, val: map[key] }); })
}
function normalizeNamespace (fn) {
return function (namespace, map) {
if (typeof namespace !== 'string') {
map = namespace;
namespace = '';
} else if (namespace.charAt(namespace.length - 1) !== '/') {
namespace += '/';
}
return fn(namespace, map)
}
}
function getModuleByNamespace (store, helper, namespace) {
var module = store._modulesNamespaceMap[namespace];
if ("development" !== 'production' && !module) {
console.error(("[vuex] module namespace not found in " + helper + "(): " + namespace));
}
return module
}
var index = {
Store: Store,
install: install,
version: '3.0.1',
mapState: mapState,
mapMutations: mapMutations,
mapGetters: mapGetters,
mapActions: mapActions,
createNamespacedHelpers: createNamespacedHelpers
};
return index;
})));

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long