├── package.json ├── README.md └── windowManager.js /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "node-webkit-window-manager", 3 | "version": "0.0.3", 4 | "description": "A module for globally managing and maintaining references between all open windows in a node-webkit application.", 5 | "main": "windowManager.js", 6 | "repository": { 7 | "type": "git", 8 | "url": "https://github.com/varunvairavan/node-webkit-window-manager.git" 9 | }, 10 | "bugs": { 11 | "url": "https://github.com/varunvairavan/node-webkit-window-manager/issues" 12 | }, 13 | "homepage": "https://github.com/varunvairavan/node-webkit-window-manager", 14 | "scripts": { 15 | "test": "echo \"Error: no test specified\" && exit 1" 16 | }, 17 | "keywords": [ 18 | "nodewebkit", 19 | "windowmanager", 20 | "window", 21 | "manager", 22 | "node-webkit" 23 | ], 24 | "author": "Varun Vairavan ", 25 | "contributors": [ 26 | "James Mortensen " 27 | ], 28 | "license": "MIT", 29 | "dependencies": { 30 | "jquery": "^3.0.0", 31 | "underscore": "^1.6.0" 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## node-webkit-window-manager 2 | 3 | This is a utility to help manage all windows in a node-webkit application. It gives all open windows a global reference to other windows and allows passing data between windows. 4 | 5 | 6 | ### Example 7 | 8 | #### Instantiate the window manager and store as a global reference. 9 | 10 | ```javascript 11 | 12 | var WindowManager = require('node-webkit-window-manager').windowManager; 13 | var gui = require('nw.gui'); 14 | 15 | global.windowManager = new WindowManager(gui, { 16 | "login": { // the name "key" of the window 17 | page: "html/login.html", // the path to the HTML page. 18 | options: { // Standard nw.gui Window options 19 | frame : true, 20 | toolbar: true, 21 | width: 500, 22 | height: 600, 23 | show: true 24 | } 25 | }, 26 | "dashboard": { 27 | page:"html/dashboard.html", 28 | options: { 29 | frame : true, 30 | toolbar: true, 31 | width: 900, 32 | height: 700, 33 | show: true 34 | } 35 | }, 36 | "index": { 37 | page:"html/index.html", 38 | options: { 39 | frame: true, 40 | toolbar: true, 41 | width: 900, 42 | height: 700, 43 | show: true 44 | } 45 | } 46 | }); 47 | ``` 48 | 49 | #### Open the login page and pass a user object 50 | 51 | ```javascript 52 | var loginWindow = global.windowManager.open('login', { 53 | username: 'johndoe', 54 | auth_token: 'abc123', 55 | location: 'Earth' 56 | }); 57 | ``` 58 | 59 | #### On login.html, get reference to user object 60 | 61 | ```javascript 62 | var loginWindow = global.windowManager.get('login'); 63 | console.log('username is ' + loginWindow.data.username); 64 | console.log('and is located on ' + loginWindow.data.location); 65 | ``` 66 | 67 | #### Close the login window 68 | 69 | ```javascript 70 | global.windowManager.get('login').close(); 71 | ``` 72 | 73 | #### Close all opened windows, except the background page. 74 | 75 | ```javascript 76 | global.windowManager.openWindows.forEach(function(windowRef) { 77 | windowRef.close(); 78 | }); 79 | ``` 80 | 81 | 82 | -------------------------------------------------------------------------------- /windowManager.js: -------------------------------------------------------------------------------- 1 | var $ = require("jquery"); 2 | var _ = require("underscore"); 3 | 4 | 5 | /** 6 | * A reference to all potentially managed windows, open or closed. 7 | */ 8 | var windows = { 9 | "index": { 10 | page:"html/index.html", 11 | options: { 12 | frame: true, 13 | toolbar: true, 14 | width: 900, 15 | height: 700, 16 | show: true 17 | } 18 | } 19 | }; 20 | 21 | 22 | /** 23 | * Instantiate a windowManager object. Note that require('nw.gui') cannot be called inside a Node.js 24 | * module, but it can be passed into the module and stored as a reference. If _windows argument is 25 | * ommitted, this module defaults to managing only the index page located at html/index.html. 26 | * 27 | * @param {nw.gui} gui This is the require('nw.gui') object. 28 | * @param {Object} _windows Optional collection of windows to load into the module to be managed. 29 | */ 30 | function WindowManager(gui, _windows) { 31 | windows = _windows || windows; 32 | this.gui = gui; 33 | this.openWindows = new Array(); 34 | } 35 | 36 | 37 | /** 38 | * Open the managed window identified by "name". 39 | * 40 | * @param {String} name The name of the window to open, identified by the key in the windows object. 41 | * @param {Object} data A collection of data to pass from the calling window or module to the target window. 42 | * @return {Object} Returns a reference to the opened window. 43 | */ 44 | WindowManager.prototype.open = function(name, data) { 45 | var _self = this; 46 | var windowRef = _self.gui.Window.open(windows[name].page,windows[name].options); 47 | windowRef.on('close',function(){ 48 | this.hide(); 49 | _self.openWindows = _.without(_self.openWindows,windowRef); 50 | this.close(true); 51 | this.closed = true; 52 | _self.openWindows = _self.openWindows.filter(function(element) { 53 | return element.closed === undefined; 54 | }); 55 | }); 56 | windowRef.on('load',function() { 57 | windowRef.window.console.warn("MESSAGES AFDAFDAS FDAS F"); 58 | }); 59 | windowRef.name = name; 60 | windowRef.data = data; 61 | _self.openWindows.push(windowRef); 62 | return windowRef; 63 | } 64 | 65 | 66 | /** 67 | * Close the window identified by "name". 68 | * 69 | * @param {String} name The name of a window to close, identified by the key in the windows object. 70 | * @return {Object} Returns a reference to the closed window. 71 | */ 72 | WindowManager.prototype.close = function(name) { 73 | var _self = this; 74 | var windowRef = _.where(_self.openWindows, {name: name}); 75 | if(windowRef === undefined || windowRef.length == 0) throw new Error('The window "' + name + '"" is not open, or an unknown error has occurred.'); 76 | windowRef[0].close(); 77 | return windowRef; 78 | } 79 | 80 | 81 | /** 82 | * Get a reference to the opened window, identified by "name". 83 | * 84 | * @param {String} name The name of a window reference to retrieve, identified by the key in the windows object. 85 | * @return {Object} Returns a reference to the open window. 86 | */ 87 | WindowManager.prototype.get = function(name) { 88 | var _self = this; 89 | var windowRef = _.where(_self.openWindows, {name: name}); 90 | if(windowRef === undefined || windowRef.length == 0) throw new Error('The window "' + name + '"" is not open, or an unknown error has occurred.'); 91 | return windowRef[0]; 92 | } 93 | 94 | exports.windowManager = WindowManager; --------------------------------------------------------------------------------