Repository URL to install this package:
|
Version:
1.0.7 ▾
|
(function() {
'use strict';
var gobox = angular.module('angularGobox', []);
})();
(function() {
'use strict';
angular.module('angularGobox')
.constant('CustomerboxStatus', {
inStorage: 1,
atCustomer: 2,
beingPickedUp: 3, // legacy
beingDroppedOff: 4, // legacy
ended: 5,
pendingDropOff: 6,
pendingPickUp: 7
})
.constant('BoxType', {
standard: 1,
odd: 2
})
.constant('TripType', {
dropOff: 1,
pickUp: 2
})
.constant('TripStatus', {
awaitingConfirmation: 1,
confirmed: 2,
fulfilled: 3,
cancelled: 4
})
;
})();
(function() {
'use strict';
angular.module('angularGobox').factory('AddressValidator', AddressValidatorFactory);
function AddressValidatorFactory() {
function AddressValidator(address) {
this.address = addEmptyValues(address);
}
AddressValidator.requiredFields = ['name', 'address1', 'postal_code', 'city'];
AddressValidator.addressFields = ['name', 'tlf', 'address1', 'postal_code', 'city'];
AddressValidator.prototype.invalidFields = function() {
var fields = _.pick(this.address, AddressValidator.requiredFields);
return _.reduce(fields, function(arr, val, key) {
if (!val) arr.push(key);
return arr;
}, []);
}
AddressValidator.prototype.valid = function() {
return this.invalidFields().length == 0;
};
function addEmptyValues(address) {
return angular.extend(_.reduce(AddressValidator.addressFields, function(obj, key) {
obj[key] = ''; return obj;
}, {}), address);
}
return AddressValidator;
}
})();
(function () {
angular.module('angularGobox').factory('DatePicker', DatePickerFactory);
DatePickerFactory.$inject = ['$rootScope'];
function DatePickerFactory($rootScope) {
var $scope = $rootScope.$new();
var dateFormat = 'dddd, Do MMMM';
var timeFormat = 'HH:MM';
// saturday is 6, sunday is 0
var SAT = 6, SUN = 0;
function DatePicker(date, occupiedTimeslots) {
var picker = this;
picker.refDate = date || new Date();
picker.occupiedTimeslots = occupiedTimeslots || [];
calculateDates(picker);
$scope.$watch(function () {
return picker.date;
}, _.partial(calculateTimes, picker));
$scope.$watch(function () {
return picker.time;
}, function (time) {
picker.datetime = time && moment(picker.date).add(time, 'hours').toDate();
});
}
// private
function hours(i) {
return ('0' + i + ':00').slice(-5, 6);
}
function add(a, b) {
return a + b;
}
function calculateDates(picker) {
// We don't want to allow new trips during the weekend.
// So, if today is saturday (day num 6), we add an offset of 1 day
// to the possibleDates calculation. Since the earliest available date
// is at minimum today + 1 day, this means that we skip sunday.
var offset = picker.refDate.getDay() === SAT ? 1 : 0;
picker.possibleDates = _.map(_.times(30, _.partial(add, 1)), function (i) {
var date = moment(picker.refDate).startOf('day').add(i + offset, 'days');
return {
label: date.format(dateFormat),
value: date.toDate()
};
});
picker.date = picker.possibleDates[0].value;
}
function calculateTimes(picker) {
var firstHour = 8, lastHour = 19;
var day = picker.date.getDay();
var tomorrow = moment(picker.refDate).startOf('day').add(1, 'days');
if (tomorrow.diff(moment(picker.date).startOf('day'), 'days') === 0) {
// If the picked date is tomorrow, the earliest available time is 14:00.
firstHour = 14;
} else if (day === SAT || day === SUN) {
// Only set firstHour for weekends, if it isn't the next day.
firstHour = 9;
}
if (day === SAT || day === SUN) {
// Set lastHour for weekends no matter what.
lastHour = 17;
}
var span = lastHour - firstHour + 1;
picker.possibleTimes = _.map(_.times(span, _.partial(add, firstHour)), function (i) {
return {
label: '' + hours(i) + ' - ' + hours(i + 1),
value: i
};
});
removeOccupiedTimeslots(picker);
picker.time = picker.possibleTimes[0].value;
}
function removeOccupiedTimeslots(picker) {
picker.possibleTimes = _.filter(picker.possibleTimes, function (possibleTime) {
// Remove all possible times that are on the same date && time as already occupied timeslots.
return _.filter(picker.occupiedTimeslots, function (occupiedTimeslot) {
return moment(picker.date).diff(moment(occupiedTimeslot.timeslot), 'days') === 0 && moment(occupiedTimeslot.timeslot).hour() === possibleTime.value;
}).length < 1;
});
}
return DatePicker;
}
})();
(function() {
'use strict';
angular.module('angularGobox').factory('OrderPriceCalculator', OrderPriceCalculatorFactory);
OrderPriceCalculatorFactory.$inject = ["TripType"];
function OrderPriceCalculatorFactory(TripType) {
function OrderPriceCalculator(order) {
this.order = order;
}
OrderPriceCalculator.prototype.priceWithSubscription = function(subscription) {
var price; switch (this.order.trip_type) {
case TripType.pickUp: price = priceForPickup(this.order, subscription); break;
case TripType.dropOff: price = priceForDropOff(this.order, subscription); break;
default: break;
}
return price;
};
function priceForPickup(order, subscription) {
return 0;
}
function priceForDropOff(order, subscription) {
return subscription.delivery_price;
}
return OrderPriceCalculator;
}
})();
(function() {
'use strict';
angular.module('angularGobox').filter('customerboxStatusText', customerboxStatusText)
customerboxStatusText.$inject = ['CustomerboxStatus'];
function customerboxStatusText(CustomerboxStatus) {
return function(status) {
status = parseInt(status, 10);
switch(status) {
case CustomerboxStatus.inStorage: return "I opbevaring"; break;
case CustomerboxStatus.atCustomer: return "Hos mig"; break;
case CustomerboxStatus.ended: return "Afsluttet"; break;
case CustomerboxStatus.pendingDropOff: return "Afventer afhentning"; break;
case CustomerboxStatus.pendingPickUp: return "Afventer aflevering"; break;
default: return status; break;
}
}
}
})();
(function() {
'use strict';
angular.module('angularGobox').filter('tripStatusText', tripStatusText)
tripStatusText.$inject = ['TripStatus'];
function tripStatusText(TripStatus) {
return function(status) {
status = parseInt(status, 10);
switch(status) {
case TripStatus.awaitingConfirmation: return "Afventer godkendelse"; break;
case TripStatus.confirmed: return "Godkendt"; break;
case TripStatus.fulfilled: return "Fuldendt"; break;
case TripStatus.cancelled: return "Aflyst"; break;
default: return status; break;
}
}
}
})();
(function() {
'use strict';
angular.module('angularGobox').service('API', API);
API.$inject = ['$http', '$rootScope'];
function API($http, $rootScope) {
var service = this;
service.baseUrl = '';
service.token = '';
service.defaultHeaders = {
'Accept': 'application/json'
};
service.get = get;
service.post = post;
service.put = put;
service.patch = patch;
service.delete = delete_;
service.authorize = authorize;
// Public
function get(path, params, opts) {
path = service.baseUrl + expandPath(path);
opts = expandOpts(opts);
opts.params = params;
return $http.get(path, opts).error(err);
}
function post(path, data, opts) {
path = service.baseUrl + expandPath(path);
opts = expandOpts(opts);
return $http.post(path, data, opts).error(err);
}
function put(path, data, opts) {
path = service.baseUrl + expandPath(path);
opts = expandOpts(opts);
return $http.put(path, data, opts).error(err);
}
function patch(path, data, opts) {
path = service.baseUrl + expandPath(path);
opts = expandOpts(opts);
return $http(angular.extend({
method: 'PATCH',
url: path,
data: angular.toJson(data),
}, opts)).error(err);
}
function delete_(path, opts) {
path = service.baseUrl + expandPath(path);
opts = expandOpts(opts);
return $http.delete(path, opts).error(err);
}
function authorize(username, password) {
return post('/api-token-auth/', {
username: username,
password: password
}).success(function(data) {
service.token = data.token;
return data;
});
}
// Private
function expandPath(path) {
return '/api/v1' + path;
}
function expandOpts(opts) {
opts = opts || {};
opts.headers = opts.headers || service.defaultHeaders;
if (service.token) {
opts.headers['Authorization'] = 'Token ' + service.token;
}
return opts;
}
function pluckData(resp) {
return resp;
}
function err(error) {
if (error.detail === 'Invalid token')
$rootScope.$broadcast('api.events.invalid_token', error);
console.warn("API error", error);
console.log("API service", service);
return error;
}
}
})();
(function() {
'use strict';
angular.module('angularGobox').service('BoxRegistration', BoxRegistration);
BoxRegistration.$inject = ['BoxType'];
function BoxRegistration(BoxType) {
var service = this;
service.reset = reset;
// public
function reset() {
service.box_type = BoxType.standard;
service.box = undefined;
}
// private
function initialize() {
service.reset();
}
initialize();
}
})();
(function() {
'use strict';
angular.module('angularGobox').service('BoxRepo', BoxRepo);
BoxRepo.$inject = ["$rootScope", "API", "CustomerboxStatus"];
function BoxRepo($rootScope, API, CustomerboxStatus) {
var service = this;
service.$scope = $rootScope.$new();
service.update = update;
service.findById = findById;
service.findByBoxName = findByBoxName;
service.$scope.$watch(function() { return service.allBoxes; }, function() {
sortBoxes();
});
// public
function update() {
return API.get('/customerboxes/').success(function(boxes) {
service.allBoxes = boxes;
});
}
function findById(id) {
return _.find(service.allBoxes, { id: parseInt(id, 10) });
}
function findByBoxName(box_name) {
return _.find(service.allBoxes, { box_name: box_name });
}
// private
function sortBoxes() {
resetCollections();
_.each(service.allBoxes, function(box) {
if (box.status === CustomerboxStatus.inStorage) {
service.inStorage.push(box);
} else if (box.status === CustomerboxStatus.atCustomer) {
if (box.registered) service.atCustomer.registered.push(box);
else service.atCustomer.unregistered.push(box);
} else if (box.status === CustomerboxStatus.pendingDropOff) {
service.pendingDropOff.push(box);
} else if (box.status === CustomerboxStatus.pendingPickUp) {
service.pendingPickUp.push(box);
}
});
}
function resetCollections() {
service.inStorage = [];
service.atCustomer = {
registered: [],
unregistered: []
};
service.pendingDropOff = [];
service.pendingPickUp = [];
}
function initialize() {
service.allBoxes = [];
resetCollections();
}
initialize();
}
})();
(function() {
'use strict';
angular.module('angularGobox').service('DropOffOrder', DropOffOrder);
function DropOffOrder($rootScope) {
var service = this;
service.toggle = toggle;
service.contains = contains;
service.count = count;
service.prepare = prepare;
service.reset = reset;
service.reset();
// public
function toggle(box) {
if (contains(box)) {
_.remove(service.order.customerboxes, function(otherBox) {
return box.id === otherBox.id;
});
} else {
service.order.customerboxes.push(box);
}
return box;
}
function contains(given) {
return !!_.find(service.order.customerboxes, function(box) {
return given && box && given.id == box.id;
});
}
function count() {
return service.order.customerboxes.length;
}
function prepare() {
var data = angular.extend({}, service.order.address);
data.customerboxes = _.collect(service.order.customerboxes, 'id');
data.preferred_date = service.order.preferred_date;
data.trip_type = service.order.trip_type;
return data;
}
function reset() {
service.order = {
trip_type: 1,
customerboxes: [],
preferred_date: undefined,
address: {}
};
}
}
})();
(function() {
'use strict';
angular.module('angularGobox').service('PickupOrder', PickupOrder);
function PickupOrder() {
var service = this;
service.order = {
trip_type: 2,
customerboxes: [],
preferred_date: undefined,
address: {}
};
service.prepare = prepare;
// public
function prepare() {
var data = angular.extend({}, service.order.address);
data.customerboxes = _.collect(service.order.customerboxes, 'id');
data.preferred_date = service.order.preferred_date;
data.trip_type = service.order.trip_type;
return data;
}
}
})();
(function() {
'use strict';
angular.module('angularGobox').service('TripRepo', TripRepo);
TripRepo.$inject = ["$rootScope", "API", "TripType", "TripStatus"];
function TripRepo($rootScope, API, TripType, TripStatus) {
var service = this;
service.$scope = $rootScope.$new();
service.update = update;
service.findById = findById;
service.updateOccupiedTimeslots = updateOccupiedTimeslots;
service.$scope.$watch(function() { return service.allTrips; }, sortTrips);
// public
function update() {
return API.get('/trips/').success(function(trips) {
service.allTrips = trips;
});
}
function findById(id) {
return _.find(service.allTrips, { id: parseInt(id, 10) });
}
function updateOccupiedTimeslots() {
return API.get('/trips/occupied_timeslots/').success(function(occupiedTimeslots) {
service.occupiedTimeslots = occupiedTimeslots;
});
}
// private
function initialize() {
reset();
}
function reset() {
service.allTrips = [];
sortTrips();
}
function sortTrips() {
_.each(TripStatus, function(value, key) {
service[key] = _.filter(service.allTrips, { status: value });
});
}
initialize();
}
})();