screenshot.js
4.04 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
* ADOBE CONFIDENTIAL
*
* Copyright 2015 Adobe Systems Incorporated
* All Rights Reserved.
*
* NOTICE: All information contained herein is, and remains
* the property of Adobe Systems Incorporated and its suppliers,
* if any. The intellectual and technical concepts contained
* herein are proprietary to Adobe Systems Incorporated and its
* suppliers and may be covered by U.S. and Foreign Patents,
* patents in process, and are protected by trade secret or copyright law.
* Dissemination of this information or reproduction of this material
* is strictly forbidden unless prior written permission is obtained
* from Adobe Systems Incorporated.
*/
(function(window, document, $, dcc) {
'use strict';
var POLLING_INTERVAL = 1000; // ms
var SCREENSHOT_CLASS = 'screens-DeviceScreenshot';
var SELECTOR_SCREENSHOT = '.screens-dcc-actions-Device-screenshot-activator';
var SELECTOR_FULLSCREEN = '.screens-dcc-actions-Device-fullscreen-activator';
var SELECTOR_DIALOG = '.screens-dcc-actions-DeviceScreenshotDialog';
var ui = $(window).adaptTo('foundation-ui');
function reloadUI() {
var foundationContent = $('.foundation-content').adaptTo('foundation-content');
return foundationContent.refresh();
}
function getTimestamp(html) {
var timestamp = $('.' + SCREENSHOT_CLASS + '-timestamp time', html).attr('datetime');
return timestamp ? new Date(parseInt(timestamp, 10)) : null;
}
function renderImage() {
var $screenshotImg = $('img.' + SCREENSHOT_CLASS + '-image');
if ($screenshotImg.length) {
$screenshotImg.attr('src', $screenshotImg.data('src'));
}
}
function readScreenshot(deviceProfilePath) {
var url = Granite.HTTP.externalize(deviceProfilePath + '/screenshot/jcr:content.json');
return $.ajax(url);
}
function pollScreenshot(deviceProfilePath, timestamp) {
var screenshotPollTimeout;
var deferred = $.Deferred(); // eslint-disable-line new-cap
var handleScreenshot = function() {
var checkScreenshot = function(data) {
var newTimestamp = data['jcr:lastModified'] ? new Date(data['jcr:lastModified']) : null;
if (newTimestamp && (timestamp ? timestamp.getTime() : 0) < newTimestamp.getTime()) {
console.log(timestamp, newTimestamp);
deferred.resolve(newTimestamp);
}
else {
handleScreenshot();
}
};
window.clearTimeout(screenshotPollTimeout);
screenshotPollTimeout = window.setTimeout(function() {
readScreenshot(deviceProfilePath)
.then(checkScreenshot)
.fail(handleScreenshot);
}, POLLING_INTERVAL);
};
handleScreenshot();
return deferred.promise();
}
function updateScreenshot(deviceId) {
ui.wait($('.screens-dashboard-DeviceScreenshotTile').get(0));
dcc.playerCommands.sendCommandToDevice(deviceId, dcc.playerCommands.COMMANDS.SCREENSHOT)
.then(function(deviceData) {
return pollScreenshot(deviceData.deviceProfilePath, getTimestamp());
})
.then(reloadUI);
}
$(document).on('click', SELECTOR_SCREENSHOT, function(ev) {
var $t = $(ev.currentTarget);
var $device = $t.closest('.screens-Device');
var deviceId = $device.find('[data-device-id]').data('deviceId');
if (!deviceId) {
return;
}
updateScreenshot(deviceId);
});
$(document).on('click', SELECTOR_FULLSCREEN, function(ev) {
$(document).on('coral-overlay:beforeopen', SELECTOR_DIALOG, function() {
$('.screens-dcc-actions-DeviceScreenshotDialog coral-dialog-content > div').empty().append($('.screens-DeviceScreenshot-image').clone());
});
});
$(document).on('foundation-contentloaded', function() {
renderImage();
});
}(window, document, Granite.$, window.CQ.screens.dcc));