Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attempt to reset view after closing #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 117 additions & 14 deletions cronGen.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,111 @@



(function ($) {

var resultsName = "";
var inputElement;
var displayElement;


function initView(cron) {
//Utility Functions
function padLeft(input) {
var formattedInput = '' + input;
return formattedInput.length === 1 ? ('0' + formattedInput) : formattedInput;
}

function startsWith(str, substr) {
return str.indexOf(substr) === 0;
}

function includes(str, substr) {
return str.indexOf(substr) >= 0;
}

//Get our cron segments
var segments = cron.split(' ');

//Determine which tab to select
var tabToSelect = function () {
if (segments[2] === '*') {
return 'MinutesTab';
} else if (segments[3] === '1/1') {
return 'HourlyTab';
} else if (startsWith(segments[3], '1/') || (segments[3] === '?' && segments[5] === 'MON-FRI')) {
return 'DailyTab';
} else if (segments[3] === '?' && segments[4] === '*') {
return 'WeeklyTab';
} else if (startsWith(segments[4], '1/')) {
return 'MonthlyTab';
} else {
return 'YearlyTab';
}
}();

//Select the tab
$('#' + tabToSelect).click();

//Populate the data for the tab
if (tabToSelect === 'MinutesTab') {
$('#MinutesInput').val(segments[1].substring(2));
} else if (tabToSelect === 'HourlyTab') {
if (segments[2].startsWith('0/')) {
$('input[name="HourlyRadio"]:first').attr('checked', 'checked');
$('#HoursInput').val(segments[2].substring(2));
} else {
$('input[name="HourlyRadio"]:nth(1)').attr('checked', 'checked');
$('#AtHours').val(padLeft(segments[2]));
$('#AtMinutes').val(padLeft(segments[1]));
}
} else if (tabToSelect === 'DailyTab') {
if (segments[5] === 'MON-FRI') {
$('input[name="DailyRadio"]:nth(1)').attr('checked', 'checked');
} else {
$('input[name="DailyRadio"]:first').attr('checked', 'checked');
$('#DaysInput').val(segments[3].substring(2));
}
$('#DailyHours').val(padLeft(segments[2]));
$('#DailyMinutes').val(padLeft(segments[1]));
} else if (tabToSelect === 'WeeklyTab') {
segments[5].split(',').forEach(function (input) {
$('#Weekly').find('input[value="' + input + '"]').attr('checked', 'checked');
});
$('#WeeklyHours').val(padLeft(segments[2]));
$('#WeeklyMinutes').val(padLeft(segments[1]));
} else if (tabToSelect === 'MonthlyTab') {
if (includes(segments[5], '#')) {
$('input[name="MonthlyRadio"]:nth(1)').attr('checked', 'checked');
var vals = segments[5].split('#');
var weekName = vals[0];
var monthWeek = vals[1];
$('#WeekDay').val(monthWeek);
$('#DayInWeekOrder').val(weekName);
$('#EveryMonthInput').val(segments[4].substring(2));
} else {
$('input[name="MonthlyRadio"]:first').attr('checked', 'checked');
$('#DayOfMOnthInput').val(segments[3]);
$('#MonthInput').val(segments[4].substring(2));
}
$('#MonthlyHours').val(padLeft(segments[2]));
$('#MonthlyMinutes').val(padLeft(segments[1]));
} else {
if (includes(segments[5], '#')) {
$('input[name="YearlyRadio"]:nth(1)').attr('checked', 'checked');
var vals = segments[5].split('#');
var weekName = vals[0];
var monthWeek = vals[1];
$('#DayOrderInYear').val(monthWeek);
$('#DayWeekForYear').val(weekName);
$('#MonthsOfYear2').val(segments[4]);
} else {
$('input[name="YearlyRadio"]:first').attr('checked', 'checked');
$('#MonthsOfYear').val(segments[4]);
$('#YearInput').val(segments[3]);
}
$('#YearlyHours').val(padLeft(segments[2]));
$('#YearlyMinutes').val(padLeft(segments[1]));
}
}

$.fn.extend({
cronGen: function () {
//create top menu
Expand Down Expand Up @@ -163,15 +263,15 @@
$(yearlyTab).appendTo(tabContent);
$(tabContent).appendTo(span12);

//creating the button and results input
//creating the button and results input
resultsName = $(this).prop("id");
$(this).prop("name", resultsName);

$(span12).appendTo(row);
$(row).appendTo(container);
$(container).appendTo(mainDiv);
$(cronContainer).append(mainDiv);

var that = $(this);

// Hide the original input
Expand Down Expand Up @@ -204,28 +304,32 @@

}).on('click', function (e) {
e.preventDefault();

fillDataOfMinutesAndHoursSelectOptions();
fillDayWeekInMonth();
fillInWeekDays();
fillInMonths();

var currentCron = $(inputElement).val();
if (currentCron) {
setTimeout(function() {
initView(currentCron);
}, 0);
}

$('#CronGenTabs a').click(function (e) {
e.preventDefault();
$(this).tab('show');
//generate();
});
$("#CronGenMainDiv select,input").change(function (e) {
generate();
});
$("#CronGenMainDiv input").focus(function (e) {

$("#CronGenMainDiv select, #CronGenMainDiv input").change(function (e) {
generate();
});
//generate();
});
return;
}
});


var fillInMonths = function () {
var days = [
Expand Down Expand Up @@ -358,4 +462,3 @@
};

})(jQuery);