// +build js package main import ( //"github.com/mame82/hvue" "github.com/gopherjs/gopherjs/js" pb "github.com/mame82/P4wnP1_aloa/proto/gopherjs" "github.com/mame82/hvue" ) type jsDataTablePagination struct { *js.Object RowsPerPage int `js:"rowsPerPage"` Descending bool `js:"descending"` Page int `js:"page"` } func newPagination(rowsPerPage int, startPage int) (res *jsDataTablePagination) { res = &jsDataTablePagination{Object: O()} res.RowsPerPage = rowsPerPage res.Page = startPage res.Descending = false return } func InitComponentsNetwork() { hvue.NewComponent( "network", hvue.Template(templateNetwork), hvue.DataFunc(func(vm *hvue.VM) interface{} { data := &struct { *js.Object CurrentInterface int `js:"currentIdx"` ShowStoreModal bool `js:"showStoreModal"` ShowLoadModal bool `js:"showLoadModal"` ShowDeployStoredModal bool `js:"showDeployStoredModal"` }{Object: O()} data.CurrentInterface = 0 data.ShowStoreModal = false data.ShowLoadModal = false data.ShowDeployStoredModal = false return data }), hvue.Computed("interfaces", func(vm *hvue.VM) interface{} { return vm.Get("$store").Get("state").Get("InterfaceSettings").Get("interfaces") }), // converts interface array to array which could be used with Quasar q-select (every object item has label and value) hvue.Computed("selectOptionsInterface", func(vm *hvue.VM) interface{} { selectIf := js.Global.Get("Array").New() interfaces := vm.Get("$store").Get("state").Get("InterfaceSettings").Get("interfaces") for i := 0; i < interfaces.Length(); i++ { option := struct { *js.Object Label string `js:"label"` Value int `js:"value"` }{Object: O()} currentIf := &jsEthernetInterfaceSettings{ Object: interfaces.Index(i), } option.Label = currentIf.Name option.Value = i selectIf.Call("push", option) } return selectIf }), hvue.Computed("current", func(vm *hvue.VM) interface{} { interfaces := vm.Get("$store").Get("state").Get("InterfaceSettings").Get("interfaces") idx := vm.Get("currentIdx").Int() currentIface := interfaces.Index(idx) if currentIface == js.Undefined { return &jsEthernetInterfaceSettings{Object:O()} } return currentIface }), hvue.Computed("currentWithDhcp", func(vm *hvue.VM) interface{} { if mode := vm.Get("current").Get("mode").Int(); mode == pb.EthernetInterfaceSettings_Mode_value["DHCP_SERVER"] { return true } else { return false } }), hvue.Method("deploy", func(vm *hvue.VM, ifaceSettings *jsEthernetInterfaceSettings) { vm.Get("$store").Call("dispatch", VUEX_ACTION_DEPLOY_ETHERNET_INTERFACE_SETTINGS, ifaceSettings) }), hvue.Method("updateStoredSettingsList", func(vm *hvue.VM) { vm.Store.Call("dispatch", VUEX_ACTION_UPDATE_STORED_ETHERNET_INTERFACE_SETTINGS_LIST) }), hvue.Method("store", func(vm *hvue.VM, name *js.Object) { sReq := NewEthernetRequestSettingsStorage() sReq.TemplateName = name.String() sReq.Settings = &jsEthernetInterfaceSettings{ Object: vm.Get("current"), } println("Storing :", sReq) vm.Get("$store").Call("dispatch", VUEX_ACTION_STORE_ETHERNET_INTERFACE_SETTINGS, sReq) vm.Set("showStoreModal", false) }), hvue.Method("load", func(vm *hvue.VM, name *js.Object) { println("Loading :", name.String()) vm.Get("$store").Call("dispatch", VUEX_ACTION_LOAD_ETHERNET_INTERFACE_SETTINGS, name) }), hvue.Method("deployStored", func(vm *hvue.VM, name *js.Object) { println("Loading :", name.String()) vm.Get("$store").Call("dispatch", VUEX_ACTION_DEPLOY_STORED_ETHERNET_INTERFACE_SETTINGS, name) }), hvue.Method("deleteStored", func(vm *hvue.VM, name *js.Object) { println("Deleting template :", name.String()) vm.Get("$store").Call("dispatch", VUEX_ACTION_DELETE_STORED_ETHERNET_INTERFACE_SETTINGS, name) }), // The following method doesn't make much sense anymore, but is kept as an example for working with promises hvue.Mounted(func(vm *hvue.VM) { // update network interface println("ethernet settings component mounted") promise := vm.Store.Call("dispatch", VUEX_ACTION_UPDATE_ALL_ETHERNET_INTERFACE_SETTINGS) promise.Call("then", func() { /* println("Mounting network interface settings, try to select first interface") // current" could be an empty settings object, set to first interface of computed property "interfaces" (if there is one) interfaces := vm.Get("$store").Get("state").Get("InterfaceSettings").Get("interfaces") if interfaces.Length() > 0 { hvue.Set(vm, "current", interfaces.Index(0)) println("... current is", vm.Get("current")) } else { println("... No interface found") } */ println("ethernet interface settings reloaded") hvue.Set(vm, "currentIdx", 0) }, func() { println("error in THEN ") }, ) }), ) hvue.NewComponent( "network-interface-settings", hvue.Template(templateNetworkInterface), hvue.Props("interface"), hvue.Computed("selectOptionsInterfaceModes", func(vm *hvue.VM) interface{} { modes := js.Global.Get("Array").New() for val, name := range pb.EthernetInterfaceSettings_Mode_name { mode := struct { *js.Object Val int `js:"value"` Name string `js:"label"` }{Object: O()} mode.Val = val mode.Name = name modes.Call("push", mode) } return modes }), hvue.Computed("withIP", func(vm *hvue.VM) interface{} { if mode := vm.Get("interface").Get("mode").Int(); mode == pb.EthernetInterfaceSettings_Mode_value["MANUAL"] || mode == pb.EthernetInterfaceSettings_Mode_value["DHCP_SERVER"] { return true } else { return false } }), hvue.Computed("withDhcp", func(vm *hvue.VM) interface{} { if mode := vm.Get("interface").Get("mode").Int(); mode == pb.EthernetInterfaceSettings_Mode_value["DHCP_SERVER"] { return true } else { return false } }), hvue.Method("deploy", func(vm *hvue.VM) { vm.Get("$store").Call("dispatch", VUEX_ACTION_DEPLOY_ETHERNET_INTERFACE_SETTINGS, vm.Get("interface")) }), ) hvue.NewComponent("dhcp-config", hvue.Props("interface"), hvue.Template(templateDHCPConfig), hvue.Computed("config", func(vm *hvue.VM) interface{} { if vm.Get("interface").Get("dhcpServerSettings") == js.Undefined { // no DHCP server settings present //cast interface to struct iface := &jsEthernetInterfaceSettings{Object: vm.Get("interface")} iface.CreateDhcpSettingsForInterface() //Create proper DHCP server settings for interface } return vm.Get("interface").Get("dhcpServerSettings") }), hvue.ComputedWithGetSet("authoritative", func(vm *hvue.VM) interface{} { return !vm.Get("config").Get("nonAuthoritative").Bool() }, func(vm *hvue.VM, newValue *js.Object) { vm.Get("config").Set("nonAuthoritative", !newValue.Bool()) }), ) hvue.NewComponent("dhcp-ranges", hvue.Template(templateDHCPRanges), hvue.Props("config"), hvue.Method("addRange", func(vm *hvue.VM) { s := &jsDHCPServerSettings{Object: vm.Get("config")} r := &jsDHCPServerRange{Object: O()} r.RangeLower = "" r.RangeUpper = "" r.LeaseTime = "1m" s.AddRange(r) }), hvue.Method("removeRange", func(vm *hvue.VM, delRange *jsDHCPServerRange) { s := &jsDHCPServerSettings{Object: vm.Get("config")} s.RemoveRange(delRange) }), hvue.DataFunc(func(vm *hvue.VM) interface{} { data := &struct { *js.Object Pagination *jsDataTablePagination `js:"pagination"` }{Object: O()} data.Pagination = newPagination(3, 1) return data }), ) hvue.NewComponent("dhcp-options", hvue.Props("config"), hvue.Template(templateDHCPOptions), hvue.Method("addOption", func(vm *hvue.VM) { s := &jsDHCPServerSettings{Object: vm.Get("config")} o := &jsDHCPServerOption{Object: O()} o.Option = 3 o.Value = "" s.AddOption(o) }), hvue.Method("removeOption", func(vm *hvue.VM, delOption *jsDHCPServerOption) { s := &jsDHCPServerSettings{Object: vm.Get("config")} s.RemoveOption(delOption) }), hvue.DataFunc(func(vm *hvue.VM) interface{} { data := &struct { *js.Object Pagination *jsDataTablePagination `js:"pagination"` }{Object: O()} data.Pagination = newPagination(3, 1) return data }), ) hvue.NewComponent("dhcp-static-hosts", hvue.Props("config"), hvue.Template(templateDHCPStaticHosts), hvue.Method("addStaticHost", func(vm *hvue.VM) { s := &jsDHCPServerSettings{Object: vm.Get("config")} sh := &jsDHCPServerStaticHost{Object: O()} sh.Ip = "" sh.Mac = "" s.AddStaticHost(sh) }), hvue.Method("removeStaticHost", func(vm *hvue.VM, delStaticHost *jsDHCPServerStaticHost) { s := &jsDHCPServerSettings{Object: vm.Get("config")} s.RemoveStaticHost(delStaticHost) }), hvue.DataFunc(func(vm *hvue.VM) interface{} { data := &struct { *js.Object Pagination *jsDataTablePagination `js:"pagination"` }{Object: O()} data.Pagination = newPagination(3, 1) return data }), ) } const templateNetwork = `
Network Interface Settings
Generic Interface Select which interface to configure
` const templateDHCPConfig = ` DHCP Server settings for {{ interface.name }} Authoritative If disabled, the DHCP Server isn't authoritative Path to lease file {{ config.leaseFile }} DHCP ranges DHCP options DHCP static hosts ` const templateDHCPRanges = ` {{ props.cols[0].label }} {{ props.cols[1].label }} {{ props.cols[2].label }} add del
delete range {{ props.row.__index + 1 }} {{ props.colsMap.lower.label }} {{ props.colsMap.upper.label }} {{ props.colsMap.lease.label }}
` const templateDHCPOptions = ` {{ props.cols[0].label }} {{ props.cols[1].label }} add del
delete option {{ props.row.__index + 1 }} {{ props.colsMap.optnumber.label }} {{ props.colsMap.optvalue.label }}
` const templateDHCPStaticHosts = ` {{ props.cols[0].label }} {{ props.cols[1].label }} add del
delete {{ props.colsMap.hostmac.label }} {{ props.colsMap.hostip.label }}
` const templateNetworkInterface = ` Generic settings for {{interface.name}} Enabled Enable/Disable interface Mode Enable DHCP server, client or manual configuration IP IPv4 address of interface in dotted decimal (f.e. 172.16.0.1) Netmask Netmask of interface in dotted decimal (f.e. 255.255.255.0) `