Last active
August 29, 2015 14:22
-
-
Save s-osa/6a4da69f3c28928acbe9 to your computer and use it in GitHub Desktop.
This file contains 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
(function() { | |
// Config | |
var weeks = 52; | |
var beginningOfWeek = 1; // 0:Sun, 1:Mon, 2:Tue, 3:Wed, 4:Thu, 5:Fri, 6:Sat | |
// Functions | |
function formatDate(date) { | |
return [date.getFullYear().toString(), ('0' + (date.getMonth() + 1)).slice(-2), ('0' + date.getDate()).slice(-2)].join("-"); | |
} | |
function getCurrentSearchQuery() { | |
return $("#js-issues-search")[0].value.trim().split(" ").map(function(elm){ return elm.split(":") }); | |
} | |
function buildSearchQuery(query) { | |
return query.map(function(elm){ return encodeURIComponent(elm.join(":")) }).join("+"); | |
} | |
function addSearchQuery(query, type, dateRange) { | |
// Remove existing week query | |
for(i = query.length - 1 ; i >= 0 ; i--){ | |
if(query[i][0] == type) query.splice(i,1); | |
} | |
// Remove is:open query if closed filter applied | |
if (type == "closed") { | |
for(i = query.length - 1 ; i >= 0 ; i--){ | |
if(query[i][1] == "open") query.splice(i,1); | |
} | |
} | |
if (dateRange) query.push([type, dateRange]); | |
return query; | |
} | |
function getLatestBeginningDateOfWeek(dayOfWeek, date) { | |
var d = date || new Date(); | |
d.setDate(d.getDate() - (d.getDay() - dayOfWeek)); | |
return d; | |
} | |
function dateToWeek(date) { | |
var first = new Date(date.getTime()); | |
var last = new Date(date.getTime()); | |
last.setDate(last.getDate() + 6); | |
return formatDate(first) + ".." + formatDate(last); | |
} | |
function buildPath(newQuery) { | |
var currentQuery = window.location.search.substr(1).split("&").map(function(e){ return e.split("=") }); | |
var queryObject = {}; | |
for(i = 0 ; i < currentQuery.length ; i++) { | |
queryObject[currentQuery[i][0]] = currentQuery[i][1] | |
} | |
queryObject["q"] = buildSearchQuery(newQuery); | |
return window.location.pathname + "?" + Object.keys(queryObject).map(function(key){ return [key, queryObject[key]].join("=") }).join("&"); | |
} | |
function buildFilterElementList(dates, type) { | |
return "<div class=\"select-menu-list\" role=\"menu\"><div>" + dates.map(function(date){ return buildFilterElement(date, type) }).join("") + "</div></div>"; | |
} | |
function buildFilterElement(date, type) { | |
var currentQuery = getCurrentSearchQuery(); | |
var week = dateToWeek(date) | |
var isCurrentQuery = false; | |
for (i = 0 ; i < currentQuery.length ; i++) { | |
if (currentQuery[i][0] == type && currentQuery[i][1] == week) isCurrentQuery = true; | |
} | |
if (isCurrentQuery) { | |
return "<a href=\"" + buildPath(addSearchQuery(currentQuery, type, null)) + "\" class=\"select-menu-item js-navigation-item js-navigation-open selected \" role=\"menuitem\"><span class=\"select-menu-item-icon octicon octicon-check\"></span><div class=\"select-menu-item-text\">" + week+ "</div></a>"; | |
} else { | |
return "<a href=\"" + buildPath(addSearchQuery(currentQuery, type, week)) + "\" class=\"select-menu-item js-navigation-item js-navigation-open \" role=\"menuitem\"><span class=\"select-menu-item-icon octicon octicon-check\"></span><div class=\"select-menu-item-text\">" + week+ "</div></a>"; | |
} | |
} | |
function prependWeekFilter(type) { | |
var elementId = ["week", type, "filter"].join("-"); | |
if ($("#" + elementId).length == 0) { | |
$(".table-list-header-toggle.right").prepend("<div id=\"" + elementId + "\" class=\"left select-menu js-menu-container js-select-menu\"><span class=\"btn-link select-menu-button icon-only js-menu-target\" role=\"button\" aria-haspopup=\"true\">" + type.charAt(0).toUpperCase() + type.slice(1) + " at <span class=\"sort-label\"></span></span><div class=\"select-menu-modal-holder js-menu-content js-navigation-container\" aria-hidden=\"true\"><div class=\"select-menu-modal\"><div class=\"select-menu-header\"><span class=\"select-menu-title\">Filter by " + type + " at</span><span class=\"octicon octicon-x js-menu-close\" role=\"button\" aria-label=\"Close\"></span></div>" + buildFilterElementList(beginningDates, type) + "</div></div></div>"); | |
} | |
} | |
function prependWeekFilters() { | |
if ($(".table-list-header-toggle.right").length > 0) { | |
prependWeekFilter("closed"); | |
prependWeekFilter("created"); | |
} | |
} | |
// Main | |
var latestBeginningDate = getLatestBeginningDateOfWeek(beginningOfWeek); | |
var beginningDates = []; | |
for(i = 0 ; i <= weeks ; i ++) { | |
var beginningDate = new Date(latestBeginningDate.getTime()); | |
beginningDate.setDate(latestBeginningDate.getDate() - 7 * i); | |
beginningDates.push(beginningDate); | |
} | |
setInterval(prependWeekFilters, 500); //Fix | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment