X-Git-Url: http://sru.miketaylor.org.uk/?a=blobdiff_plain;f=src%2Fmkws-core.js;h=d8ca560b6a6abd7cf4b0f28cddef377cae2fa899;hb=9044b6413fbc87b25dded1f1ecd32f4b6b971a77;hp=941cab8ad1216af876fbb44e348bb149b4cb38c5;hpb=6d1ed1311f829d8f61f3bfc0ad7327f129c87414;p=mkws-moved-to-github.git diff --git a/src/mkws-core.js b/src/mkws-core.js index 941cab8..d8ca560 100644 --- a/src/mkws-core.js +++ b/src/mkws-core.js @@ -109,20 +109,31 @@ mkws.log = function(string) { }; -// Incredible that the standard JavaScript runtime doesn't define a -// unique windowId. Instead, we have to make one up. And since there's -// no global area shared between windows, the best we can do for -// ensuring uniqueness is generating a random ID and crossing our -// fingers. We stash this in window.name, as it's the only place to -// keep data that is preserved across reloads and within-site -// navigation. pz2.js picks this up and uses it as part of the -// cookie-name, to ensure each tab gets its own session. +// We put a session ID in window.name, as it's the only place to keep +// data that is preserved across reloads and within-site navigation. +// pz2.js picks this up and uses it as part of the cookie-name, to +// ensure we get a new session when we need one. +// +// We want to use different sessions for different windows/tabs (so +// they don't receive each other's messages), different hosts and +// different paths on a host (since in general these will +// authenticate as different libraries). So the window name needs to +// include a session identifier, the hostname and the path from the +// URL. +// if (window.name) { mkws.log("Using existing window.name '" + window.name + "'"); } else { + // Incredible that the standard JavaScript runtime doesn't define a + // unique windowId. Instead, we have to make one up. And since there's + // no global area shared between windows, the best we can do for + // ensuring uniqueness is generating a random ID and crossing our + // fingers. + // // Ten chars from 26 alpha-numerics = 36^10 = 3.65e15 combinations. // At one per second, it will take 116 million years to duplicate a session - window.name = Math.random().toString(36).slice(2, 12); + var session = Math.random().toString(36).slice(2, 12); + window.name = window.location.hostname + window.location.pathname + '/' + session; mkws.log("Generated new window.name '" + window.name + "'"); } @@ -168,8 +179,13 @@ mkws.setMkwsConfig = function(overrides) { var config_default = { use_service_proxy: true, - pazpar2_url: "//mkws.indexdata.com/service-proxy/", - service_proxy_auth: "//mkws.indexdata.com/service-proxy-auth", + pazpar2_url: undefined, + pp2_hostname: "sp-mkws.indexdata.com", + pp2_path: "service-proxy", + service_proxy_auth: undefined, + sp_auth_path: "service-proxy/", + sp_auth_query: "command=auth&action=perconfig", + sp_auth_credentials: "XXX/XXX", // Should be undefined: see bug MKSP-125. lang: "", sort_options: [["relevance"], ["title:1", "title"], ["date:0", "newest"], ["date:1", "oldest"]], perpage_options: [10, 20, 30, 50], @@ -252,6 +268,18 @@ mkws.pagerNext = function(tname) { }; +mkws.pazpar2_url = function() { + if (mkws.config.pazpar2_url) { + mkws.log("using pre-baked pazpar2_url '" + mkws.config.pazpar2_url + "'"); + return mkws.config.pazpar2_url; + } else { + var s = document.location.protocol + "//" + mkws.config.pp2_hostname + "/" + mkws.config.pp2_path + "/"; + mkws.log("generated pazpar2_url '" + s + "'"); + return s; + } +}; + + // wrapper to provide local copy of the jQuery object. (function($) { var log = mkws.log; @@ -528,9 +556,9 @@ mkws.pagerNext = function(tname) { } // protocol independent link for pazpar2: "//mkws/sp" -> "https://mkws/sp" - if (mkws.config.pazpar2_url.match(/^\/\//)) { + if (mkws.pazpar2_url().match(/^\/\//)) { mkws.config.pazpar2_url = document.location.protocol + mkws.config.pazpar2_url; - log("adjusted protocol independent link to " + mkws.config.pazpar2_url); + log("adjusted protocol independent link to " + mkws.pazpar2_url()); } if (mkws.config.responsive_design_width) { @@ -562,10 +590,32 @@ mkws.pagerNext = function(tname) { } */ + function sp_auth_url(config) { + if (config.service_proxy_auth) { + mkws.log("using pre-baked sp_auth_url '" + config.service_proxy_auth + "'"); + return config.service_proxy_auth; + } else { + var s = '//'; + s += config.auth_hostname ? config.auth_hostname : config.pp2_hostname; + s += '/' + config.sp_auth_path; + var q = config.sp_auth_query; + if (q) { + s += '?' + q; + } + var c = config.sp_auth_credentials; + if (c) { + s += ('&username=' + c.substr(0, c.indexOf('/')) + + '&password=' + c.substr(c.indexOf('/')+1)); + } + mkws.log("generated sp_auth_url '" + s + "'"); + return s; + } + } + if (mkws.config.use_service_proxy && !mkws.authenticated && !mkws.authenticating) { - authenticateSession(mkws.config.service_proxy_auth, + authenticateSession(sp_auth_url(mkws.config), mkws.config.service_proxy_auth_domain, - mkws.config.pazpar2_url); + mkws.pazpar2_url()); } else if (!mkws.authenticating) { // raw pp2 or we have a session already open runAutoSearches();