From 054746a4531b7497320451b329d8cea40256dbf1 Mon Sep 17 00:00:00 2001 From: Oz Date: Fri, 30 Oct 2015 00:28:36 +0200 Subject: [PATCH] first release --- .gitignore | 3 +++ gulpfile.js | 34 ++++++++++++++++++++++++++++++++++ package.json | 30 ++++++++++++++++++++++++++++++ src/external-link.js | 12 ++++++++++++ src/process.js | 1 + src/remote.js | 37 +++++++++++++++++++++++++++++++++++++ src/rendererModules.js | 9 +++++++++ src/safeShutdown.js | 37 +++++++++++++++++++++++++++++++++++++ 8 files changed, 163 insertions(+) create mode 100644 .gitignore create mode 100644 gulpfile.js create mode 100644 package.json create mode 100644 src/external-link.js create mode 100644 src/process.js create mode 100644 src/remote.js create mode 100644 src/rendererModules.js create mode 100644 src/safeShutdown.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..69192d0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +node_modules/ + +dist/ diff --git a/gulpfile.js b/gulpfile.js new file mode 100644 index 0000000..35c80b4 --- /dev/null +++ b/gulpfile.js @@ -0,0 +1,34 @@ +var del = require('del'), + gulp = require('gulp'), + concat = require('gulp-concat'), + insert = require('gulp-insert'), + uglify = require('gulp-uglify'); + +var HEADER = "define(['angular'], function(angular) {\n" + + "'use strict';\n\n" + + "angular.module('angular-electron', []);\n\n"; +var FOOTER = "\n});"; + +gulp.task('clean', function () { + return del(['dist']); +}); + +gulp.task('build:dev', ['clean'], function () { + return gulp.src('./src/*') + .pipe(concat('angular-electron.js')) + .pipe(insert.prepend(HEADER)) + .pipe(insert.append(FOOTER)) + .pipe(gulp.dest('./dist/')); +}); + +gulp.task('build:prod', ['clean'], function () { + return gulp.src('./src/*') + .pipe(concat('angular-electron.min.js')) + .pipe(insert.prepend(HEADER)) + .pipe(insert.append(FOOTER)) + .pipe(uglify()) + .pipe(gulp.dest('./dist/')); +}); + +gulp.task('build', ['build:dev', 'build:prod'], function() { +}); diff --git a/package.json b/package.json new file mode 100644 index 0000000..4929f24 --- /dev/null +++ b/package.json @@ -0,0 +1,30 @@ +{ + "name": "angular-electron", + "version": "0.1.0", + "description": "angularjs helpers for electron apps", + "repository": { + "type": "git", + "url": "git+https://github.com/ozsay/angular-electron.git" + }, + "keywords": [ + "angular", + "angularjs", + "electron" + ], + "author": "Oz Sayag", + "license": "MIT", + "bugs": { + "url": "https://github.com/ozsay/angular-electron/issues" + }, + "homepage": "https://github.com/ozsay/angular-electron", + "dependencies": { + "angular": "^1.2.29" + }, + "devDependencies": { + "del": "^2.0.2", + "gulp": "^3.9.0", + "gulp-concat": "^2.6.0", + "gulp-insert": "^0.5.0", + "gulp-uglify": "^1.4.2" + } +} diff --git a/src/external-link.js b/src/external-link.js new file mode 100644 index 0000000..2995fdc --- /dev/null +++ b/src/external-link.js @@ -0,0 +1,12 @@ +angular.module('angular-electron').directive('externalLink', ['shell', function (shell) { + return { + restrict: 'A', + link: function(scope, elem, attrs) { + elem.on('click', function(event) { + event.preventDefault(); + + shell.openExternal(attrs.href || attrs.externalLink); + }); + } + }; +}]); diff --git a/src/process.js b/src/process.js new file mode 100644 index 0000000..e43575a --- /dev/null +++ b/src/process.js @@ -0,0 +1 @@ +angular.module('angular-electron').constant('process', process); diff --git a/src/remote.js b/src/remote.js new file mode 100644 index 0000000..ce54659 --- /dev/null +++ b/src/remote.js @@ -0,0 +1,37 @@ +var remoteModules = ['app', 'auto-updater', 'browser-window', 'content-tracing', 'dialog', + 'global-shortcut', 'menu', 'menu-item', 'power-save-blocker', + 'protocol', 'web-contents', 'tray']; +var nodeModules = ['buffer', 'child_process', 'cluster', 'crypto', 'dns', 'events', 'fs', 'http', + 'https', 'net', 'os', 'path', 'punycode', 'querystring', 'readline', 'stream', + 'string_decoder', 'tls', 'dgram', 'url', 'util', 'v8', 'vm', 'zlib']; + +angular.module('angular-electron').provider('remote', ['$provide', function($provide) { + var remote = require('remote'); + + function register(name, _require) { + _require = _require || name; + + $provide.service(name, function() { + var __module = remote.require(_require); + + return __module; + }); + } + + this.register = register; + + this.$get = [function() { + return remote; + }]; + + $provide.constant('remoteProcess', remote.process); + $provide.constant('currentWindow', remote.getCurrentWindow()); + + angular.forEach(remoteModules, function(remoteModule) { + register(remoteModule.name || remoteModule, remoteModule.require); + }); + + angular.forEach(nodeModules, function(nodeModule) { + register(nodeModule.name || nodeModule, nodeModule.require); + }); +}]); diff --git a/src/rendererModules.js b/src/rendererModules.js new file mode 100644 index 0000000..a31a2a6 --- /dev/null +++ b/src/rendererModules.js @@ -0,0 +1,9 @@ +var wrapModules = ['ipc', 'web-frame', 'clipboard', 'crash-reporter', 'native-image', 'screen', 'shell']; + +angular.forEach(wrapModules, function (_module) { + angular.module('angular-electron').service(_module.name || _module, [function() { + var __module = require(_module.require || _module); + + return __module; + }]); +}); diff --git a/src/safeShutdown.js b/src/safeShutdown.js new file mode 100644 index 0000000..15855d4 --- /dev/null +++ b/src/safeShutdown.js @@ -0,0 +1,37 @@ +angular.module('angular-electron').service('safeShutdown', ['$q', 'currentWindow', 'app', function($q, currentWindow, app) { + var actions = []; + + function register(fn) { + this.actions.push(fn); + } + + function exec() { + var promises = []; + + angular.forEach(this.actions, function (action) { + var res = action(); + + if (res !== undefined && res.then !== undefined) { + promises.push(res); + } + }); + + return $q.all(promises); + } + + currentWindow.safeReload = function() { + exec().then(function() { + currentWindow.reload(); + }); + }; + + app.safeQuit = function() { + exec().then(function() { + app.quit(); + }); + }; + + return { + register: register + }; +}]);