1
0
mirror of https://github.com/gnosygnu/xowa.git synced 2026-03-02 03:49:30 +00:00

HTTP Server: Support popups [#264]

This commit is contained in:
gnosygnu
2018-11-25 10:17:03 -05:00
parent 2e5994788f
commit 38bdc95acf
28 changed files with 1375 additions and 425 deletions

View File

@@ -41,7 +41,7 @@ public class Xoh_head_itm__popups extends Xoh_head_itm__base {
}
@Override public void Write_js_tail_script(Xoae_app app, Xowe_wiki wiki, Xoae_page page, Xoh_head_wtr wtr) {
wtr.Write_js_line(Jquery_init); // NOTE: must assert that jquery is init'd, else popup.js will not compile after going back / forward; DATE:2014-09-10
wtr.Write_js_tail_load_lib(app.Fsys_mgr().Bin_any_dir().GenSubFil_nest("xowa", "html", "res", "src", "xowa", "popups", "popups.js"));
wtr.Write_js_tail_load_lib(app.Fsys_mgr().Bin_any_dir().GenSubFil_nest("xowa", "html", "res", "src", "xowa", "popups", "xo.popup.js"));
} public static final byte[] Jquery_init = Bry_.new_a7("xowa.js.jquery.init();"), Mw_init = Bry_.new_a7("xowa.js.mediaWiki.init();");
private static byte[] Css_url_day, Css_url_night;
private static final byte[]

View File

@@ -15,6 +15,7 @@ Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
*/
package gplx.xowa.htmls.heads; import gplx.*; import gplx.xowa.*; import gplx.xowa.htmls.*;
import gplx.core.bits.*;
import gplx.xowa.wikis.pages.*;
public class Xoh_head_mgr implements gplx.core.brys.Bfr_arg {
private Xoae_app app; private Xowe_wiki wiki; private Xoae_page page;
private Xoh_head_itm__base[] itms; private int itms_len;
@@ -59,7 +60,7 @@ public class Xoh_head_mgr implements gplx.core.brys.Bfr_arg {
this.app = app; this.wiki = wiki; this.page = page;
return this;
}
public Xoh_head_mgr Init_dflts() {
public Xoh_head_mgr Init_dflts(byte html_gen_tid) {
if (page.Wtxt().Toc().Enabled()) itm__toc.Enabled_y_();
if (wiki.Html_mgr().Head_mgr().Itm__top_icon().Enabled_y()) itm__top_icon.Enabled_y_();
if (wiki.Html_mgr().Head_mgr().Itm__title_rewrite().Enabled_y()) itm__title_rewrite.Enabled_y_();
@@ -70,9 +71,7 @@ public class Xoh_head_mgr implements gplx.core.brys.Bfr_arg {
itm__xo_elem.Enabled_y_();
itm__collapsible.Enabled_y_();
itm__navframe.Enabled_y_();
boolean popups_enabled
= !app.Mode().Tid_is_http() // do not enable if http_server, else js errors when calling xowa_exec; DATE:2016-06-22
&& wiki.Html_mgr().Head_mgr().Popup_mgr().Enabled(); // check user_cfg
boolean popups_enabled = html_gen_tid != Xopg_page_.Tid_edit && wiki.Html_mgr().Head_mgr().Popup_mgr().Enabled();
itm__popups.Enabled_(popups_enabled);
return this;
}
@@ -123,6 +122,7 @@ public class Xoh_head_mgr implements gplx.core.brys.Bfr_arg {
Xoh_head_itm__base itm = list__js_include.Get_at(i);
itm.Write_js_include(app, wiki, page, wtr);
}
int tail_script_len = list__js_tail_script.Len();
int window_onload_len = list__js_window_onload.Len();
if (tail_script_len + window_onload_len > 0) {

View File

@@ -19,6 +19,7 @@ import gplx.langs.jsons.*;
import gplx.xowa.htmls.js.*;
import gplx.xowa.guis.views.*;
import gplx.xowa.parsers.*;
import gplx.xowa.htmls.modules.popups.*;
public class Xoh_js_cbk implements Gfo_invk {
private Xoae_app app;
private Xog_html_itm html_itm;
@@ -67,19 +68,22 @@ public class Xoh_js_cbk implements Gfo_invk {
return String_.new_u8(page.Db().Text().Text_bry());
} catch (Exception e) {Err_.Noop(e); return null;}
}
private String Popups_get_async_bgn(GfoMsg m) {
try {
byte[] js_cbk = m.Args_getAt(0).Val_to_bry();
byte[] href_bry = m.Args_getAt(1).Val_to_bry();
return html_itm.Owner_tab().Wiki().Html_mgr().Head_mgr().Popup_mgr().Get_async_bgn(js_cbk, href_bry);
} catch (Exception e) {Err_.Noop(e); return null;}
}
private String Popups_get_html(GfoMsg m) {
try {
int popups_id = Int_.By_double(Double_.cast(m.Args_getAt(0).Val()));
byte[] href_bry = m.Args_getAt(1).Val_to_bry();
byte[] tooltip_bry = m.Args_getAt(2).Val_to_bry();
return html_itm.Owner_tab().Wiki().Html_mgr().Head_mgr().Popup_mgr().Show_init(popups_id, href_bry, tooltip_bry);
String mode = m.Args_getAt(0).Val_to_str_or_empty();
String popups_id = m.Args_getAt(1).Val_to_str_or_empty();
Xow_popup_mgr popup_mgr = html_itm.Owner_tab().Wiki().Html_mgr().Head_mgr().Popup_mgr();
if (String_.Eq(mode, "init")) {
byte[] href_bry = m.Args_getAt(2).Val_to_bry();
byte[] tooltip_bry = m.Args_getAt(3).Val_to_bry();
return popup_mgr.Show_init(popups_id, href_bry, tooltip_bry);
}
else if (String_.Eq(mode, "more"))
return popup_mgr.Show_more(popups_id);
else if (String_.Eq(mode, "all"))
popup_mgr.Show_all(popups_id);
return "";
} catch (Exception e) {Err_.Noop(e); return null;}
}
private String[] Get_title_meta(Xowe_wiki wiki, byte[] ttl_bry) {
@@ -175,7 +179,6 @@ public class Xoh_js_cbk implements Gfo_invk {
else if (ctx.Match(k, Invk_get_page)) return Get_page(m);
else if (ctx.MatchIn(k, Invk_cmd, Invk_scripts_exec)) return Scripts_exec(m);
else if (ctx.Match(k, Invk_scripts_exec)) return Scripts_exec(m);
else if (ctx.Match(k, Invk_popups_get_async_bgn)) return Popups_get_async_bgn(m);
else if (ctx.Match(k, Invk_popups_get_html)) return Popups_get_html(m);
else if (ctx.Match(k, Invk_get_search_suggestions)) return Get_search_suggestions(m);
else if (ctx.Match(k, Invk_get_titles_meta)) return Get_titles_meta(m);
@@ -190,7 +193,6 @@ public class Xoh_js_cbk implements Gfo_invk {
public static final String Invk_parse_to_html = "parse_to_html", Invk_wikidata_get_label = "wikidata_get_label", Invk_get_page = "get_page", Invk_cmd = "cmd", Invk_scripts_exec = "scripts_exec"
, Invk_get_search_suggestions = "get_search_suggestions", Invk_get_titles_meta = "get_titles_meta", Invk_get_titles_exists = "get_titles_exists", Invk_get_current_url = "get_current_url"
, Invk_xowa_exec_test = "xowa_exec_test", Invk_xowa_exec_test_as_array = "xowa_exec_test_as_array"
, Invk_popups_get_async_bgn = "popups_get_async_bgn"
, Invk_popups_get_html = "popups_get_html"
, Invk_exec_json = "exec_json"
, Invk_bldr_exec = "bldr_exec"

View File

@@ -26,6 +26,9 @@ public class Xow_popup_html_mkr {
public void Output_tidy_(boolean v) {output_tidy = v;} private boolean output_tidy = true;
public void Ctor(Xoae_app app, Xowe_wiki wiki) {
this.wiki = wiki; this.app = app;
this.fmtr_popup = app.Mode().Tid_is_http() ?
Bry_fmtr.new_(Web_html_fmtr_popup, Dflt_html_fmtr_popup_keys) :
Bry_fmtr.new_(Dflt_html_fmtr_popup, Dflt_html_fmtr_popup_keys);
wiki.Eval_mgr().Eval_mgr_(fmtr_popup, fmtr_viewed, fmtr_wiki, fmtr_next_sect);
}
public byte[] Bld(Xowe_wiki cur_wiki, Xoae_page page, Xow_popup_itm popup_itm, Bry_bfr wrdx_bfr) {
@@ -79,6 +82,24 @@ public class Xow_popup_html_mkr {
, Dflt_html_fmtr_viewed = Bry_.new_a7("\n <span class='data_key'>~{<>msgs.get('api-xowa.html.modules.popups.msgs.view_time-name');<>}</span><span class='data_val'>~{viewed_val}</span>")
, Dflt_html_fmtr_wiki = Bry_.new_a7("\n <span class='data_key'>~{<>msgs.get('api-xowa.html.modules.popups.msgs.wiki-name');<>}</span><span class='data_val'>~{wiki_val}</span>")
, Dflt_html_fmtr_next_sect = Bry_.new_a7("\n\n<span class='next_sect'>~{<>msgs.get('api-xowa.html.modules.popups.msgs.next_sect-name');<>}~{next_sect_val}</span>")
, Web_html_fmtr_popup = Bry_.new_a7(String_.Concat_lines_nl_skip_last
( "<div dir=~{page_lang_ltr}>"
, " <div>~{content}"
, " </div>"
, " <hr/>"
, " <div>"
, " <span class='data_val'><b>~{page_title}</b></span>~{wiki_item}"
, " <span class='data_key'>~{<>msgs.get('api-xowa.html.modules.popups.msgs.size-name');<>}</span><span class='data_val'>~{page_size}</span>"
, " <span class='data_key'>~{<>msgs.get('api-xowa.html.modules.popups.msgs.edited-name');<>}</span><span class='data_val'>~{edit_time}</span>~{view_time_item}"
, " </div>"
, " <hr/>"
, " <div style='float:bottom;'>"
, " <span><a href='/~{page_url}' title='~{<>msgs.get('api-xowa.gui.browser.url.exec-name');<>}' class='xowa-hover-off'><img src='~{xowa_root_dir}bin/any/xowa/file/app.menu/page/open.png'></a></span>"
, " <span><a href='/~{page_url}' title='~{<>msgs.get('api-xowa.gui.browser.tabs.new_link__at_dflt__focus_y-name');<>}' class='xowa-hover-off' target='_blank'><img src='~{xowa_root_dir}bin/any/xowa/file/app.menu/tabs/new.png'></a></span>"
// , " <span><a href='javascript:xowa.popups.FetchMore(\"~{popup_id}\");' title='~{<>msgs.get('api-xowa.html.modules.popups.show_more-tip');<>}'><img src='~{xowa_root_dir}bin/any/xowa/html/res/src/xowa/popups/imgs/show_more.png'></a></span>"
, " </div>"
, "</div>"
))
;
private static final String[]
Dflt_html_fmtr_popup_keys = String_.Ary("content", "page_lang_ltr", "page_url", "page_title", "popup_id", "wiki_item", "page_size", "edit_time", "view_time_item", "xowa_root_dir")

View File

@@ -15,8 +15,8 @@ Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
*/
package gplx.xowa.htmls.modules.popups; import gplx.*; import gplx.xowa.*; import gplx.xowa.htmls.*; import gplx.xowa.htmls.modules.*;
public class Xow_popup_itm implements Cancelable {
public Xow_popup_itm(int id, byte[] page_href, byte[] tooltip, int init_words_needed) {
this.popup_id = gplx.xowa.apps.Xoa_thread_.Key_page_popup + Int_.To_str(id);
public Xow_popup_itm(String popup_id, byte[] page_href, byte[] tooltip, int init_words_needed) {
this.popup_id = popup_id;
this.words_needed = init_words_needed;
this.page_href = page_href;
this.tooltip = tooltip;

View File

@@ -26,7 +26,7 @@ public class Xow_popup_mgr implements Gfo_invk, Gfo_evt_itm {
private Xoae_app app; private Xowe_wiki wiki; private Js_wtr js_wtr = new Js_wtr();
private int show_init_word_count, show_more_word_count;
private Xoa_url tmp_url = Xoa_url.blank();
private static final Object thread_lock = new Object(); private Xow_popup_itm async_itm; private Gfo_invk async_cmd_show; private int async_id_next = 1;
private static final Object thread_lock = new Object();
public Xow_popup_mgr(Xowe_wiki wiki) {
this.wiki = wiki; this.app = wiki.Appe();
ev_mgr = new Gfo_evt_mgr(this);
@@ -44,35 +44,27 @@ public class Xow_popup_mgr implements Gfo_invk, Gfo_evt_itm {
, Cfg__ns_allowed, Cfg__xnde_ignore_ids, Cfg__scan_len, Cfg__scan_max
);
}
public String Show_init(int id, byte[] href, byte[] tooltip) {
public String Show_init(String popup_id, byte[] href, byte[] tooltip) {
Xoae_page cur_page = Cur_page();
Xog_tab_itm tab = cur_page.Tab_data().Tab();
if (tab != null && tab.Tab_is_loading()) return ""; // NOTE: tab is null when previewing
Xow_popup_itm itm = new Xow_popup_itm(id, href, tooltip, show_init_word_count);
Xow_popup_itm itm = new Xow_popup_itm(popup_id, href, tooltip, show_init_word_count);
String rv = String_.new_u8(Get_popup_html(Cur_wiki(), cur_page, itm));
return tab != null && tab.Tab_is_loading() ? "" : rv;
}
public void Show_more(String popup_id) {
public String Show_more(String popup_id) {
Xoae_page cur_page = Cur_page();
Xow_popup_itm popup_itm = Itms_get_or_null(cur_page, popup_id).Mode_more_(show_more_word_count);
popup_itm.Popup_html_(Get_popup_html(Cur_wiki(), cur_page, popup_itm));
Show_popup_html(Cbk_xowa_popups_show_update, Mode_show_more, popup_itm);
byte[] html = Get_popup_html(Cur_wiki(), cur_page, popup_itm);
popup_itm.Popup_html_(html);
Show_popup_html(popup_itm, Cbk_update_popup_html, Mode_show_more);
return String_.new_u8(html);
}
public void Show_all(String popup_id) {
Xoae_page cur_page = Cur_page();
Xow_popup_itm popup_itm = Itms_get_or_null(cur_page, popup_id).Mode_all_();
popup_itm.Popup_html_(Get_popup_html(Cur_wiki(), cur_page, popup_itm));
Show_popup_html(Cbk_xowa_popups_show_update, Mode_show_all, popup_itm);
}
public String Get_async_bgn(byte[] js_cbk, byte[] href) {
if (Bry_.Has_at_bgn(href, gplx.xowa.parsers.lnkes.Xop_lnke_wkr.Bry_xowa_protocol)) return null; // ignore xowa-cmd
synchronized (thread_lock) {
if (async_itm != null) async_itm.Cancel();
async_itm = new Xow_popup_itm(++async_id_next, href, Bry_.Empty, show_init_word_count);
String id_str = async_itm.Popup_id();
Thread_adp_.Start_by_key(id_str, this, Invk_show_popup_async);
return id_str;
}
Show_popup_html(popup_itm, Cbk_update_popup_html, Mode_show_all);
}
public static boolean Running() {
boolean rv = false;
@@ -139,27 +131,18 @@ public class Xow_popup_mgr implements Gfo_invk, Gfo_evt_itm {
href = Bry_.Add(tooltip);
Xog_win_itm__prog_href_mgr.Hover(app, app.Gui_mgr().Browser_win().Cfg().Status__show_short_url(), cur_wiki, cur_page, String_.new_u8(href)); // set page ttl again in prog bar; DATE:2014-06-28
}
public void Show_popup_html(String cbk, byte[] mode, Xow_popup_itm popup_itm) {
private void Show_popup_html(Xow_popup_itm popup_itm, String cbk, byte[] mode) {
// build js cmd
js_wtr.Func_init(cbk);
js_wtr.Prm_str(popup_itm.Popup_id());
js_wtr.Prm_bry(mode);
js_wtr.Prm_bry(popup_itm.Popup_html());
js_wtr.Func_term();
String js_cmd = js_wtr.To_str_and_clear();
// send it
Xog_tab_itm cur_tab = app.Gui_mgr().Browser_win().Active_tab();
cur_tab.Html_box().Html_js_eval_script(Xow_popup_mgr_.Bld_js_cmd(js_wtr, cbk, mode, popup_itm.Page_href(), popup_itm.Popup_html()));
}
private void Show_popup_async() {
try {
synchronized (thread_lock) {
Xoae_page cur_page = app.Gui_mgr().Browser_win().Active_page();
async_itm.Popup_html_(Get_popup_html(app.Gui_mgr().Browser_win().Active_wiki(), cur_page, async_itm));
}
if (async_cmd_show == null)
async_cmd_show = app.Gui_mgr().Kit().New_cmd_sync(this);
Gfo_invk_.Invk_by_key(async_cmd_show, Invk_show_popup);
}
catch(Exception e) {
app.Usr_dlg().Warn_many("", "", "failed to get popup: href=~{0} err=~{1}", async_itm.Page_href(), Err_.Message_gplx_full(e));
}
}
private void Show_popup() {
if (async_itm.Canceled()) return;
Show_popup_html(Cbk_xowa_popups_show_create, Bry_.Empty, async_itm);
cur_tab.Html_box().Html_js_eval_script(js_cmd);
}
public void Ns_allowed_(byte[] raw) {
ns_allowed_regy.Clear();
@@ -195,10 +178,7 @@ public class Xow_popup_mgr implements Gfo_invk, Gfo_evt_itm {
private Xowe_wiki Cur_wiki() {return app.Gui_mgr().Browser_win().Active_tab().Wiki();}
private Xow_popup_itm Itms_get_or_null(Xoae_page page, String popup_id) {return (Xow_popup_itm)page.Popup_mgr().Itms().Get_by(popup_id);}
public Object Invk(GfsCtx ctx, int ikey, String k, GfoMsg m) {
if (ctx.Match(k, Invk_show_popup_async)) Show_popup_async();
else if (ctx.Match(k, Invk_show_popup)) Show_popup();
else if (ctx.Match(k, Cfg__enabled)) enabled = m.ReadYn("v");
if (ctx.Match(k, Cfg__enabled)) enabled = m.ReadYn("v");
else if (ctx.Match(k, Cfg__show_init_word_count)) show_init_word_count = m.ReadInt("v");
else if (ctx.Match(k, Cfg__show_more_word_count)) show_more_word_count = m.ReadInt("v");
else if (ctx.Match(k, Cfg__show_all_if_less_than)) parser.Cfg().Show_all_if_less_than_(m.ReadInt("v"));
@@ -214,10 +194,8 @@ public class Xow_popup_mgr implements Gfo_invk, Gfo_evt_itm {
else return Gfo_invk_.Rv_unhandled;
return this;
}
public static final String Invk_show_popup_async = "show_popup_async", Invk_show_popup = "show_popup";
private static final String
Cbk_xowa_popups_show_update = "xowa_popups_show_update"
, Cbk_xowa_popups_show_create = "xowa_popups_show_create"
Cbk_update_popup_html = "window.xowa.popups.UpdatePopupHtml"
;
private static final byte[]
Mode_show_more = Bry_.new_a7("more")

View File

@@ -16,16 +16,6 @@ Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
package gplx.xowa.htmls.modules.popups; import gplx.*; import gplx.xowa.*; import gplx.xowa.htmls.*; import gplx.xowa.htmls.modules.*;
import gplx.core.threads.*; import gplx.core.primitives.*; import gplx.core.js.*;
import gplx.xowa.wikis.nss.*; import gplx.xowa.specials.*;
class Xow_popup_mgr_ {
public static String Bld_js_cmd(Js_wtr js_wtr, String cbk, byte[] mode, byte[] href, byte[] html) {
js_wtr.Func_init(cbk);
js_wtr.Prm_bry(mode);
js_wtr.Prm_bry(href);
js_wtr.Prm_bry(html);
js_wtr.Func_term();
return js_wtr.To_str_and_clear();
}
}
class Load_popup_wkr implements Gfo_thread_wkr {
private Xow_popup_itm itm; private Xoae_page cur_page; private Xoa_url tmp_url;
private Hash_adp ns_allowed_regy;

View File

@@ -506,7 +506,7 @@ class Xop_popup_parser_fxt {
public void Test_parse(String raw, String ttl, String expd) {
Xoae_page page = Xoae_page.New_edit(wiki, Xoa_ttl.Parse(wiki, Bry_.new_a7(ttl)));
page.Db().Text().Text_bry_(Bry_.new_u8(raw));
Xow_popup_itm itm = new Xow_popup_itm(1, Bry_.new_u8(raw), Bry_.Empty, word_min);
Xow_popup_itm itm = new Xow_popup_itm("popup_1", Bry_.new_u8(raw), Bry_.Empty, word_min);
itm.Init(wiki.Domain_bry(), page.Ttl());
byte[] actl = parser.Parse(wiki, page, null, itm);
Tfds.Eq_str_lines(expd, String_.new_u8(actl));