From 8e8763bdcde814fdb57d542c536418c1977f9e97 Mon Sep 17 00:00:00 2001 From: Jesse Weinstein Date: Sun, 28 Dec 2014 22:42:31 -0800 Subject: [PATCH] Simplify the logic of the date parsing func & other cleanup --- batch-add-recording-relationships.user.js | 92 +++++++++++------------ 1 file changed, 44 insertions(+), 48 deletions(-) diff --git a/batch-add-recording-relationships.user.js b/batch-add-recording-relationships.user.js index 1cac040..c43952d 100644 --- a/batch-add-recording-relationships.user.js +++ b/batch-add-recording-relationships.user.js @@ -592,25 +592,9 @@ function batch_recording_rels() { $(".tbl > thead > tr").append("Performance Attributes"); - $recordings.append( - $('').append( - $('part./' + - 'live/' + - 'inst./' + - 'cover') - .css("cursor", "pointer") - .data("checked", false) - .click(function () { - var $this = $(this), checked = !$this.data("checked"); - $this.data("checked", checked); - if (checked) { - $this.css({"background": "blue", "color": "white"}); - } else { - $this.css({"background": "inherit", "color": "black"}); - } - }), ' ')); - - $recordings.find("td.bpr_attrs input.date") + var date_element = $('') + .attr('type', "text") + .addClass('date') .val("yyyy-mm-dd") .css({"color": "#ddd", "width": "7em", "border": "1px #999 solid"}) .bind("focus", function () { @@ -625,45 +609,57 @@ function batch_recording_rels() { } }) .bind("input", function () { - var error = (function ($input) { - return function () { + var regex_match, date = null, + error = ( + function ($input) {return function () { $input.css("border-color", "#f00"); $input.parent().data("date", null); - }; - })($(this)); + date = null; + };})($(this)), + use_if_in_range = function (pos, type, min, max) { + var val; + if (date != null && regex_match[pos]) { + val = parseInt(regex_match[pos], 10); + if (val < min || val > max) { + error(); + } else { + date[type] = val; + } + } + }; + + $(this).css("border-color", "#999"); if (this.value) { $(this).css("color", "#000"); - var date = this.value.match(/^([0-9]{4})(?:-([0-9]{2})(?:-([0-9]{2}))?)?$/); - if (date == null) { + regex_match = this.value.match(/^([0-9]{4})(?:-([0-9]{2})(?:-([0-9]{2}))?)?$/); + if (regex_match != null) { + date = {"year": parseInt(date[1], 10)}; + use_if_in_range(2, 'month', 1, 12) + use_if_in_range(3, 'day', 1, 31); + } else { error(); - return; } - var data = {}, year = date[1], month = date[2], day = date[3]; - data["year"] = parseInt(year, 10); - if (month) { - month = parseInt(month, 10); - if (month < 1 || month > 12) { - error(); - return; - } - data["month"] = month; - } - if (day) { - day = parseInt(day, 10); - if (day < 1 || day > 31) { - error(); - return; - } - data["day"] = day; - } - $(this).parent().data("date", data); - } else { - $(this).parent().data("date", null); } - $(this).css("border-color", "#999"); + $(this).parent().data("date", data); }); + $recordings.append(td( + $('part./' + + 'live/' + + 'inst./' + + 'cover') + .css("cursor", "pointer") + .data("checked", false) + .click(function () { + var checked = !$this.data("checked"); + $(this).data("checked", checked) + .css({"background": checked ? "blue": "inherit", + "color": checked ? "white" : "black"}); + }), + ' ', + date_element).addClass("bpr_attrs")); + // Style buttons function style_buttons($buttons) {