Created
July 25, 2017 17:39
-
-
Save simkessy/6de5099bf477b5eb06d678e5f2a22650 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* HillbillyTabs.2013 - Place SharePoint 2013 Web Parts in Tabs | |
* Version 3.0 | |
* @requires jQuery v1.7 or greater | |
* @requires jQueryUI v1.11 or greater | |
* @requires jQuery.cookie | |
* | |
* Copyright (c) 2013-2015 Mark Rackley | |
* Licensed under the MIT license: | |
* http://www.opensource.org/licenses/mit-license.php | |
*/ | |
/** | |
* @description Places SharePoint WebPart into jQuery UI Tabs | |
* @type jQuery | |
* @name HillbillyTabs.2013 | |
* @category Plugins/HillbillyTabs | |
* @author Mark Rackley / http://www.markrackley.net / [email protected] | |
*/ | |
var hillbillyTabs = { | |
tabClass: "hillbilly-tab", | |
wpTitles: [], | |
getSite: function () { | |
return _spPageContextInfo.webServerRelativeUrl.split("/").pop() | |
}, | |
isNil: function (n) { | |
return null == n | |
}, | |
build: function (webPartTitles) { | |
hillbillyTabs.wpTitles = webPartTitles; | |
var CEWPID = ""; | |
var tabDivID = ""; | |
var ulID = ""; | |
$("#tabsContainer").closest("[id^='MSOZoneCell_WebPart']").find("span[id^='WebPartCaptionWPQ']").each(function () { | |
CEWPID = $(this).attr("id"); | |
}); | |
if (CEWPID == "") { | |
CEWPID = $("#tabsContainer").closest("[id^='MSOZoneCell_WebPart']").attr("id"); | |
} | |
tabDivID = CEWPID + "TabsDiv"; | |
ulID = CEWPID + "Tabs"; | |
$("#tabsContainer").attr("id", tabDivID).append("<ul id='" + ulID + "'></ul>"); | |
// If no web part has been specified | |
if (webPartTitles == undefined) { | |
var index = 0; | |
$("#" + tabDivID).closest("div.ms-webpart-zone, div.ms-rte-layoutszone-inner") | |
.find("h2.ms-webpart-titleText") | |
.each(function () { | |
if ($(this).find("span[id^='WebPartCaptionWPQ']").attr("id") != CEWPID) { | |
var title = $(this).text(); | |
$("#" + ulID).append('<li><a href="#Tab' + index + CEWPID + '" id="TabHead' + index + CEWPID + '" onclick="hillbillyTabs.click(this.id);">' + | |
title + '</a></li>').after('<div id="Tab' + index + CEWPID + '"></div>'); | |
var webPart = $(this).closest("[id^='MSOZoneCell_WebPart']"); | |
$("#Tab" + index + CEWPID).append((webPart)); | |
index++; | |
} | |
}); | |
} else { | |
// If webpart titles are specified | |
for (index in webPartTitles) { | |
var title = webPartTitles[index]; | |
var tabContent = title.split(";#"); | |
// Check if web part title name overrides are used | |
if (tabContent.length > 1) { | |
// Create the tab links | |
var webpartID = $("h2.ms-webpart-titleText").find("span:contains(" + title + ")").closest("div[id^='MSOZoneCell_WebPartWPQ']").attr("id"); | |
// Build on-click event to switch tabs | |
var buildCall = "hillbillyTabs.click(\"" + webpartID + "\", \"" + title.split("'")[0] + "\")"; | |
// Add tab to container | |
$("#" + ulID).append("<li><a href='#Tab" + index + CEWPID + "' id='TabHead" + index + CEWPID + "' onclick='" + buildCall + "'>" + | |
tabContent[0] + "</a></li>").after("<div id='Tab" + index + CEWPID + "'></div>"); | |
for (i = 1; i < tabContent.length; i++) { | |
$("h2.ms-webpart-titleText").each(function () { | |
$(this).find("span:contains('" + tabContent[i] + "')").each(function () { | |
if ($(this).text() == tabContent[i]) { | |
// Hide webpart | |
$(this).closest("div").hide(); | |
var webPart = $(this).closest("span").closest("[id^='MSOZoneCell_WebPart']").addClass(hillbillyTabs.tabClass).css("margin-left", 10).hide() | |
} | |
}); | |
}); | |
} | |
} else { | |
$("h2.ms-webpart-titleText").each(function () { | |
$(this).find('span:contains("' + title + '")').each(function () { | |
if ($(this).text() == title) { | |
// Create the tab links | |
var webpartID = $("h2.ms-webpart-titleText").find("span:contains(" + title + ")").closest("div[id^='MSOZoneCell_WebPart']").attr("id"); | |
// Build on-click event to switch tabs | |
var buildCall = "hillbillyTabs.click(\"" + webpartID + "\", \"" + title.split("'")[0] + "\")"; | |
// Add tab to container | |
$("#" + ulID).append("<li><a href='#Tab" + index + CEWPID + "' id='TabHead" + index + CEWPID + "' onclick='" + buildCall + "'>" + | |
title + "</a></li>").after("<div id='Tab" + index + CEWPID + "'></div>"); | |
// Hide webpart | |
$(this).closest("div").hide(); | |
var webPart = $(this).closest("span").closest("[id^='MSOZoneCell_WebPart']").addClass(hillbillyTabs.tabClass).css("margin-left", 10).hide() | |
} | |
}); | |
}); | |
} | |
} | |
} | |
// Hide webparts which have errors | |
hillbillyTabs.hideErrorParts(); | |
// jqueryUI initiate tabs | |
$("#" + tabDivID).tabs(); | |
// Set cookie | |
hillbillyTabs.setCookies() | |
// Show tab | |
hillbillyTabs.showActiveTab(); | |
// Set event handler | |
hillbillyTabs.setEventHandlers() | |
}, | |
hideErrorParts: function () { | |
$("span[id^='WebPartCaptionWPQ']").each(function () { | |
$(this).prev("span:contains('Error')").each(function () { | |
var webPart = $(this).closest("span").closest("[id^='MSOZoneCell_WebPart']").hide(); | |
}); | |
}); | |
}, | |
showActiveTab: function () { | |
// Get Cookies | |
var lastActiveTab = $.cookie("lastActiveTab" + hillbillyTabs.getSite()); | |
var lastActiveTabTitle = $.cookie("lastActiveTabTitle" + hillbillyTabs.getSite()); | |
// Show last active tab | |
$("a.ui-tabs-anchor:contains('" + lastActiveTabTitle + "')").click(); | |
// Display tabs | |
$("#contentBox").fadeIn("slow"); | |
}, | |
click: function (id, title) { | |
// Hide all tabs | |
$.map(hillbillyTabs.wpTitles, function (title) { | |
$("h2.ms-webpart-titleText") | |
.find('span:contains("' + title + '")') | |
.closest("span").closest("[id^='MSOZoneCell_WebPart']").hide(); | |
}) | |
// Set cookies | |
$.cookie("lastActiveTab" + hillbillyTabs.getSite(), id) | |
$.cookie("lastActiveTabTitle" + hillbillyTabs.getSite(), title.split("'")[0]) | |
// Show clicked tab | |
$("div#" + id).show() | |
}, | |
// If first load, set cookie to first webpart | |
setCookies: function () { | |
// If first load and there's no webpart set in cookie | |
var noCookieSet = hillbillyTabs.isNil($.cookie("lastActiveTab" + hillbillyTabs.getSite())) | |
if (noCookieSet) { | |
// get first webpart on page | |
var firstWebPart = $("div." + hillbillyTabs.tabClass).first().attr("id") | |
var firstWPTitle = $("#" + $.cookie("lastActiveTab" + hillbillyTabs.getSite())).find("nobr").text() | |
// set cookie to first webpart id | |
$.cookie("lastActiveTab" + hillbillyTabs.getSite(), firstWebPart) | |
$.cookie("lastActiveTabTitle" + hillbillyTabs.getSite(), firstWPTitle) | |
} | |
}, | |
setEventHandlers: function () { | |
// Set focus event | |
$(".ui-tabs-tab").on("focus", function (e) { | |
$(this).find("a").click() | |
}) | |
}, | |
init: function () { | |
$("#contentBox").hide(); | |
} | |
} | |
hillbillyTabs.init() | |
// Set HillbillyTabs (old) to new function so that anyone using this won't have to change anythnig | |
HillbillyTabs = hillbillyTabs.build; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment