From b2a685756150129fab22dc418e1630944ac41bf4 Mon Sep 17 00:00:00 2001 From: Ryan Cramer Date: Wed, 2 Nov 2016 10:35:39 -0400 Subject: [PATCH] Update WireTabs module to support automatic tab selection by querystring (i.e. url?WireTab=tab_id). This is in addition to the existing hash url#tab_id option, which seems to have issues in certain cases like that reported in processwire/processwire-issues#71 --- .../Jquery/JqueryWireTabs/JqueryWireTabs.js | 31 ++++++++++++++----- .../JqueryWireTabs/JqueryWireTabs.min.js | 2 +- .../ProcessTemplate/ProcessTemplate.module | 4 +-- 3 files changed, 26 insertions(+), 11 deletions(-) diff --git a/wire/modules/Jquery/JqueryWireTabs/JqueryWireTabs.js b/wire/modules/Jquery/JqueryWireTabs/JqueryWireTabs.js index 5cbf2ac3..53521fa3 100644 --- a/wire/modules/Jquery/JqueryWireTabs/JqueryWireTabs.js +++ b/wire/modules/Jquery/JqueryWireTabs/JqueryWireTabs.js @@ -61,16 +61,31 @@ }); } - var hash = document.location.hash.replace("#",""); // thanks to @da-fecto - if(hash.length) { - $rememberTab = $tabList.find("a#_" + hash); - if($rememberTab.length == 0) { - $rememberTab = null; - } else { - document.location.hash = ''; + var href = window.location.href; + var hrefMatch = ''; + if(href.indexOf('WireTab')) { + var regex = new RegExp('[&;?]WireTab=([-_a-z0-9]+)', 'i'); + hrefMatch = href.match(regex); + hrefMatch = hrefMatch ? hrefMatch[1] : ''; + if(hrefMatch.length) { + $rememberTab = $tabList.find("a#_" + hrefMatch); } } - if($rememberTab == null && cookieTab.length > 0 && options.rememberTabs > -1) $rememberTab = $tabList.find("a#_" + cookieTab); + + if($rememberTab == null) { + var hash = document.location.hash.replace("#", ""); // thanks to @da-fecto + if(hash.length) { + $rememberTab = $tabList.find("a#_" + hash); + if($rememberTab.length == 0) { + $rememberTab = null; + } else { + document.location.hash = ''; + } + } + } + if($rememberTab == null && cookieTab.length > 0 && options.rememberTabs > -1) { + $rememberTab = $tabList.find("a#_" + cookieTab); + } if($rememberTab && $rememberTab.length > 0) { $rememberTab.click(); if (options.rememberTabs == 0) setTabCookie(''); // don't clear cookie when rememberTabs=1, so it continues diff --git a/wire/modules/Jquery/JqueryWireTabs/JqueryWireTabs.min.js b/wire/modules/Jquery/JqueryWireTabs/JqueryWireTabs.min.js index 60bbe435..a6321fd3 100644 --- a/wire/modules/Jquery/JqueryWireTabs/JqueryWireTabs.min.js +++ b/wire/modules/Jquery/JqueryWireTabs/JqueryWireTabs.min.js @@ -1 +1 @@ -(function(a){a.fn.WireTabs=function(c){var d={rememberTabs:0,cookieName:"WireTabs",items:null,skipRememberTabIDs:[],itemsParent:null,id:""};if(ProcessWire.config.JqueryWireTabs.rememberTabs!="undefined"){d.rememberTabs=ProcessWire.config.JqueryWireTabs.rememberTabs}var b=0;a.extend(d,c);return this.each(function(i){var k=null;var e=a(this);var h="";var m=true;function n(){if(!d.items){return}if(d.items.length<1){return}if(d.id.length){k=a("#"+d.id);if(k.length){m=false}else{k=null}}if(!k){k=a("").addClass("WireTabs nav");if(d.id.length){k.attr("id",d.id)}}d.items.each(f);if(m){e.prepend(k)}var o=e;var p=null;var r=l();if(d.rememberTabs==0){o.submit(function(){g(h);return true})}var q=document.location.hash.replace("#","");if(q.length){p=k.find("a#_"+q);if(p.length==0){p=null}else{document.location.hash=""}}if(p==null&&r.length>0&&d.rememberTabs>-1){p=k.find("a#_"+r)}if(p&&p.length>0){p.click();if(d.rememberTabs==0){g("")}setTimeout(function(){p.click()},200)}else{k.children("li:first").children("a").click()}}function f(){b++;var s=a(this);if(!s.attr("id")){s.attr("id","WireTab"+b)}var r=s.attr("title")||s.attr("id");s.removeAttr("title");var o=s.attr("id");var q=a("a#_"+o);if(q.length>0){q.click(j)}else{var q=a("").attr("href","#"+o).attr("id","_"+o).html(r).click(j);k.append(a("
  • ").append(q))}var p=s.attr("data-tooltip");if(s.hasClass("WireTabTip")||p){q.addClass("tooltip");q.attr("title",p?p:r)}s.hide()}function j(){var o=a(k.find("a.on").removeClass("on").attr("href")).hide();var p=a(a(this).addClass("on").attr("href")).show();var q=p.attr("id");var r=o.attr("id");if(r){e.removeClass(o.attr("id"))}e.addClass(q);if(d.rememberTabs>-1){if(jQuery.inArray(q,d.skipRememberTabIDs)!=-1){q=""}if(d.rememberTabs==1){g(q)}h=q}a(document).trigger("wiretabclick",[p,o]);return false}function g(o){document.cookie=d.cookieName+"="+escape(o)}function l(){var p=new RegExp("(?:^|;)\\s?"+d.cookieName+"=(.*?)(?:;|$)","i");var o=document.cookie.match(p);o=o?o[1]:"";return o}n()})}})(jQuery); \ No newline at end of file +(function(a){a.fn.WireTabs=function(c){var d={rememberTabs:0,cookieName:"WireTabs",items:null,skipRememberTabIDs:[],itemsParent:null,id:""};if(ProcessWire.config.JqueryWireTabs.rememberTabs!="undefined"){d.rememberTabs=ProcessWire.config.JqueryWireTabs.rememberTabs}var b=0;a.extend(d,c);return this.each(function(i){var k=null;var e=a(this);var h="";var m=true;function n(){if(!d.items){return}if(d.items.length<1){return}if(d.id.length){k=a("#"+d.id);if(k.length){m=false}else{k=null}}if(!k){k=a("").addClass("WireTabs nav");if(d.id.length){k.attr("id",d.id)}}d.items.each(f);if(m){e.prepend(k)}var p=e;var s=null;var u=l();if(d.rememberTabs==0){p.submit(function(){g(h);return true})}var o=window.location.href;var q="";if(o.indexOf("WireTab")){var r=new RegExp("[&;?]WireTab=([-_a-z0-9]+)","i");q=o.match(r);q=q?q[1]:"";if(q.length){s=k.find("a#_"+q)}}if(s==null){var t=document.location.hash.replace("#","");if(t.length){s=k.find("a#_"+t);if(s.length==0){s=null}else{document.location.hash=""}}}if(s==null&&u.length>0&&d.rememberTabs>-1){s=k.find("a#_"+u)}if(s&&s.length>0){s.click();if(d.rememberTabs==0){g("")}setTimeout(function(){s.click()},200)}else{k.children("li:first").children("a").click()}}function f(){b++;var s=a(this);if(!s.attr("id")){s.attr("id","WireTab"+b)}var r=s.attr("title")||s.attr("id");s.removeAttr("title");var o=s.attr("id");var q=a("a#_"+o);if(q.length>0){q.click(j)}else{var q=a("").attr("href","#"+o).attr("id","_"+o).html(r).click(j);k.append(a("
  • ").append(q))}var p=s.attr("data-tooltip");if(s.hasClass("WireTabTip")||p){q.addClass("tooltip");q.attr("title",p?p:r)}s.hide()}function j(){var o=a(k.find("a.on").removeClass("on").attr("href")).hide();var p=a(a(this).addClass("on").attr("href")).show();var q=p.attr("id");var r=o.attr("id");if(r){e.removeClass(o.attr("id"))}e.addClass(q);if(d.rememberTabs>-1){if(jQuery.inArray(q,d.skipRememberTabIDs)!=-1){q=""}if(d.rememberTabs==1){g(q)}h=q}a(document).trigger("wiretabclick",[p,o]);return false}function g(o){document.cookie=d.cookieName+"="+escape(o)}function l(){var p=new RegExp("(?:^|;)\\s?"+d.cookieName+"=(.*?)(?:;|$)","i");var o=document.cookie.match(p);o=o?o[1]:"";return o}n()})}})(jQuery); \ No newline at end of file diff --git a/wire/modules/Process/ProcessTemplate/ProcessTemplate.module b/wire/modules/Process/ProcessTemplate/ProcessTemplate.module index 59f8e807..8b59376a 100644 --- a/wire/modules/Process/ProcessTemplate/ProcessTemplate.module +++ b/wire/modules/Process/ProcessTemplate/ProcessTemplate.module @@ -1480,7 +1480,7 @@ class ProcessTemplate extends Process { if($hasSuccess) $notes[] = sprintf($this->_('Templates indicated with a %s are already configured to allow this template as a parent.'), $successIndicator); $field->notes = implode("\n", $notes); $field->showIf = 'noChildren!=1'; - $field->setAsmSelectOption('editLink', "./edit?id={value}&modal=1#tab_family"); + $field->setAsmSelectOption('editLink', "./edit?id={value}&modal=1&WireTab=tab_family"); $field->setAsmSelectOption('editLinkOnlySelected', false); $form->add($field); @@ -1519,7 +1519,7 @@ class ProcessTemplate extends Process { if($hasSuccess) $notes[] = sprintf($this->_('Templates indicated with a %s are already configured to allow this template as a child.'), $successIndicator); $field->notes = implode("\n", $notes); $field->showIf = 'noParents!=1'; - $field->setAsmSelectOption('editLink', "./edit?id={value}&modal=1#tab_family"); + $field->setAsmSelectOption('editLink', "./edit?id={value}&modal=1&WireTab=tab_family"); $field->setAsmSelectOption('editLinkOnlySelected', false); $form->add($field);