Commit 128fe356 authored by Aaron Harder's avatar Aaron Harder

Framed page: Support passing hash portion of URL from address bar to framed page, and back.

parent 139dd891
/* globals module, require, Materialize, BL, _ */
/* globals window, module, require, Materialize, BL, _ */
var slingConnector=require("./sling-client-connector.js");
var editPageDialogs=require("./page-dialogs");
......@@ -7,7 +7,7 @@ module.exports=function($, window){
var sling=slingConnector({$:$, _:_, page: window.BL.page});
var tree=require("./tree.js")($, window, {hasPageListingCapability: window.BL.config.hasPageListingCapability, supressSlingProxy: window.BL.config.treeIgnoreProxy, treeIcon: window.BL.config.treeIcon, languageSupport: true});
var view={};
var $modeButtons, modeButtons={}, currentMode="view", currentPath;
var $modeButtons, modeButtons={}, currentMode="view", currentPath, currentExtraHashSegments="";
var $iframe, $loadingIndicator;
var dialog = window.BL.editDialog;
var $document=$(window.document);
......@@ -110,6 +110,22 @@ module.exports=function($, window){
});
settingsButton.modalButton($.extend({},dialog.getStandardEditDialogOptions(), {beforeClose: function(){allowDialogOpen = false; return true;}}));
(function watchForIframeURLChanges(){
var last=$iframe[0].contentWindow.location.href, modes=global.BL.config.modes;
// TODO: Polling is lame, but iframe URL change events are not readily available
window.setInterval(function(){
var currentIframeURL = $iframe[0].contentWindow.location.href;
if(last!==currentIframeURL){
last=currentIframeURL;
var result = view.parseUrlByMode(currentIframeURL);
view.brieflyIgnoreHashEvents();
view.path(result.path);
view.mode(result.mode || view.mode());
view.applyPathAndMode({skipIframeLoad:true});
}
}, 2000);
})();
$iframe.on("load", function(){
try{
......@@ -203,7 +219,13 @@ module.exports=function($, window){
/****************************************************/
view.parseUrlByMode=function(url){
view.parseUrlByMode=function(href){
var incomingLocation = window.document.createElement("a");
incomingLocation.href = href;
var incomingPath = incomingLocation.pathname;
currentExtraHashSegments = (incomingLocation.hash || "").replace(/^\#\!?/,"");
if(!viewModeRegexes){
viewModeRegexes = _.mapValues(viewModes, (mode)=>{
var reg=new RegExp("^" + regexize(mode.prefix) + "(.*)" + regexize(mode.postfix) + "$" );
......@@ -212,10 +234,10 @@ module.exports=function($, window){
});
}
var result={path:url};
var result={path:incomingPath};
_.each(viewModeRegexes, (rule, mode)=>{
var match=url.match(rule.reg);
var match=incomingPath.match(rule.reg);
if(match){
if(!result.score || rule.score>result.score){
result={path:match[1], mode, score:rule.score};
......@@ -247,11 +269,6 @@ module.exports=function($, window){
}
/****************************************************/
view.setHash=function(){
view.brieflyIgnoreHashEvents();
window.document.location.hash="!" + currentPath + "." + currentMode;
};
/****************************************************/
view.path=function(path, dontSetHash){
......@@ -312,7 +329,7 @@ module.exports=function($, window){
if(currentPath === defaultFrameURL){
url=currentPath;
}else{
url = (mode.prefix?mode.prefix:"") + currentPath + (mode.postfix?mode.postfix:"");
url = (mode.prefix?mode.prefix:"") + currentPath + (mode.postfix?mode.postfix:"") + (currentExtraHashSegments?("#!" + currentExtraHashSegments):"");
}
}else{
url=currentPath;
......@@ -321,11 +338,14 @@ module.exports=function($, window){
url=url?url.replace(/\/\//g,"/"):url;
if(!options.skipIframeLoad){
var currentUrl=$iframe[0].contentWindow.location.pathname;
if(currentUrl!==url){
var currentIframeUrl=$iframe[0].contentWindow.location.pathname;
if(currentIframeUrl!==url){
view.brieflyIgnoreFrameLoadEvents();
$iframe.attr("src", url);
$loadingIndicator.css("display", "block");
var parts=url.split("#");
if(parts[0]!==currentIframeUrl){
$loadingIndicator.css("display", "block");
}
}
}
......@@ -381,10 +401,21 @@ module.exports=function($, window){
view.applyPathAndMode(); //skip reload of iframe if not a real click
}
/****************************************************/
view.setHash=function(){
view.brieflyIgnoreHashEvents();
window.document.location.hash="!" + currentPath + "." + currentMode + (currentExtraHashSegments?("!" + currentExtraHashSegments):"");
};
/****************************************************/
view.getHash=function(){
var hash=window.document.location.hash.replace(/\.html/g,"");
var parts=hash.match(/^\#\!(.*?)((\.[^\/^\.]*)?)$/,"");
var fullHash=window.document.location.hash.replace(/^\#\!?|\.html/g,"");
var hashSegments=fullHash.split("!");
var hash = hashSegments.shift();
currentExtraHashSegments = hashSegments.join("!") || "";
var parts=hash.match(/^(.*?)((\.[^\/^\.]*)?)$/,"");
if(parts){
view.path(parts[1], "dontSetHash");
view.mode(parts[2].slice(1) || defaultFrameMode, "dontSetHash");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment