Commit b6895860 authored by Cerdic's avatar Cerdic

gestion du bouton de download avec mise a jour de l'état pour savoir si on a déjà downloadé

parent 796c34b6
......@@ -82,6 +82,7 @@ function action_api_offline_install_js_dist($force_refresh = false) {
$c = array(
"swUrl" => $config["swUrl"],
"swOptions" => $config["swOptions"],
"cacheName" => $config["cacheName"],
);
}
......
jQuery.fn.checkOfflineStatus = function() {
return this.each(function() {
var me = jQuery(this);
var url = me.attr('href');
console.log('check offline '+url);
caches.match(url).then(
function (response){
if (response && response!==undefined){
me.closest('.offline-download').addClass('offline-ok');
}
}
)
});
};
var offline = {
activeDownloads : function() {
var c = navigator.serviceWorker.controller;
if (typeof c!=="undefined" && c){
jQuery('.offline-download.activable').removeClass('activable').addClass('active');
jQuery('.offline-download.activable')
.removeClass('activable')
.addClass('active')
.find('a').on('click', offline.downloadURLs)
.checkOfflineStatus()
;
}
else {
jQuery('.offline-download.activable').removeClass('activable').addClass('unactive');
}
if (typeof offline.showSWStatut === 'function') {offline.showSWStatut();}
},
downloadURLs: function(event) {
event.preventDefault();
var me = jQuery(this);
var src = me.attr('href');
caches.open(offlineConfig.cacheName).then(function(cache) {
fetch(src).then(function(response) {
// put this in cache then we know this is already downloaded for offline
console.log('ofl-download:got response '+src);
cache.put(src,response.clone());
console.log('ofl-download:cache.put ok');
// src returns a JSON-encoded array of
// resource URLs that a given object depends on
//console.log(response.json());
return response.json().then(function(urls){
console.log('ofl-download:addAll',urls);
cache.addAll(urls);
me.checkOfflineStatus();
});
});
});
},
promptPersistent : function() {
// TODO : https://storage.spec.whatwg.org/
Promise.all([
......
<div class="offline-download[ (#ENV{status,unactive})]">
<a href="#"
<a href="[(#EVAL{_DIR_RACINE}|url_absolue|concat{offline.api.urlsdownload.json}|parametre_url{objet,#ENV{objet}}|parametre_url{id_objet,#ENV{id_objet}})]"
class="btn-link">
[(#CHEMIN{img/travel-32.png}|balise_img)]
<span class="label">[(#ENV{label_active,<:offline:offline_info_telecharger_lecture_offline_active:>}|attribut_html)]</span>
......@@ -10,11 +10,22 @@
</span>
</div>
<style type="text/css">
.offline-download {float: right;margin: 10px;text-align: center}
.offline-download {float: right;margin: 10px;text-align: center;position: relative}
.offline-download .label {display: block;text-align: center;max-width: 150px;font-size: 12px}
.offline-download>a {display: none}
.offline-download>span {display: block}
.offline-download.active>a {display: block}
.offline-download.active>span {display: none}
.offline-download.activable {opacity: 0.5;}
.offline-download.offline-ok:after {
content:'\02714';
font-size: 48px;
font-weight: bold;
line-height: 0.75;
color: #5EC738;
display: block;
position: absolute;
right: 0;
bottom: 0;
}
</style>
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment