This repository has been archived by the owner on Oct 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
site.js
114 lines (99 loc) · 3.28 KB
/
site.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/**** UNL Textbook Buyback ****/
var Utils = {
cleanIsbn: function (isbn) {
return isbn.replace(/[^0-9]/g, '');
},
isNullOrEmpty: function (s) {
return (s == null || s == undefined || s == '');
},
test: function () {
buybackVM.addBook('9780262033848');
buybackVM.addBook('9780805091182');
buybackVM.addBook('9781118226155');
buybackVM.addBook('9780738205373');
buybackVM.addBook('9780071749275');
buybackVM.addBook('9781111773397');
buybackVM.addBook('9780446563048');
buybackVM.addBook('0020303955');
buybackVM.addBook('9781845116545');
buybackVM.addBook('');
buybackVM.addBook('');
buybackVM.addBook('');
}
}
var BuybackViewModel = function () {
var self = this;
self.inputIsbn = ko.observable('');
self.searchIsbn = ko.computed(function () {
return Utils.cleanIsbn(self.inputIsbn());
});
self.books = ko.observableArray([]);
self.addBook = function (isbn) {
if (!Utils.isNullOrEmpty(isbn)) {
self.books.push(new Book(isbn));
}
}
self.search = function () {
self.addBook(self.searchIsbn());
self.inputIsbn('');
}
}
var Book = function (isbn) {
var self = this;
self.isbn = ko.observable(isbn);
self.title = ko.observable('');
self.author = ko.observable('');
self.edition = ko.observable('');
self.providers = ko.observableArray([]);
self.isLoading = ko.observable(false);
self.fetchData = function () {
self.isLoading(true);
$.get('get.php', { 'isbn': self.isbn() }, function (data) {
data = $.parseJSON(data);
self.title(data.Title);
self.providers(ko.utils.arrayMap(data.Providers, function (item) {
return new Provider(item);
}));
}).then(function () {
self.isLoading(false);
self.findBestProvider();
});
}
self.findBestProvider = function () {
var providers = self.providers();
var best = null;
var bestPrice = 0;
for (var i = 0; i < providers.length; i++) {
var price = parseFloat(providers[i].price());
if (price > bestPrice) {
best = providers[i];
bestPrice = price;
}
}
if (best) {
best.isBest(true);
}
}
self.fetchData();
}
var Provider = function (data) {
var self = this;
self.provider = ko.observable(data.Provider);
self.title = data.Title;
self.author = data.Author;
self.edition = data.Edition;
self.isbn = data.Isbn;
self.price = ko.observable(data.Price);
self.displayPrice = ko.computed(function () {
var price = self.price();
if (price.length > 1 && price.indexOf('.') == price.length-2) {
price = price + "0";
}
return '$' + (price.substr(-3) == ".00" ? price.substr(0,price.length-3) : price);
});
self.isBest = ko.observable(false);
}
var buybackVM = new BuybackViewModel();
$(document).ready(function () {
ko.applyBindings(buybackVM, $('body')[0]);
})