diff --git a/openlibrary/plugins/openlibrary/js/index.js b/openlibrary/plugins/openlibrary/js/index.js index b90a5bdc604..b29c4ae7cc7 100644 --- a/openlibrary/plugins/openlibrary/js/index.js +++ b/openlibrary/plugins/openlibrary/js/index.js @@ -11,7 +11,7 @@ import automaticInit from './automatic'; import bookReaderInit from './bookreader_direct'; import { ungettext, ugettext, sprintf } from './i18n'; import jQueryRepeat from './jquery.repeat'; -import { enumerate, htmlquote, websafe, foreach, join, len, range } from './jsdef'; +import { enumerate, htmlquote, websafe, foreach, join, len, range, jsdef_get } from './jsdef'; import initAnalytics from './ol.analytics'; import init from './ol.js'; import * as Browser from './Browser'; @@ -32,6 +32,7 @@ window.cond = cond; window.enumerate = enumerate; window.foreach = foreach; window.htmlquote = htmlquote; +window.jsdef_get = jsdef_get; window.len = len; window.range = range; window.slice = slice; diff --git a/openlibrary/plugins/openlibrary/js/jsdef.js b/openlibrary/plugins/openlibrary/js/jsdef.js index 3611e4f1ead..af763e4f7fe 100644 --- a/openlibrary/plugins/openlibrary/js/jsdef.js +++ b/openlibrary/plugins/openlibrary/js/jsdef.js @@ -145,3 +145,17 @@ export function htmlquote(text) { export function is_jsdef() { return true; } + + +/** + * foo.get(KEY, default) isn't defined in js, so we can't use that construct + * in our jsdef methods. This helper function provides a workaround, and works + * in both environments. + * + * @param {object} obj - the object to get the key from + * @param {string} key - the key to get from the object + * @param {any} def - the default value to return if the key isn't found + */ +export function jsdef_get(obj, key, def=null) { + return (key in obj) ? obj[key] : def; +} diff --git a/openlibrary/plugins/upstream/jsdef.py b/openlibrary/plugins/upstream/jsdef.py index 745e8e18b70..2c5978b7eff 100644 --- a/openlibrary/plugins/upstream/jsdef.py +++ b/openlibrary/plugins/upstream/jsdef.py @@ -229,7 +229,7 @@ def py2js(expr): >>> py2js("x or not y") 'x || ! y' """ - d = {"and": "&&", "or": "||", "not": "!"} + d = {"and": "&&", "or": "||", "not": "!", "None": "null"} def f(tokens): for t in tokens: diff --git a/openlibrary/plugins/upstream/utils.py b/openlibrary/plugins/upstream/utils.py index 506f0fe6727..ff64a1e33de 100644 --- a/openlibrary/plugins/upstream/utils.py +++ b/openlibrary/plugins/upstream/utils.py @@ -1437,6 +1437,16 @@ def is_jsdef(): return False +@public +def jsdef_get(obj, key, default=None): + """ + foo.get(KEY, default) isn't defined in js, so we can't use that construct + in our jsdef methods. This helper function provides a workaround, and works + in both environments. + """ + return obj.get(key, default) + + @public def get_donation_include() -> str: ia_host = get_ia_host(allow_dev=True) diff --git a/openlibrary/templates/type/list/edit.html b/openlibrary/templates/type/list/edit.html index 6538b22fc79..a3aacf7e9c3 100644 --- a/openlibrary/templates/type/list/edit.html +++ b/openlibrary/templates/type/list/edit.html @@ -20,8 +20,9 @@