-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paths3ml.js
112 lines (94 loc) · 3.41 KB
/
s3ml.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
function S3ml(conf) {
this.dom_element = document.getElementById(conf.container_id || "images");
this.previous_page_link = document.getElementById(conf.previous_page_id || "previous-page");
this.previous_page_link.addEventListener("click", this.previous_page.bind(this));
this.next_page_link = document.getElementById(conf.next_page_id || "next-page");
this.next_page_link.addEventListener("click", this.next_page.bind(this));
this.previous_page_link.href = this.next_page_link.href = "javascript:void(0)";
this.img_dir = conf.image_dir || "images";
this.page_size = conf.page_size || 5;
this.img_extensions = conf.image_extensions || ["png", "jpg", "jpeg", "gif"];
this.request_params = conf.request_params || "?C=M;O=D";
this.page = 0;
this.image_names = [];
this.get_image_names();
};
S3ml.prototype.images_in_page = function (page) {
return this.image_names.slice(page*this.page_size,
(page+1)*this.page_size);
};
S3ml.prototype.load_page = function (page) {
console.log("loading page: " + page);
imgs_in_page = this.images_in_page(page, this.page_size);
for (img_name of imgs_in_page) {
var href = this.img_dir + "/" + img_name;
this.add_image(href);
console.log("adding image: " + href);
}
if (this.previous_page_available(this.page-1)) {
this.previous_page_link.style.display = "inline";
} else {
this.previous_page_link.style.display = "none";
}
if (this.next_page_available(this.page+1)) {
this.next_page_link.style.display = "inline";
} else {
this.next_page_link.style.display = "none";
}
};
S3ml.prototype.get_image_names = function (complete) {
var req = new XMLHttpRequest();
req.open("GET", this.img_dir + this.request_params, true);
req.responseType = "document"; // Parse result as HTML
req.onreadystatechange = (function () {
if (req.readyState != 4 || req.status != 200) return;
data = req.responseXML
var sels = this.img_extensions.map(function(e) {return "a:contains(." + e + ")"});
var selector = sels.join(",");
var anchors = data.getElementsByTagName("a");
for (anchor of anchors) {
var filename = anchor.href.split("/").pop();
var type = filename.split(".").pop();
if (this.img_extensions.indexOf(type) != -1) {
this.image_names.push(filename);
}
}
console.log("got image names: ", this.image_names);
if (document.location.hash) {
this.page = parseInt(document.location.hash.substring(1));
}
this.load_page(this.page);
}).bind(this);
req.send();
};
S3ml.prototype.page_count = function () {
return Math.floor(this.image_names.length / this.page_size);
};
S3ml.prototype.add_image = function (href) {
this.dom_element.innerHTML += "<article><a href="+href+"><img src='" +href+ "'></a></article>";
};
S3ml.prototype.clear_page = function () {
this.dom_element.innerHTML = "";
};
S3ml.prototype.next_page_available = function() {
return (this.images_in_page(this.page+1).length > 0);
};
S3ml.prototype.next_page = function () {
if (this.next_page_available()) {
this.page += 1;
this.clear_page();
this.load_page(this.page);
window.location.hash = "#"+this.page;
}
};
S3ml.prototype.previous_page_available = function () {
return (this.page > 0);
}
S3ml.prototype.previous_page = function () {
if (this.previous_page_available()) {
this.page -= 1;
this.clear_page();
this.load_page(this.page);
window.location.hash = "#"+this.page;
}
}