Browse Source

Merge branch 'pr/993'

master
Igor Zhukov 8 years ago
parent
commit
a494c9b407
  1. 8
      Makefile
  2. 1
      app/index.html
  3. 64
      app/js/init.js
  4. 92
      app/js/offline-manager.js
  5. 75
      gulpfile.js
  6. 2
      package.json

8
Makefile

@ -13,11 +13,9 @@ ghdist: @@ -13,11 +13,9 @@ ghdist:
cd dist && git checkout gh-pages
publish:
./node_modules/gulp/bin/gulp.js clean
cd dist && git pull origin gh-pages
./node_modules/gulp/bin/gulp.js publish
./node_modules/gulp/bin/gulp.js build
echo -n "Please open http://localhost:8000/dist/index.html and check if everything works fine." && read -e
cd dist && git add --all . && git commit -am "merged with master" && git push origin gh-pages
./node_modules/gulp/bin/gulp.js deploy
bump:
./node_modules/gulp/bin/gulp.js bump
@ -32,4 +30,4 @@ txupdate: @@ -32,4 +30,4 @@ txupdate:
txupload:
tx pull -f
tx push -s
tx push -s

1
app/index.html

@ -89,6 +89,7 @@ @@ -89,6 +89,7 @@
<script type="text/javascript" src="js/controllers.js"></script>
<script type="text/javascript" src="js/filters.js"></script>
<script type="text/javascript" src="js/messages_manager.js"></script>
<script type="text/javascript" src="js/offline-manager.js"></script>
<!--PRODUCTION_ONLY_BEGIN
<script type="text/javascript" src="js/templates.js"></script>

64
app/js/init.js

@ -1,67 +1,3 @@ @@ -1,67 +1,3 @@
;(function initAutoUpgrade () {
// Prevent click-jacking
try {
if (window == window.top || window.chrome && chrome.app && chrome.app.window) {
document.documentElement.style.display = 'block';
} else {
top.location = self.location;
}
} catch (e) {console.error('CJ protection', e)};
window.safeConfirm = function (params, callback) {
if (typeof params === 'string') {
params = {message: params};
}
var result = false
try {
result = confirm(params.message);
} catch (e) {
result = true;
}
setTimeout(function () {callback(result)}, 10);
};
if (!window.applicationCache || Config.Modes.packed || !window.addEventListener) {
return;
}
var appCache = window.applicationCache,
declined = false,
updateTimeout = false,
scheduleUpdate = function (delay) {
clearTimeout(updateTimeout);
updateTimeout = setTimeout(function () {
try {
appCache.update();
} catch (ex) {
console.log('appCache.update: ' + ex);
}
}, delay || 300000);
},
attach = function () {
appCache.addEventListener('updateready', function (e) {
if (appCache.status == appCache.UPDATEREADY) {
if (!declined) {
safeConfirm({type: 'WEBOGRAM_UPDATED_RELOAD', message: 'A new version of Webogram is downloaded. Launch it?'}, function (result) {
if (result) {
window.location.reload();
} else {
declined = true;
}
});
scheduleUpdate();
}
}
}, false);
appCache.addEventListener('noupdate', function () {scheduleUpdate()}, false);
appCache.addEventListener('error', function () {scheduleUpdate()}, false);
};
scheduleUpdate(3000);
window.addEventListener('load', attach);
})();
(function initApplication () {
var classes = [
Config.Navigator.osX ? 'osx' : 'non_osx',

92
app/js/offline-manager.js

@ -0,0 +1,92 @@ @@ -0,0 +1,92 @@
(function initAutoUpgrade () {
// Prevent click-jacking
try {
if (window == window.top || window.chrome && chrome.app && chrome.app.window) {
document.documentElement.style.display = 'block';
} else {
top.location = self.location;
}
} catch (e) {console.error('CJ protection', e)};
window.safeConfirm = function (params, callback) {
if (typeof params === 'string') {
params = {message: params};
}
var result = false
try {
result = confirm(params.message);
} catch (e) {
result = true;
}
setTimeout(function () {callback(result)}, 10);
};
if ((!navigator.serviceWorker && !window.applicationCache) || Config.Modes.packed || !window.addEventListener) {
return;
}
var declined = false;
function updateFound() {
if (!declined) {
safeConfirm({type: 'WEBOGRAM_UPDATED_RELOAD', message: 'A new version of Webogram is downloaded. Launch it?'}, function (result) {
if (result) {
window.location.reload();
} else {
declined = true;
}
});
}
}
if (navigator.serviceWorker) {
// If available, use a Service Worker to handle offlining.
navigator.serviceWorker.register('offline-worker.js').then(function(registration) {
console.log('offline worker registered');
registration.addEventListener('updatefound', function() {
var installingWorker = this.installing;
// Wait for the new service worker to be installed before prompting to update.
installingWorker.addEventListener('statechange', function() {
switch (installingWorker.state) {
case 'installed':
// Only show the prompt if there is currently a controller so it is not
// shown on first load.
if (navigator.serviceWorker.controller) {
updateFound();
}
break;
case 'redundant':
console.error('The installing service worker became redundant.');
break;
}
});
});
});
} else {
// Otherwise, use AppCache.
var appCache = window.applicationCache,
updateTimeout = false,
scheduleUpdate = function (delay) {
clearTimeout(updateTimeout);
updateTimeout = setTimeout(function () {
try {
appCache.update();
} catch (ex) {
console.log('appCache.update: ' + ex);
}
}, delay || 300000);
};
scheduleUpdate(3000);
window.addEventListener('load', function () {
appCache.addEventListener('updateready', function () {
if (appCache.status == appCache.UPDATEREADY) {
updateFound();
}
}, false);
appCache.addEventListener('noupdate', function () {scheduleUpdate()}, false);
appCache.addEventListener('error', function () {scheduleUpdate()}, false);
});
}
})();

75
gulpfile.js

@ -10,6 +10,8 @@ var st = require('st'); @@ -10,6 +10,8 @@ var st = require('st');
var less = require('gulp-less');
var del = require('del');
var runSequence = require('run-sequence');
var oghliner = require('oghliner');
var gulpServiceWorker = require('gulp-serviceworker');
// The generated file is being created at src
// so it can be fetched by usemin.
@ -23,7 +25,7 @@ gulp.task('templates', function() { @@ -23,7 +25,7 @@ gulp.task('templates', function() {
.pipe(gulp.dest('app/js'));
});
gulp.task('usemin', ['templates', 'enable-production'], function() {
gulp.task('usemin', function() {
return gulp.src(['app/index.html', 'app/badbrowser.html'])
.pipe($.usemin({
html: [$.minifyHtml({empty: true})],
@ -152,37 +154,32 @@ gulp.task('disable-production', function() { @@ -152,37 +154,32 @@ gulp.task('disable-production', function() {
);
});
gulp.task('add-appcache-manifest', function() {
var sources = [
'./dist/**/*',
'!dist/manifest.*',
'!dist/*.html',
'!dist/fonts/*',
'!dist/img/icons/icon*.png',
'!dist/js/background.js'
];
var fileGlobs = [
'./dist/**/*',
'!dist/manifest.*',
'!dist/*.html',
'!dist/fonts/*',
'!dist/img/icons/icon*.png',
'!dist/js/background.js',
];
gulp.task('generate-service-worker', ['build'], function() {
return gulp.src(fileGlobs)
.pipe(gulpServiceWorker({
rootDir: 'dist/',
}));
});
return es.concat(
gulp.src(sources)
.pipe($.manifest({
timestamp: true,
network: ['http://*', 'https://*', '*'],
filename: 'webogram.appcache',
exclude: ['webogram.appcache', 'app.manifest']
})
)
.pipe(gulp.dest('./dist')),
gulp.src(sources)
.pipe($.manifest({
timestamp: true,
network: ['http://*', 'https://*', '*'],
filename: 'app.manifest',
exclude: ['webogram.appcache', 'app.manifest']
})
)
.pipe(gulp.dest('./dist'))
);
gulp.task('add-appcache-manifest', ['build'], function() {
return gulp.src(fileGlobs)
.pipe($.manifest({
timestamp: true,
network: ['http://*', 'https://*', '*'],
filename: 'webogram.appcache',
exclude: ['webogram.appcache', 'app.manifest']
})
)
.pipe(gulp.dest('./dist'));
});
gulp.task('package-dev', function() {
@ -250,9 +247,9 @@ gulp.task('bump', ['update-version-manifests', 'update-version-config'], functio @@ -250,9 +247,9 @@ gulp.task('bump', ['update-version-manifests', 'update-version-config'], functio
gulp.start('update-version-comments');
});
gulp.task('build', function(callback) {
gulp.task('build', ['clean'], function(callback) {
runSequence(
'less',
['less', 'templates', 'enable-production'],
'usemin',
['copy', 'copy-locales', 'copy-images', 'disable-production'],
callback
@ -261,10 +258,12 @@ gulp.task('build', function(callback) { @@ -261,10 +258,12 @@ gulp.task('build', function(callback) {
gulp.task('package', ['cleanup-dist']);
gulp.task('publish', ['build'], function() {
gulp.start('add-appcache-manifest');
});
gulp.task('offline', ['add-appcache-manifest', 'generate-service-worker']);
gulp.task('default', ['clean'], function() {
gulp.start('build');
gulp.task('deploy', ['offline'], function() {
return oghliner.deploy({
rootDir: 'dist/',
});
});
gulp.task('default', ['build']);

2
package.json

@ -57,10 +57,12 @@ @@ -57,10 +57,12 @@
"gulp-ng-annotate": "~0.5.2",
"gulp-replace": "^0.2.0",
"gulp-rev": "^1.1.0",
"gulp-serviceworker": "0.0.3",
"gulp-uglify": "^1.0.2",
"gulp-usemin": "^0.3.11",
"gulp-zip": "^0.1.2",
"http": "0.0.0",
"oghliner": "^1.0.1",
"run-sequence": "^1.0.2",
"st": "^0.5.2"
}

Loading…
Cancel
Save