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

Modified the "days of the week" code to make the first day configurable #37

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
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
13 changes: 13 additions & 0 deletions app/options.html
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,19 @@ <h1>OpenAir Reborn Options</h1>
</select>
<p class="description">Choosing "Yes" will automatically save your timesheet after 15 minutes of inactivity.</p>
</div>
<div class="form-input-wrapper">
<label for="firstDayOfWeek">First day of the week</label>
<select id="firstDayOfWeek" name="firstDayOfWeek" ng-model="firstDayOfWeek">
<option value="su">Sunday</option>
<option value="mo">Monday</option>
<option value="tu">Tuesday</option>
<option value="we">Wednesday</option>
<option value="th">Thursday</option>
<option value="fr">Friday</option>
<option value="sa">Saturday</option>
</select>
<p class="description">Select the first day of the week. This can be set by the business, and will be the first time entry column.</p>
</div>
<!--
<div class="form-input-wrapper">
<label for="persistentTimers">Persistent timers</label>
Expand Down
34 changes: 26 additions & 8 deletions app/scripts/openair.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
*/
app.service('OpenAirService', function() {

this.firstDayOfWeek = 'mo';

var parent = this; // Used in functions below to reference other functions below.

/**
Expand Down Expand Up @@ -382,21 +384,37 @@ app.service('OpenAirService', function() {

/**
* Helper function to convert two digit day code to integer.
* This takes the "first day of week" setting into account
*
* @TODO: Start using numbers instead of day codes. It'll remove a lot of dumb logic.
*
* @param {string} dayCode
* @returns {int}
*/
this.getDayNum = function(dayCode) {
var weekdays = [];
weekdays.mo = 0;
weekdays.tu = 1;
weekdays.we = 2;
weekdays.th = 3;
weekdays.fr = 4;
weekdays.sa = 5;
weekdays.su = 6;
var weekdays = {};
var days = [
'su',
'mo',
'tu',
'we',
'th',
'fr',
'sa',
];

// figure out which day is the first day
var firstDayStr = parent.firstDayOfWeek;
var firstDayIndex = days.findIndex(function (d) { return d === firstDayStr; });

// assign integers for each day, looping around where necessary
days.forEach(function (day, i) {
var index = i - firstDayIndex;
if(index < 0) {
index = days.length + index;
}
weekdays[day] = index;
});
return weekdays[dayCode];
};

Expand Down
4 changes: 3 additions & 1 deletion app/scripts/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ app.controller('OptionsController', ['$scope', function($scope) {
timeFormat : $scope.timeFormat,
multipleTimers : $scope.multipleTimers,
persistentTimers : $scope.persistentTimers,
autosave : $scope.autosave
autosave : $scope.autosave,
firstDayOfWeek : $scope.firstDayOfWeek,
}, function() {
$scope.status = "Settings saved successfully.";
$scope.$apply();
Expand Down Expand Up @@ -69,6 +70,7 @@ app.controller('OptionsController', ['$scope', function($scope) {
$scope.load('multipleTimers', 1);
$scope.load('persistentTimers', 1);
$scope.load('autosave', 0);
$scope.load('firstDayOfWeek', 'mo');
};

// And away we go...
Expand Down
57 changes: 35 additions & 22 deletions app/scripts/timeentry.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,13 +297,16 @@ app.controller('TimeEntryController', ['$scope', '$timeout', '$interval', 'OpenA
* @param {string} optionName
* @param {mixed} defaultVal
*/
$scope.loadSetting = function(optionName, defaultVal) {
$scope.loadSetting = function(optionName, defaultVal, callback) {
var exists = false;
chrome.storage.sync.get(optionName, function (obj) {
if (obj[optionName]) {
exists = true;
$scope[optionName] = obj[optionName];
}
if(callback) {
callback();
}
});
if (!exists) {
$scope[optionName] = defaultVal;
Expand All @@ -317,6 +320,34 @@ app.controller('TimeEntryController', ['$scope', '$timeout', '$interval', 'OpenA
$scope.loadSetting('timeFormat', 'hhmm');
$scope.loadSetting('multipleTimers', 1);
$scope.loadSetting('autosave', 0);
$scope.loadSetting('firstDayOfWeek', 'fr', $scope.afterFirstDayOfWeekLoaded);
};

/**
* Callback after the firstDayOfWeek setting has been loaded
* After we load this, we can pass it to OpenAirService and set up everything that relies on it
*/
$scope.afterFirstDayOfWeekLoaded = function() {
// set the first day of the week for calculations
OpenAirService.firstDayOfWeek = $scope.firstDayOfWeek;

// Days of the week, to match code to day and cycle through in the view.
$scope.weekdays = [
{code: "mo", name: 'Monday', shortName: 'Mo', timestamp: OpenAirService.getDateTimestamp("mo")},
{code: "tu", name: 'Tuesday', shortName: 'Tu', timestamp: OpenAirService.getDateTimestamp("tu")},
{code: "we", name: 'Wednesday', shortName: 'We', timestamp: OpenAirService.getDateTimestamp("we")},
{code: "th", name: 'Thursday', shortName: 'Th', timestamp: OpenAirService.getDateTimestamp("th")},
{code: "fr", name: 'Friday', shortName: 'Fr', timestamp: OpenAirService.getDateTimestamp("fr")},
{code: "sa", name: 'Saturday', shortName: 'Sa', timestamp: OpenAirService.getDateTimestamp("sa")},
{code: "su", name: 'Sunday', shortName: 'Su', timestamp: OpenAirService.getDateTimestamp("su")}
];

// The time table is listed in reverse order.
// @TODO: Use a filter instead.
$scope.reverseWeekdays = $scope.weekdays.reverse();

// Default the "Day" field to the current day.
$scope.when = [$scope.getDay()];
};

/**
Expand All @@ -339,27 +370,6 @@ app.controller('TimeEntryController', ['$scope', '$timeout', '$interval', 'OpenA
return weekdays;
};

// Days of the week, to match code to day and cycle through in the view.
$scope.weekdays = [
{code: "mo", name: 'Monday', shortName: 'Mo', timestamp: OpenAirService.getDateTimestamp("mo")},
{code: "tu", name: 'Tuesday', shortName: 'Tu', timestamp: OpenAirService.getDateTimestamp("tu")},
{code: "we", name: 'Wednesday', shortName: 'We', timestamp: OpenAirService.getDateTimestamp("we")},
{code: "th", name: 'Thursday', shortName: 'Th', timestamp: OpenAirService.getDateTimestamp("th")},
{code: "fr", name: 'Friday', shortName: 'Fr', timestamp: OpenAirService.getDateTimestamp("fr")},
{code: "sa", name: 'Saturday', shortName: 'Sa', timestamp: OpenAirService.getDateTimestamp("sa")},
{code: "su", name: 'Sunday', shortName: 'Su', timestamp: OpenAirService.getDateTimestamp("su")}
];

// The time table is listed in reverse order.
// @TODO: Use a filter instead.
$scope.reverseWeekdays = $scope.weekdays.reverse();

// Default the "Day" field to the current day.
$scope.when = [$scope.getDay()];

// Load in any user settings if saved, otherwise just load the defaults.
$scope.loadSettings();

// Update the submit button's value based on whether the time field has a value or not.
$scope.$watch('time', function(newTime) {
if (newTime) {
Expand Down Expand Up @@ -503,4 +513,7 @@ app.controller('TimeEntryController', ['$scope', '$timeout', '$interval', 'OpenA
};

var checkForTimesheet = $interval($scope.initializeFromTimesheet, 100);

// Load in any user settings if saved, otherwise just load the defaults.
$scope.loadSettings();
}]);