Coming back to the file-explorer, we can start with the TitleBarActions module that listens to user click events on title bar buttons and performs the corresponding windowing action. First, we need to mark the action nodes in HTML. The ./index.html file contains the following code:
<header class="l-app__titlebar titlebar" data-bind="titlebar">
...
<a class="titlebar__btn" data-bind="close" > ;</a>
</header>
Here, we specify our bounding box (data-bind="titlebar") and the close window button (data-bind="close"). Let's begin with the only button. The ./js/View/TitleBarActions.js file contains the following code:
class TitleBarActionsView {
constructor( boundingEl ){
this.closeEl = boundingEl.querySelector( "[data-bind=close]" );
this.bindUi();
}
bindUi(){
this.closeEl.addEventListener( "click", this.onClose.bind( this ), false );
}
onClose( e ) {
e.preventDefault();
nw.Window.get().close();
}
}
exports.TitleBarActionsView = TitleBarActionsView;
Here, we define a TitleBarActionView class that accepts an HTML element as a parameter. This element represents the view bounding box, meaning that the instance of this class will take care only of the passed in element and its descendants. During construction, the class will search for the first element in the scope of the bounding box that matches selector [data-bind=close]--the close window button of the title bar. In the bindUI method, we subscribe for clicks events on the Close button. When the button is clicked, the onClose method is called in the context of a TitleBarActionView instance, as we bound it in bindUi (this.onClose.bind( this )). The onClose method closes the window using the NW.js Window API (http://docs.nwjs.io/en/latest/References/Window/), namely it requests a current window object nw.Window.get() and calls its close method.
NW.js doesn't provide a module for the API, but exposes the nw variable in the global scope.
So, we have the first view module and can use it the main script:
./js/app.js
const { TitleBarActionsView } = require( "./js/View/TitleBarActions" );
new TitleBarActionsView( document.querySelector( "[data-bind=titlebar]" ) );
Here, we import the TileBarActionView class from the ./js/View/TitleBarActions module and make an instance of it. We pass the first document element matching selector [data-bind=titlebar] to the class constructor.
Have you noticed that we used destructuring while importing from the module? Particularly, we extracted the TitleBarActionsView class into a respectively called constant.
Now, we can launch the application and observe, as clicking on the close button really closes the window.
Going further, we take care of other title bar buttons. So, we adapt our index.html file to identify the buttons, nodes with unmaximize, maximize, and minimize values for the data-bind attribute. Then, we collect in the TileBarActionView constructor references to the corresponding HTML elements:
this.unmaximizeEl = boundingEl.querySelector( "[data-bind=unmaximize]" );
this.maximizeEl = boundingEl.querySelector( "[data-bind=maximize]" );
this.minimizeEl = boundingEl.querySelector( "[data-bind=minimize]" );
Of course, we have to add new listeners in the bindUi module, respectively:
this.minimizeEl.addEventListener( "click", this.onMinimize.bind( this ), false );
this.maximizeEl.addEventListener( "click", this.onMaximize.bind( this ), false );
this.unmaximizeEl.addEventListener( "click", this.onUnmaximize.bind( this ), false );
The handler for minimizing the window button looks pretty much the same as the one we have already examined previously. It just uses the corresponding method of the NW.js Window API:
onMinimize( e ) {
e.preventDefault();
nw.Window.get().minimize();
}
With maximize and minimize (restore) window buttons, we need to take the fact that while one button is visible the second one shall be hidden into account. This we achieve with the toggleMaximize method:
toggleMaximize(){
this.maximizeEl.classList.toggle( "is-hidden" );
this.unmaximizeEl.classList.toggle( "is-hidden" );
}
Event handler for these buttons calls this method to the toggle buttons view:
onUnmaximize( e ) {
e.preventDefault();
nw.Window.get().unmaximize();
this.toggleMaximize();
}
onMaximize( e ) {
e.preventDefault();
nw.Window.get().maximize();
this.toggleMaximize();
}