Skip to content
This repository has been archived by the owner on Dec 21, 2023. It is now read-only.

Commit

Permalink
modularized the javascript
Browse files Browse the repository at this point in the history
  • Loading branch information
dirkroorda committed Jun 25, 2021
1 parent 629f0fe commit b7b9080
Show file tree
Hide file tree
Showing 33 changed files with 5,045 additions and 4,931 deletions.
10 changes: 6 additions & 4 deletions static/js/.eslintrc → .eslintrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
parserOptions:
sourceType: script
sourceType: module
failOnError: true
ecmaVersion: 2021

Expand Down Expand Up @@ -28,7 +28,8 @@ rules:
- warn
- ignoreRestSiblings: true
no-implicit-globals: off
no-invalid-this: off
no-invalid-this:
- error
no-loop-func: warn
no-multi-spaces: warn
no-new: warn
Expand All @@ -41,7 +42,7 @@ rules:
no-useless-concat: warn
no-useless-escape: warn
camelcase:
- warn
- off
- properties: always
comma-dangle:
- warn
Expand Down Expand Up @@ -137,7 +138,8 @@ rules:
- enforceForRenamedProperties: false
prefer-rest-params: warn
prefer-spread: warn
prefer-template: off
prefer-template:
- warn
rest-spread-spacing:
- warn
- never
Expand Down
6 changes: 4 additions & 2 deletions controllers/hebrew.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ def text():

return dict(
viewsettings=Viewsettings(cache, passage_dbs, URL, versions),
versions=versions,
colorpicker=colorpicker,
legend=legend,
tp_labels=tp_labels,
Expand Down Expand Up @@ -1440,8 +1439,11 @@ def words_page(viewsettings, vr, lan=None, letter=None):
cache,
"words_data_{}_".format(vr), lambda: get_words_data(vr), None
)
version = viewsettings.versionstate()

return dict(
versionstate=viewsettings.versionstate(),
version=version,
viewsettings=viewsettings,
lan=lan,
letter=letter,
letters=letters,
Expand Down
41 changes: 13 additions & 28 deletions modules/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -924,9 +924,11 @@ def __init__(self, cache, passage_dbs, URL, versions):
lambda: collections.defaultdict(lambda: {})
)
self.pref = get_request_val("rest", "", "pref")

self.versions = {
k: v for (k, v) in versions.items() if versions[k]["present"] is not None
v: info for (v, info) in versions.items() if info["present"] is not None
}

for group in settings:
self.state[group] = {}
for qw in settings[group]:
Expand Down Expand Up @@ -994,26 +996,18 @@ def __init__(self, cache, passage_dbs, URL, versions):
cache = self.cache
passage_dbs = self.passage_dbs

for vr in versions:
if not versions[vr]["present"]:
for (v, vinfo) in self.versions.items():
if not vinfo["present"]:
continue
(books[vr], books_order[vr], book_id[vr], book_name[vr]) = from_cache(
cache, "books_{}_".format(vr), lambda: get_books(passage_dbs, vr), None
(books[v], books_order[v], book_id[v], book_name[v]) = from_cache(
cache, "books_{}_".format(v), lambda: get_books(passage_dbs, v), None
)

def theversion(self):
return self.state["material"][""]["version"]

def versionstate(self):
return """
var versions = {versions}
var version = '{version}'
""".format(
versions=json.dumps(
dict((v, self.versions[v]["present"]) for v in self.versions)
),
version=self.state["material"][""]["version"],
)
return self.state["material"][""]["version"]

def writeConfig(self):
URL = self.URL
Expand Down Expand Up @@ -1081,7 +1075,7 @@ def writeConfig(self):
dncols=dncols,
dnrows=dnrows,
versions=json.dumps(
dict((v, self.versions[v]["present"]) for v in self.versions)
[v for (v, inf) in self.versions.items() if inf["present"]]
),
tp_labels=json.dumps(tp_labels),
tr_labels=json.dumps(tr_labels),
Expand Down Expand Up @@ -1120,21 +1114,12 @@ def writeConfig(self):
pn_url=URL("hebrew", "note_tree", extension="json"),
n_url=URL("hebrew", "text", extension=""),
upload_url=URL("hebrew", "note_upload", extension="json"),
pq_url=URL('hebrew', 'query_tree', extension='json'),
queriesr_url=URL('hebrew', 'queriesr', extension='json'),
q_url=URL('hebrew', 'text', extension=''),
record_url=URL('hebrew', 'record', extension='json'),
pq_url=URL("hebrew", "query_tree", extension="json"),
queriesr_url=URL("hebrew", "queriesr", extension="json"),
q_url=URL("hebrew", "text", extension=""),
record_url=URL("hebrew", "record", extension="json"),
)

def dynamics(self):
config = self.writeConfig()

return f"""
{config}
var P = setup()
dynamics(P)
"""


def h_esc(material, fill=True):
material = (
Expand Down
201 changes: 201 additions & 0 deletions static/js/app/colorpicker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
/* eslint-env jquery */
/* globals Config, P */

import { close_dialog, defcolor } from "./helpers.js"

export class Colorpicker1 {
/* the colorpicker associated with individual items
*
* These pickers show up in lists of items (in mq and mw sidebars) and
* near individual items (in rq and rw sidebars).
* They also have a checkbox, stating whether the color counts as customized.
* Customized colors are held in a global colormap,
* which is saved in a cookie upon every picking action.
*
* All actions are processed by the highlight2 (!) method
* of the associated Settings object.
*/
constructor(qw, iid, is_item, do_highlight) {
const { style, vcolors } = Config

const pointer = is_item ? "me" : iid
this.code = is_item ? "1a" : "1"
this.qw = qw
this.iid = iid
this.picker = $(`#picker_${qw}${pointer}`)
this.stl = style[qw]["prop"]
this.sel = $(`#sel_${qw}${pointer}`)
this.selw = $(`#sel_${qw}${pointer}>a`)
this.selc = $(`#selc_${qw}${pointer}`)

this.sel.click(e => {
e.preventDefault()
this.picker.dialog({
dialogClass: "picker_dialog",
closeOnEscape: true,
modal: true,
title: "choose a color",
position: { my: "right top", at: "left top", of: this.selc },
width: "200px",
})
})

this.selc.click(() => {
/* process a click on the selectbox of the picker
*/
const { qw, iid, picker } = this
const was_cust = P.vs.iscolor(qw, iid)
close_dialog(picker)
if (was_cust) {
P.vs.cstatex(qw, iid)
} else {
const vals = {}
vals[iid] = defcolor(qw == "q", iid)
P.vs.cstatesv(qw, vals)
const active = P.vs.active(qw)
if (active != "hlcustom" && active != "hlmany") {
P.vs.hstatesv(qw, { active: "hlcustom" })
}
}
P.vs.addHist()
this.apply(true)
})

$(`.c${qw}.${qw}${pointer}>a`).click(e => {
/* process a click on a colored cell of the picker
*/
e.preventDefault()
const elem = $(e.target)
const { picker } = this
close_dialog(picker)

const { qw, iid } = this
const vals = {}
vals[iid] = elem.html()
P.vs.cstatesv(qw, vals)
P.vs.hstatesv(qw, { active: "hlcustom" })
P.vs.addHist()
this.apply(true)
})

this.picker.hide()
$(`.c${qw}.${qw}${pointer}>a`).each((i, el) => {
/* initialize the individual color cells in the picker
*/
const elem = $(el)
const { qw } = this
const target = qw == "q" ? elem.closest("td") : elem
target.css(this.stl, vcolors[elem.html()][qw])
})
this.apply(do_highlight)
}

adapt(iid, do_highlight) {
this.iid = iid
this.apply(do_highlight)
}

apply(do_highlight) {
const { vcolors } = Config

const { qw, iid, stl, sel, selc, selw } = this
const color = P.vs.color(qw, iid) || defcolor(qw == "q", iid)
const target = qw == "q" ? sel : selw
if (color) {
target.css(stl, vcolors[color][qw])
/* apply state to the selected cell
*/
}
selc.prop("checked", P.vs.iscolor(qw, iid))
/* apply state to the checkbox
*/
if (do_highlight) {
P.highlight2(this)
}
}
}

export class Colorpicker2 {
/* the colorpicker associated with the view settings in a sidebar
*
* These pickers show up at the top of the individual sidebars,
* only on mq and mw sidebars.
* They are used to control the uniform color with which
* the results are to be painted.
* They can be configured for dealing with background or foreground painting.
* The paint actions depend on the mode of coloring
* that the user has selected in settings.
* So the paint logic is more involved.
* But there is no associated checkbox.
* The selected color is stored in the highlight settings,
* which are synchronized in a cookie.
* All actions are processed by the highlight2 method
* of the associated Settings object.
*/
constructor(qw, do_highlight) {
const { style, vcolors } = Config

this.code = "2"
this.qw = qw
this.picker = $(`#picker_${qw}one`)
this.stl = style[this.qw]["prop"]
this.sel = $(`#sel_${qw}one`)
this.selw = $(`#sel_${qw}one>a`)

this.sel.click(e => {
e.preventDefault()
this.picker.dialog({
dialogClass: "picker_dialog",
closeOnEscape: true,
modal: true,
title: "choose a color",
position: { my: "right top", at: "left top", of: this.sel },
width: "200px",
})
})

$(`.c${qw}.${qw}one>a`).click(e => {
/* process a click on a colored cell of the picker
*/
e.preventDefault()
const elem = $(e.target)
const { picker } = this
close_dialog(picker)
const { qw } = this
const current_active = P.vs.active(qw)
if (current_active != "hlone" && current_active != "hlcustom") {
P.vs.hstatesv(qw, { active: "hlcustom", sel_one: elem.html() })
} else {
P.vs.hstatesv(qw, { sel_one: elem.html() })
}
P.vs.addHist()
this.apply(true)
})

this.picker.hide()

$(`.c${qw}.${qw}one>a`).each((i, el) => {
/* initialize the individual color cells in the picker
*/
const elem = $(el)
const { qw, stl } = this
const target = qw == "q" ? elem.closest("td") : elem
target.css(stl, vcolors[elem.html()][qw])
})
this.apply(do_highlight)
}

apply(do_highlight) {
const { vcolors } = Config

const { qw, stl, sel, selw } = this
const color = P.vs.sel_one(qw) || defcolor(qw, null)
const target = qw == "q" ? sel : selw
target.css(stl, vcolors[color][qw])
/* apply state to the selected cell
*/
if (do_highlight) {
P.highlight2(this)
}
}
}
Loading

0 comments on commit b7b9080

Please sign in to comment.