1// Copyright 2013 The Chromium Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5// 6// <window-controls> shadow element implementation. 7// 8 9var chrome = requireNative('chrome').GetChrome(); 10var forEach = require('utils').forEach; 11var addTagWatcher = require('tagWatcher').addTagWatcher; 12var appWindow = require('app.window'); 13var getHtmlTemplate = 14 requireNative('app_window_natives').GetWindowControlsHtmlTemplate; 15 16/** 17 * @constructor 18 */ 19function WindowControls(node) { 20 this.node_ = node; 21 this.shadowRoot_ = this.createShadowRoot_(node); 22 this.setupWindowControls_(); 23} 24 25/** 26 * @private 27 */ 28WindowControls.prototype.template_element = null; 29 30/** 31 * @private 32 */ 33WindowControls.prototype.createShadowRoot_ = function(node) { 34 // Initialize |template| from HTML template resource and cache result. 35 var template = WindowControls.prototype.template_element; 36 if (!template) { 37 var element = document.createElement('div'); 38 element.innerHTML = getHtmlTemplate(); 39 WindowControls.prototype.template_element = element.firstChild; 40 template = WindowControls.prototype.template_element; 41 } 42 // Create shadow root element with template clone as first child. 43 var shadowRoot = node.createShadowRoot(); 44 shadowRoot.appendChild(template.content.cloneNode(true)); 45 return shadowRoot; 46} 47 48/** 49 * @private 50 */ 51WindowControls.prototype.setupWindowControls_ = function() { 52 var self = this; 53 this.shadowRoot_.querySelector("#close-control").addEventListener('click', 54 function(e) { 55 chrome.app.window.current().close(); 56 }); 57 58 this.shadowRoot_.querySelector("#maximize-control").addEventListener('click', 59 function(e) { 60 self.maxRestore_(); 61 }); 62} 63 64/** 65 * @private 66 * Restore or maximize depending on current state 67 */ 68WindowControls.prototype.maxRestore_ = function() { 69 if (chrome.app.window.current().isMaximized()) { 70 chrome.app.window.current().restore(); 71 } else { 72 chrome.app.window.current().maximize(); 73 } 74} 75 76addTagWatcher('WINDOW-CONTROLS', function(addedNode) { 77 new WindowControls(addedNode); 78}); 79