Permalink
Browse files

initial commit

  • Loading branch information...
jprichardson committed May 22, 2015
0 parents commit bd5735e43282035e9738333fbffc84fb37c7a627
Showing with 183 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +3 −0 CHANGELOG.md
  3. +64 −0 README.md
  4. +85 −0 index.js
  5. +30 −0 package.json
@@ -0,0 +1 @@
node_modules/
@@ -0,0 +1,3 @@
0.1.0 / 2015-05-22
------------------
- initial release
@@ -0,0 +1,64 @@
electron-window
===============
Convenience methods for Electron windows.
Installation
------------
npm i --save electron-window
Usage
-----
### API
#### createWindow(options)
Class method that creates a new [BrowserWindow](https://github.com/atom/electron/blob/master/docs/api/browser-window.md) with
the following default `options`: `{show: false, resizable: false, frame: true}`. No need to worry about keeping a global reference
to prevent garbage collection, this is handled for you.
#### showUrl(httpOrFileUrl, [argsForRenderer], callback)
Instance method that shows the url. When the url is finished loading, the callback is returned. If the optional `argsForRenderer` is set
then `__args__` will be a global object for the page in the renderer process. This is a convenient way to pass
arguments from the main process to the renderer process.
#### unref()
Instance method to call if you ever want to remove the global reference. Should only need to be called if
`[destroy()](https://github.com/atom/electron/blob/master/docs/api/browser-window.md#browserwindowdestroy)` is ever called.
Most likely, you won't need to use this.
### Example
```js
var window = require('electron-window')
var mainWindow = window.createWindow({width: 1000, height: 400})
// can access at window.__args__ from scripts
// ran from index.html
var args = {
data: 'some secret data'
}
mainWindow.showUrl('index.html', args, function() {
console.log('the window should be showing with the contents of the URL now')
})
```
License
-------
MIT
@@ -0,0 +1,85 @@
var assert = require('assert')
var path = require('path')
var url = require('url')
var BrowserWindow = require('browser-window')
// retain global references, if not, window will be closed automatically when
// garbage collected
var _windows = {}
function createWindow (options) {
var window = new BrowserWindow(extend({
show: false,
resizable: false,
frame: true
}, options))
_windows[window.id] = window
// should not need to be called, but just in case
// window.destroy() is ever called
window.unref = function () {
delete _windows[window.id]
}
window.once('closed', function () {
window.unref()
})
window.showUrl = function (httpOrFileUrl, args, callback) {
if (typeof args === 'function') {
callback = args
args = null
}
assert.strictEqual(typeof args, 'object', 'args must be an object')
// stringify the args
args = args ? encodeURIComponent(JSON.stringify(args)) : ''
window.webContents.once('did-finish-load', callback)
window.webContents.once('did-start-loading', function () {
window.webContents.executeJavaScript('(' + parseArgs.toString() + ')()')
})
if (httpOrFileUrl.indexOf('http') === 0) {
var urlData = url.parse(httpOrFileUrl)
var httpUrl = url.format(extend(urlData, {
hash: urlData.hash || args ? args : undefined
}))
window.loadUrl(httpUrl)
} else { // presumably a file url
var fileUrl = url.format({
protocol: 'file',
pathname: path.resolve(httpOrFileUrl),
slashes: true,
hash: args ? args : undefined
})
window.loadUrl(fileUrl)
window.show()
}
}
return window
}
function extend (target, source) {
Object.keys(source).forEach(function (key) {
target[key] = source[key]
})
return target
}
// executed in context of window renderer
function parseArgs () {
if (!window.location.hash) {
window.__args__ = {}
} else {
var hash = window.location.hash.slice(1)
window.__args__ = Object.freeze(JSON.parse(decodeURIComponent(hash)))
}
}
module.exports = {
createWindow: createWindow
}
@@ -0,0 +1,30 @@
{
"name": "electron-window",
"version": "0.1.0",
"description": "Convenience methods for Electron windows.",
"main": "index.js",
"scripts": {
"test": "standard"
},
"repository": {
"type": "git",
"url": "https://github.com/jprichardson/elecron-window"
},
"keywords": [
"window",
"atom",
"atom-shell",
"electron",
"gui",
"electron-component"
],
"author": "JP Richardson",
"license": "MIT",
"bugs": {
"url": "https://github.com/jprichardson/elecron-window/issues"
},
"homepage": "https://github.com/jprichardson/elecron-window",
"devDependencies": {
"standard": "^3.11.0"
}
}

0 comments on commit bd5735e

Please sign in to comment.