Skip to content

Instantly share code, notes, and snippets.

@ajhodges
Last active August 23, 2019 19:15
Show Gist options
  • Save ajhodges/d258c2bfa484018127939e7bd037f2de to your computer and use it in GitHub Desktop.
Save ajhodges/d258c2bfa484018127939e7bd037f2de to your computer and use it in GitHub Desktop.
Userscript for adding drag n' drop to Yahoo Pick'em
// ==UserScript==
// @name Yahoo Pick'em Drag n' Drop
// @namespace http://tampermonkey.net/
// @version 0.1
// @description No more awful confidence points UI!
// @author ajhodges
// @match https://football.fantasysports.yahoo.com/*
// @grant none
// @require http://code.jquery.com/jquery-3.4.1.min.js
// @require https://code.jquery.com/ui/1.12.1/jquery-ui.min.js
// ==/UserScript==
/* jshint esversion: 6 */
/* globals $:false */
function addGlobalStyle(css) {
var head;
var style;
head = document.getElementsByTagName("head")[0];
if (!head) { return; }
style = document.createElement("style");
style.type = "text/css";
style.innerHTML = css;
head.appendChild(style);
}
(function() {
"use strict";
// Add style for placeholder (hover element)
addGlobalStyle(`.dnd-placeholder {
height: 55px;
border: 2px dashed red;
}`);
// Add handle for dragging
addGlobalStyle(`.conf-score {
cursor: grab;
}
`);
addGlobalStyle(`.conf-score::after {
content: '\\E00B';
font-family: "yahoo";
}`);
var numGames = $("#ysf-picks-table tbody tr").length;
$("#ysf-picks-table tbody").sortable({
axis: "y",
containment: $("#ysf-picks-table"),
cursor: "grabbing",
handle: ".conf-score",
opacity: 0.8,
placeholder: "dnd-placeholder",
revert: 100,
create: function(e, ui) {
// Sort initially by confidence
var rows = $("#ysf-picks-table tbody tr").get();
rows.sort(function(row1, row2){
// Sort descending
var val1 = parseInt($(row1).find("select").val());
var val2 = parseInt($(row2).find("select").val());
return val2 - val1;
});
$(rows).each(function(index){
$(this).find("select").val(numGames - index); // Assign select value if blank
$("#ysf-picks-table tbody").append(rows[index]);
});
// Set confidence dropdowns to read-only
$("#ysf-picks-table tbody select").css("pointer-events","none");
},
start : function(event, ui) {
ui.item.data("start-pos", ui.item.index()+1);
},
change: function(e, ui) {
// Reassign confidence points based on dragging
var startPos = ui.item.data("start-pos");
var correction = (startPos <= ui.placeholder.index()) ? 0 : 1;
// Resort all other games/points
ui.item.parent().find("tr:not(.ui-sortable-helper)").each(function(idx, el){
var index = $(el).index();
if ( ( index+1 >= startPos && correction === 0) || (index+1 <= startPos && correction === 1 ) )
{
$(this).find("select").val(numGames + 1 - (index + correction));
}
});
// Get new value for 'floating' game
ui.item.find("select").val(numGames + 1 - (ui.placeholder.index() + correction));
}
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment