Esempio n. 1
0
func Keypress(model Model, event dom.Event) Model {
	if t, ok := event.Target().(*dom.HTMLInputElement); ok {
		// TODO: save/restore focus and cursor position
		return Model(t.Value)
	}
	return model
}
Esempio n. 2
0
// CreateTodo is an event listener which creates a new todo and adds it to the
// todo list.
func (v *App) CreateTodo(ev dom.Event) {
	input, ok := ev.Target().(*dom.HTMLInputElement)
	if !ok {
		panic("Could not convert event target to dom.HTMLInputElement")
	}
	v.Todos.AddTodo(input.Value)
	document.QuerySelector(".new-todo").(dom.HTMLElement).Focus()
}
Esempio n. 3
0
// ToggleAll toggles all the todos in the list.
func (v *App) ToggleAll(ev dom.Event) {
	input := ev.Target().(*dom.HTMLInputElement)
	if !input.Checked {
		v.Todos.UncheckAll()
	} else {
		v.Todos.CheckAll()
	}
}
func paliIME(event dom.Event) {
	elm := event.Target().(*dom.HTMLInputElement)

	if lastInput != "" && elm.Value != "" {
		v := elm.Value
		if len(v) == (len(lastInput) + 1) {
			result := checkLastTwoCharacter(lastInput[len(lastInput)-1:], v[len(v)-1:])
			if result != "" {
				elm.Value = v[:len(v)-2] + result
			}
		}
	}
	lastInput = elm.Value
}
Esempio n. 5
0
// UpdateRepository updates specified repository.
// repoRoot is the import path corresponding to the root of the repository.
func UpdateRepository(event dom.Event, repoRoot string) {
	event.PreventDefault()
	if event.(*dom.MouseEvent).Button != 0 {
		return
	}

	repoUpdate := document.GetElementByID(repoRoot)
	updateButton := repoUpdate.GetElementsByClassName("update-button")[0].(*dom.HTMLAnchorElement)

	updateButton.SetTextContent("Updating...")
	updateButton.AddEventListener("click", false, func(event dom.Event) { event.PreventDefault() })
	updateButton.SetTabIndex(-1)
	updateButton.Class().Add("disabled")

	go func() {
		req := xhr.NewRequest("POST", "/-/update")
		req.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded")
		err := req.Send(url.Values{"repo_root": {repoRoot}}.Encode())
		if err != nil {
			println(err.Error())
			return
		}

		// Hide the "Updating..." label.
		updateButton.Style().SetProperty("display", "none", "")

		// Show "No Updates Available" if there are no remaining updates.
		if !anyUpdatesRemaining() {
			document.GetElementByID("no_updates").(dom.HTMLElement).Style().SetProperty("display", "", "")
		}

		// Move this Go package to "Installed Updates" list.
		installedUpdates := document.GetElementByID("installed_updates").(dom.HTMLElement)
		installedUpdates.Style().SetProperty("display", "", "")
		installedUpdates.ParentNode().InsertBefore(repoUpdate, installedUpdates.NextSibling()) // Insert after.
	}()
}
Esempio n. 6
0
func (v *NewPerson) CreatePerson(ev dom.Event) {
	ev.PreventDefault()
	formEl, ok := ev.CurrentTarget().(*dom.HTMLFormElement)
	if !ok {
		panic("Could not cast target to dom.HTMLFormElement: " + fmt.Sprintf("%T", ev.CurrentTarget()))
	}
	f, err := form.Parse(formEl)
	if err != nil {
		panic(err)
	}
	f.Validate("name").Required()
	f.Validate("age").Required().IsInt().Greater(0)
	if f.HasErrors() {
		if errorsView == nil {
			errorsView = NewErrors(f.Errors)
			view.InsertBefore(errorsView, v)
		}
		errorsView.Errors = f.Errors
		if err := errorsView.Render(); err != nil {
			panic(err)
		}
		return
	}
	person := &models.Person{}
	if err := f.Bind(person); err != nil {
		panic(err)
	}
	restClient := rest.NewClient()
	restClient.ContentType = rest.ContentJSON
	go func() {
		if err := restClient.Create(person); err != nil {
			if httpErr, ok := err.(rest.HTTPError); ok {
				fmt.Println(string(httpErr.Body))
			}
			panic(err)
		}
		v.Router.Navigate("/people")
	}()
}
Esempio n. 7
0
func inputKeyUp(event dom.Event) {
	input := event.Target().(*dom.HTMLInputElement)

	span := dom.GetWindow().Document().GetElementByID("foo2")
	span.SetInnerHTML(input.Value)
}