From 766a39da8ba18b7b6bf214ed328bc129436737e7 Mon Sep 17 00:00:00 2001 From: Philip Chimento Date: Wed, 4 Sep 2013 16:07:56 -0700 Subject: Install WebHelper into GJS modules directory WebHelper is now importable using const WebHelper = imports.webhelper; [endlessm/eos-sdk#289] --- Makefile.am | 4 +- test/smoke-tests/WebHelper.js | 86 ------------------------------------------- test/smoke-tests/webview.js | 2 +- webhelper/Makefile.am.inc | 10 +++++ webhelper/webhelper.js | 86 +++++++++++++++++++++++++++++++++++++++++++ wikipedia/Makefile.am.inc | 4 +- 6 files changed, 102 insertions(+), 90 deletions(-) delete mode 100644 test/smoke-tests/WebHelper.js create mode 100644 webhelper/Makefile.am.inc create mode 100644 webhelper/webhelper.js diff --git a/Makefile.am b/Makefile.am index f7d41d7..80a6476 100644 --- a/Makefile.am +++ b/Makefile.am @@ -57,8 +57,10 @@ pkgconfigdir = $(libdir)/pkgconfig pkgconfig_DATA = @EOS_SDK_API_NAME@.pc DISTCLEANFILES += @EOS_SDK_API_NAME@.pc -# Open Endless Wikipedia SDK library +# SDK sublibraries +gjsmodulesdir = $(datadir)/gjs-1.0 include $(top_srcdir)/wikipedia/Makefile.am.inc +include $(top_srcdir)/webhelper/Makefile.am.inc # # # INTROSPECTION FILES # # # diff --git a/test/smoke-tests/WebHelper.js b/test/smoke-tests/WebHelper.js deleted file mode 100644 index 8b78931..0000000 --- a/test/smoke-tests/WebHelper.js +++ /dev/null @@ -1,86 +0,0 @@ -const Endless = imports.gi.Endless; -const GLib = imports.gi.GLib; -const Gtk = imports.gi.Gtk; -const WebKit = imports.gi.WebKit; - -const EOS_URI_SCHEME = 'endless://'; - - -const Application = new Lang.Class({ - Name: 'WebApplication', - Extends: Endless.Application, - -// Set of actions that may be invoked from a WebView. -// Declare them as function(dict), and use links with the format -// "endless://actionName?parameter=value" - - _webActions: { }, - -// This callback does the translation from URI to action -// this._webview.connect('navigation-policy-decision-requested', -// Lang.bind(this, this._webHelper.onNavigationRequested)); - - _onNavigationRequested : function(web_view, frame, request, - navigation_action, policy_decision, - user_data) { - let uri = request.get_uri(); - - if(uri.indexOf(EOS_URI_SCHEME) == 0) { - // get the name and parameters for the desired function - let f_call = uri.substring(EOS_URI_SCHEME.length, uri.length).split('?'); - var function_name = f_call[0]; - var parameters = {}; - - if(f_call[1]) { - // there are parameters - let params = f_call[1].split('&'); - params.forEach(function(entry) { - let param = entry.split('='); - - if(param.length == 2) { - param[0] = decodeURIComponent(param[0]); - param[1] = decodeURIComponent(param[1]); - // and now we add it... - parameters[param[0]] = param[1]; - } - }); - } - - if(this._webActions[function_name]) - Lang.bind(this, this._webActions[function_name])(parameters); - else - print('Unknown function '+function_name); - - policy_decision.ignore(); - return true; - } else { - // this is a regular URL, just navigate there - return false; - } - }, - -// convenience functions - - _getElementById: function(webview, id) { - // WebKit.DOMDocument - let dom = webview.get_dom_document(); - - // WebKit.DOMElement - return dom.get_element_by_id(id); - }, - - _translateHTML: function(webview, lang) { - let dom = webview.get_dom_document(); - - // WebKit.DOMNodeList - let translatable = dom.get_elements_by_name('translatable'); - - for (var i = 0 ; i < translatable.get_length() ; i++) { - // WebKit.DOMNode - let element = translatable.item(i); - - // TODO here is where we would do the translation - element.inner_html = '' + element.inner_text + ''; - } - } -}); diff --git a/test/smoke-tests/webview.js b/test/smoke-tests/webview.js index 9b84728..98226f3 100644 --- a/test/smoke-tests/webview.js +++ b/test/smoke-tests/webview.js @@ -7,7 +7,7 @@ const Gtk = imports.gi.Gtk; const WebKit = imports.gi.WebKit; // WebHelper.js must be copied somewhere in GJS's imports.searchPath -const WebHelper = imports.WebHelper; +const WebHelper = imports.webhelper; const TEST_APPLICATION_ID = 'com.endlessm.example.test-webview'; diff --git a/webhelper/Makefile.am.inc b/webhelper/Makefile.am.inc new file mode 100644 index 0000000..f7b26b0 --- /dev/null +++ b/webhelper/Makefile.am.inc @@ -0,0 +1,10 @@ +# Copyright 2013 Endless Mobile, Inc. + +# # # INSTALL RULES # # # + +webhelper_sources = webhelper/webhelper.js + +webhelperdir = $(gjsmodulesdir) +dist_webhelper_DATA = \ + $(webhelper_sources) \ + $(NULL) diff --git a/webhelper/webhelper.js b/webhelper/webhelper.js new file mode 100644 index 0000000..8b78931 --- /dev/null +++ b/webhelper/webhelper.js @@ -0,0 +1,86 @@ +const Endless = imports.gi.Endless; +const GLib = imports.gi.GLib; +const Gtk = imports.gi.Gtk; +const WebKit = imports.gi.WebKit; + +const EOS_URI_SCHEME = 'endless://'; + + +const Application = new Lang.Class({ + Name: 'WebApplication', + Extends: Endless.Application, + +// Set of actions that may be invoked from a WebView. +// Declare them as function(dict), and use links with the format +// "endless://actionName?parameter=value" + + _webActions: { }, + +// This callback does the translation from URI to action +// this._webview.connect('navigation-policy-decision-requested', +// Lang.bind(this, this._webHelper.onNavigationRequested)); + + _onNavigationRequested : function(web_view, frame, request, + navigation_action, policy_decision, + user_data) { + let uri = request.get_uri(); + + if(uri.indexOf(EOS_URI_SCHEME) == 0) { + // get the name and parameters for the desired function + let f_call = uri.substring(EOS_URI_SCHEME.length, uri.length).split('?'); + var function_name = f_call[0]; + var parameters = {}; + + if(f_call[1]) { + // there are parameters + let params = f_call[1].split('&'); + params.forEach(function(entry) { + let param = entry.split('='); + + if(param.length == 2) { + param[0] = decodeURIComponent(param[0]); + param[1] = decodeURIComponent(param[1]); + // and now we add it... + parameters[param[0]] = param[1]; + } + }); + } + + if(this._webActions[function_name]) + Lang.bind(this, this._webActions[function_name])(parameters); + else + print('Unknown function '+function_name); + + policy_decision.ignore(); + return true; + } else { + // this is a regular URL, just navigate there + return false; + } + }, + +// convenience functions + + _getElementById: function(webview, id) { + // WebKit.DOMDocument + let dom = webview.get_dom_document(); + + // WebKit.DOMElement + return dom.get_element_by_id(id); + }, + + _translateHTML: function(webview, lang) { + let dom = webview.get_dom_document(); + + // WebKit.DOMNodeList + let translatable = dom.get_elements_by_name('translatable'); + + for (var i = 0 ; i < translatable.get_length() ; i++) { + // WebKit.DOMNode + let element = translatable.item(i); + + // TODO here is where we would do the translation + element.inner_html = '' + element.inner_text + ''; + } + } +}); diff --git a/wikipedia/Makefile.am.inc b/wikipedia/Makefile.am.inc index 493b539..dd1c5bb 100644 --- a/wikipedia/Makefile.am.inc +++ b/wikipedia/Makefile.am.inc @@ -48,7 +48,7 @@ js_sources = \ wikipedia/WikipediaWebView.js \ $(NULL) -gjsmodulesdir = $(datadir)/gjs-1.0 -nobase_dist_gjsmodules_DATA = \ +wikipediadir = $(gjsmodulesdir) +nobase_dist_wikipedia_DATA = \ $(js_sources) \ $(NULL) -- cgit v1.2.3