Repository navigation
Expand file tree
/
Copy pathpick.js
More file actions
94 lines (81 loc) · 2.72 KB
/
Copy pathpick.js
File metadata and controls
94 lines (81 loc) · 2.72 KB
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
var Wallpaper = {
wallpapersUrl: '/resources/320x480/list.json',
init: function wallpaper_init() {
var self = this;
if (navigator.mozSetMessageHandler) {
navigator.mozSetMessageHandler('activity', function handler(request) {
var activityName = request.source.name;
if (activityName !== 'pick')
return;
self.startPick(request);
});
}
this.cancelButton = document.getElementById('cancel');
this.wallpapers = document.getElementById('wallpapers');
this.generateWallpaperList();
},
generateWallpaperList: function wallpaper_generateWallpaperList(cb) {
var xhr = new XMLHttpRequest();
xhr.open('GET', this.wallpapersUrl, true);
xhr.responseType = 'json';
xhr.send(null);
var self = this;
xhr. successGenerateWallpaperList() {
self.wallpapers.innerHTML = '';
xhr.response.forEach(function(wallpaper) {
var div = document.createElement('div');
div.classList.add('wallpaper');
div.style.backgroundImage = 'url(resources/320x480/' + wallpaper + ')';
self.wallpapers.appendChild(div);
});
if (cb) {
cb();
}
};
},
startPick: function wallpaper_startPick(request) {
this.pickActivity = request;
this.wallpapers.addEventListener('click', this.pickWallpaper.bind(this));
this.cancelButton.addEventListener('click', this.cancelPick.bind(this));
},
pickWallpaper: function wallpaper_pickWallpaper(e) {
// Identify the wallpaper
var backgroundImage = e.target.style.backgroundImage;
var src = backgroundImage.match(/url\([\"']?([^\s\"']*)[\"']?\)/)[1];
// Ignore clicks that are not on one of the images
if (src == '')
return;
if (!this.pickActivity) { return; }
var img = new Image();
img.src = src;
var self = this;
img. {
var canvas = document.createElement('canvas');
var context = canvas.getContext('2d');
canvas.width = img.width;
canvas.height = img.height;
context.drawImage(img, 0, 0);
canvas.toBlob(function(blob) {
self.pickActivity.postResult({
type: 'image/jpeg',
blob: blob,
name: src
}, 'image/jpeg');
self.endPick();
}, 'image/jpeg');
};
},
cancelPick: function wallpaper_cancelPick() {
this.pickActivity.postError('cancelled');
this.endPick();
},
endPick: function wallpaper_endPick() {
this.pickActivity = null;
this.cancelButton.removeEventListener('click', this.cancelPick);
this.wallpapers.removeEventListener('click', this.pickWallpaper);
}
};
window.addEventListener('load', function pick() {
window.removeEventListener('load', pick);
Wallpaper.init();
});