Skip to content

Instantly share code, notes, and snippets.

@greatghoul
Created May 12, 2017 03:13
Show Gist options
  • Save greatghoul/f79510d5909a5033bd27b49f14c09df8 to your computer and use it in GitHub Desktop.
Save greatghoul/f79510d5909a5033bd27b49f14c09df8 to your computer and use it in GitHub Desktop.
Basecamp Todo Auto Number
// ==UserScript==
// @name Basecamp Todo Auto Number
// @namespace http://tampermonkey.net/
// @version 0.1
// @description Prefix todo's title with an autoincrement number by shortcut Cmd+click
// @author You
// @require https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js
// @match https://3.basecamp.com/*
// ==/UserScript==
(function() {
'use strict';
var SUMMARY_PAT = /^\d+\. .*$/;
function setSuggestedNumber($input, todos) {
var numbers = todos.filter(function(todo) { return SUMMARY_PAT.test(todo); })
.map(function(todo) { return parseInt(todo.replace(/^(\d+)\. .*/, '$1')); })
.concat([0]);
var number = Math.max.apply(null, numbers) + 1;
$input.val('' + number + '. ' + $input.val());
}
function autoSetNumber(event) {
if (!event.metaKey) return;
var $input = $(this);
if (!SUMMARY_PAT.test($input.val())) {
var todos = [];
var $todolist = $input.parents('.todolist');
$todolist.find('.todo .checkbox__content > a').each(function() { todos.push($(this).text()); });
var $listLink = $todolist.find('.todolist__permalink');
if ($listLink.length) {
$.getJSON($listLink.attr('href') + '/todos.json?completed=1', function(data) {
data.forEach(function(todo) {
todos.push(todo.content);
});
setSuggestedNumber($input, todos);
});
} else {
setSuggestedNumber($input, todos);
}
}
}
$(document).on('click', '.todos-form__input--summary', autoSetNumber);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment