Skip to content

Instantly share code, notes, and snippets.

@bentleyo
Last active February 3, 2020 21:10
Show Gist options
  • Save bentleyo/5933119 to your computer and use it in GitHub Desktop.
Save bentleyo/5933119 to your computer and use it in GitHub Desktop.
Timesheets Select2 UserScript
// ==UserScript==
// @name Timesheets Select2
// @namespace https://gist.github.com/Bentleyo/5933119/raw/TimesheetsSelect2.js
// @version 0.1.8 (this one is dedicated to Scotty)
// @description Replace awful timesheets select with select2 interface
// @match https://www.timesheets.com.au/*
// @copyright 2012+, Bentley O'Kane-Chase
// @require http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js
// @require https://cdnjs.cloudflare.com/ajax/libs/select2/3.5.2/select2.min.js
// @resource select2-352-css https://cdnjs.cloudflare.com/ajax/libs/select2/3.5.2/select2.min.css
// ==/UserScript==
GM_addStyle(GM_getResourceText("select2-352-css"));
GM_addStyle('.select2-result-label { font-family: sans-serif; font-size:14px; } ');
GM_addStyle('.select2-results { max-height: 400px; } ');
$('body').append("<style>.select2-container .select2-choice .select2-arrow b { background-image: url(https://cdnjs.cloudflare.com/ajax/libs/select2/3.5.2/select2.png)!important; background-repeat: no-repeat!important; background-size: 60px 40px!important}</style>")
$('#frmTimesheet').on('keypress', ':input', function(e){
if ( e.which == 13 ) { // Enter key = keycode 13
var t = e.target;
// Prevent enter key from submitting the form ONLY if focussed on select2 elements
if(t.classList.contains('select2-offscreen') || t.classList.contains('select2-input')) {
e.preventDefault();
return false;
}
}
});
$(window).load(function() {
function format(item) {
if(item.text) {
if(item.id) {
var match = item.id.match(/^([^{]+){/);
if(match) {
return $("<span><small style=\"color:lightgrey;\">" + match[1] + "</small> " + item.text + "</span>")
}
}
return item.text;
} else {
return $("<span>Select Project</span>");
}
}
// Construct select2. Requires jQuery object as parameter
function constructSelect(select) {
select.css('width', '100%');
if(!select.find('option').length)
select.append('<option></option>');
select.select2({
formatSelection: format,
formatResult: format,
matcher: function(term, text, opt) {
var body = text;
var value = opt.val();
var match = value.match(/^([^{]+){/);
if(match) {
body += " " + match[1];
}
return body.toLowerCase().indexOf(term.toLowerCase())>=0;
},
sortResults: function(results, container, query) {
// Use default order (should be sorted by timesheets, but put the following items first)
var highPriority = ['iOnline Internal - Miscellaneous (please specify task completed)', 'iOnline Misc BILLABLE - Developer', 'Hosting Support (Email/ iCMS/ iCart Support) - Developer'];
var first = results.shift();
results.forEach(function(value, index) {
if(value.text && highPriority.indexOf(value.text) > -1) {
var copy = results[index];
results.splice(index, 1);
results.unshift(copy);
}
});
results.unshift(first);
return results;
}
});
// Call timesheets methods on change
select.on('change', function() {
try { ProjectChange(this); } catch(e) { }
try { NewRowInsert(this); } catch(e) { }
});
}
// Override new row insert method (but call the original inside)
oldRowInsert = NewRowInsert;
NewRowInsert = function(row) {
// Timesheets method of calculating id
var sName = $(row).attr('name');
var nMarker = sName.indexOf("_") + 1;
var sIndex = sName.substr(nMarker, sName.length - nMarker);
if(parseInt($('#InputRows').val()) != parseInt(sIndex))// only add a new row from the last one
return true;
// Find the select in the row
var select = $($(row).parents('tr:first').find('select'));
// Calculate original value
var originalVal = select.val();
select.addClass('last-select-in-form').select2('destroy');// Add temporary id - marking what will need select2 constructs (select is duplicated)
var ret = oldRowInsert(row);// Get Timesheets defined return value
constructSelect($('.last-select-in-form').removeClass('last-select-in-form'));// Construct selects
select.select2('val', originalVal).trigger("change");// Set select value back to original
return ret;
}
$('#frmTimesheet select').each(function() {
constructSelect($(this));
});
});
@bentleyo
Copy link
Author

Modified the script so it puts iOnline Internal tasks at the top. Not the most performant, but JS is quick enough that you don't notice.

@j0rdsta
Copy link

j0rdsta commented Apr 17, 2015

Nuuuuu, it broke! Select2 moved away from the http://ivaynberg.github.io/select2/ domain, but for some reason the replacement domain http://select2.github.io/select2/ doesn't appear to work either 😠

@bentleyo
Copy link
Author

Changes:

  • Switch to CDN
  • Display project code
  • Prevent form submission when hitting enter

@bentleyo
Copy link
Author

Changes:

  • Only prevent form submission if focussed on select2 input fields

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment