Skip to content

Instantly share code, notes, and snippets.

@kevyworks
Created March 30, 2014 18:42
Show Gist options
  • Save kevyworks/9877531 to your computer and use it in GitHub Desktop.
Save kevyworks/9877531 to your computer and use it in GitHub Desktop.
CNativeWindow
/*jslint esnext: true, vars: true, plusplus: true, regexp: true, devel: true, nomen: true, indent: 2, maxerr: 50 */
/*global require, namespace, window, global, App, WND_SHELL */
namespace("App.ux.CNativeWindow", (function () {
"use strict";
// variables
//
var _ = require("lodash");
var jsClass = require("js-class");
var nwgui = require("nw.gui");
var wcount = 1;
/**
* App.ux.CNativeWindow
* @constructor
*/
return jsClass({
/**
* Constructor
* @param {object} config
* @return {void}
*/
constructor: function (config) {
var self = this;
this._config = config;
_.defaults(this._config, {
title: "Untitled " + (wcount++),
position: "center",
icon: process.cwd() + "/app/res/img/icon.png",
page: App.Config.app_url + "html/blank.html",
show: false,
toolbar: false,
focus: true,
frame: true,
modal: false,
minimizable: true,
closable: true,
autoShow: true,
width: 200,
height: 200
});
// force not to show directly
_.assign(this._config, {
show: false,
show_in_taskbar: !this._config.modal
});
this._win = nwgui.Window.open(this._config.page, _.omit(this._config, ["page", "closable", "modal", "minimizable", "parent"]));
this._win.Config = this._config;
this._win.isModal = this._config.modal;
this._win.on("loaded", function () {
if (self._config.autoShow === true) {
this.show();
}
});
this._win.on("close", function () {
if (self._config.closable === false) return false;
this.hide();
this.close(true);
});
this._win.on("closed", function () {
if (App.ux.CNativeWindow.countModalWindows() > 0) {
App.ux.CNativeWindow.focusToModalWindow();
}
});
if (this._config.modal === true) {
App.ux.Overlay.show(true);
this._win.on("focus", function () {
App.ux.CNativeWindow.focusToModalWindow();
});
}
},
get shell() {
return this._win;
}
}, {
// --------------------------------------------------------------
// STATIC METHODS/OBJECTS
// --------------------------------------------------------------
statics: {
/**
* Counts all modal windows
* @return {integer}
*/
countModalWindows: function () {
return App.ux.CNativeWindow.getModalWindows().length;
},
/**
* Returns Main Shell Window
* @return {object} NodeWebkit Window Object
*/
getMainShell: function () {
return _.where(global.__nwWindowsStore, {
isMainShell: true
})[0] || null;
},
/**
* Get All Modal Windows
* @return {array} NodeWebkit Window Objects
*/
getModalWindows: function () {
return _.where(global.__nwWindowsStore, {
isModal: true
});
},
/**
* Focus to Last Modal Window
* @return {void}
*/
focusToModalWindow: function () {
var windows = App.ux.CNativeWindow.getModalWindows();
if (windows.length > 0) {
var win = windows[windows.length - 1];
win.show();
win.focus();
}
},
/**
* Setup MainShell
* @return {void}
*/
setupMainShell: function () {
WND_SHELL.isMainShell = true;
WND_SHELL.__defineGetter__("isFocused", function () {
return WND_SHELL._isFocused;
});
WND_SHELL.__defineSetter__("isFocused", function (value) {
var modalCount = App.ux.CNativeWindow.countModalWindows();
if (modalCount > 0) {
App.ux.CNativeWindow.focusToModalWindow();
}
// CSS
WND_SHELL._isFocused = value;
$("body")[value ? "removeClass" : "addClass"]("lostFocus");
//App.ux.Overlay.show(value ? false : true);
App.ux.Overlay.show(!value);
});
var windowFocusHandler = function () {
WND_SHELL.isFocused = true;
};
var windowBlurHandler = function () {
WND_SHELL.isFocused = false;
};
//WND_SHELL.on("focus", windowFocusHandler);
//WND_SHELL.on("blur", windowBlurHandler);
WND_SHELL.window.addEventListener("focus", windowFocusHandler);
WND_SHELL.window.addEventListener("blur", windowBlurHandler);
}
}
});
}()));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment