<commit_before>// +build js

package studyhandler

import (
	"net/url"
	"time"

	"github.com/flimzy/log"
	"github.com/gopherjs/gopherjs/js"
	"github.com/gopherjs/jquery"
	"github.com/pkg/errors"

	"github.com/FlashbackSRS/flashback/cardmodel"
	"github.com/FlashbackSRS/flashback/fserve"
	"github.com/FlashbackSRS/flashback/repository"
)

var jQuery = jquery.NewJQuery

type cardState struct {
	Card      *repo.Card
	StartTime time.Time
	Face      int
}

var currentCard *cardState

// BeforeTransition prepares the page to study
func BeforeTransition(event *jquery.Event, ui *js.Object, _ url.Values) bool {
	u, err := repo.CurrentUser()
	if err != nil {
		log.Printf("No user logged in: %s\n", err)
		return false
	}
	go func() {
		if err := ShowCard(u); err != nil {
			log.Printf("Error showing card: %+v", err)
		}
	}()

	return true
}

func ShowCard(u *repo.User) error {
	// Ensure the indexes are created before trying to use them
	u.DB()

	if currentCard == nil {
		card, err := u.GetNextCard()
		if err != nil {
			return errors.Wrap(err, "fetch card")
		}
		if card == nil {
			return errors.New("got a nil card")
		}
		currentCard = &cardState{
			Card: card,
		}
	}
	log.Debugf("Card ID: %s\n", currentCard.Card.DocID())

	mh, err := currentCard.Card.ModelHandler()
	if err != nil {
		return errors.Wrap(err, "failed to get card's model handler")
	}

	log.Debug("Setting up the buttons\n")
	buttons := jQuery(":mobile-pagecontainer").Find("#answer-buttons").Find(`[data-role="button"]`)
	buttons.On("click", func(e *js.Object) {
		buttons.Off() // Make sure we don't accept other press events
		id := e.Get("currentTarget").Call("getAttribute", "data-id").String()
		log.Debugf("Button %s was pressed!\n", id)
		HandleCardAction(cardmodel.Button(id))
	})
	buttonAttrs, err := mh.Buttons(currentCard.Face)
	if err != nil {
		return errors.Wrap(err, "failed to get buttons list")
	}
	for i := 0; i < buttons.Length; i++ {
		button := jQuery(buttons.Underlying().Index(i))
		id := button.Attr("data-id")
		attr, ok := buttonAttrs[(cardmodel.Button(id))]
		button.Call("button")
		if !ok {
			button.SetText(" ")
		} else {<commit_msg>Clear 'active' state from buttons on page refresh<commit_after>// +build js

package studyhandler

import (
	"net/url"
	"time"

	"github.com/flimzy/log"
	"github.com/gopherjs/gopherjs/js"
	"github.com/gopherjs/jquery"
	"github.com/pkg/errors"

	"github.com/FlashbackSRS/flashback/cardmodel"
	"github.com/FlashbackSRS/flashback/fserve"
	"github.com/FlashbackSRS/flashback/repository"
)

var jQuery = jquery.NewJQuery

type cardState struct {
	Card      *repo.Card
	StartTime time.Time
	Face      int
}

var currentCard *cardState

// BeforeTransition prepares the page to study
func BeforeTransition(event *jquery.Event, ui *js.Object, _ url.Values) bool {
	u, err := repo.CurrentUser()
	if err != nil {
		log.Printf("No user logged in: %s\n", err)
		return false
	}
	go func() {
		if err := ShowCard(u); err != nil {
			log.Printf("Error showing card: %+v", err)
		}
	}()

	return true
}

func ShowCard(u *repo.User) error {
	// Ensure the indexes are created before trying to use them
	u.DB()

	if currentCard == nil {
		card, err := u.GetNextCard()
		if err != nil {
			return errors.Wrap(err, "fetch card")
		}
		if card == nil {
			return errors.New("got a nil card")
		}
		currentCard = &cardState{
			Card: card,
		}
	}
	log.Debugf("Card ID: %s\n", currentCard.Card.DocID())

	mh, err := currentCard.Card.ModelHandler()
	if err != nil {
		return errors.Wrap(err, "failed to get card's model handler")
	}

	log.Debug("Setting up the buttons\n")
	buttons := jQuery(":mobile-pagecontainer").Find("#answer-buttons").Find(`[data-role="button"]`)
	buttons.RemoveClass("ui-btn-active")
	buttons.On("click", func(e *js.Object) {
		buttons.Off() // Make sure we don't accept other press events
		id := e.Get("currentTarget").Call("getAttribute", "data-id").String()
		log.Debugf("Button %s was pressed!\n", id)
		HandleCardAction(cardmodel.Button(id))
	})
	buttonAttrs, err := mh.Buttons(currentCard.Face)
	if err != nil {
		return errors.Wrap(err, "failed to get buttons list")
	}
	for i := 0; i < buttons.Length; i++ {
		button := jQuery(buttons.Underlying().Index(i))
		id := button.Attr("data-id")
		attr, ok := buttonAttrs[(cardmodel.Button(id))]
		button.Call("button")
		if !ok {
			button.SetText(" ")
		} else {
{'fraction_non_alphanumeric': 0.15940366972477063, 'fraction_numerical': 0.00045871559633027525, 'mean_word_length': 3.8401775804661487, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3202956', 'n_tokens_mistral': 1643, 'n_tokens_neox': 1552, 'n_words': 382}