1 /**
  2  * Copyright (C) 2012 KO GmbH <copyright@kogmbh.com>
  3  *
  4  * @licstart
  5  * This file is part of WebODF.
  6  *
  7  * WebODF is free software: you can redistribute it and/or modify it
  8  * under the terms of the GNU Affero General Public License (GNU AGPL)
  9  * as published by the Free Software Foundation, either version 3 of
 10  * the License, or (at your option) any later version.
 11  *
 12  * WebODF is distributed in the hope that it will be useful, but
 13  * WITHOUT ANY WARRANTY; without even the implied warranty of
 14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 15  * GNU Affero General Public License for more details.
 16  *
 17  * You should have received a copy of the GNU Affero General Public License
 18  * along with WebODF.  If not, see <http://www.gnu.org/licenses/>.
 19  * @licend
 20  *
 21  * @source: http://www.webodf.org/
 22  * @source: https://github.com/kogmbh/WebODF/
 23  */
 24 
 25 /*global gui*/
 26 
 27 /**
 28  * The avatar is a passive element that can be displayed above an element.
 29  * It will always keep a relative distance to that element, so automatically
 30  * move around with the parent element.
 31  * @constructor
 32  * @implements {core.Destroyable}
 33  * @param {!Element} parentElement
 34  * @param {boolean} avatarInitiallyVisible Sets the initial visibility of the avatar
 35  */
 36 gui.Avatar = function Avatar(parentElement, avatarInitiallyVisible) {
 37     "use strict";
 38     var self = this,
 39         /**@type{!HTMLDivElement}*/
 40         handle,
 41         /**@type{!HTMLImageElement}*/
 42         image,
 43         pendingImageUrl,
 44         displayShown = "block",
 45         displayHidden = "none";
 46 
 47     /**
 48      * @param {!string} color
 49      */
 50     this.setColor = function (color) {
 51         image.style.borderColor = color;
 52     };
 53     /**
 54      * @param {!string} url
 55      */
 56     this.setImageUrl = function (url) {
 57         if (self.isVisible()) {
 58             image.src = url;
 59         } else {
 60             // Delay loading of the associated image until the avatar is displayed
 61             pendingImageUrl = url;
 62         }
 63     };
 64     /**
 65      * @return {boolean}
 66      */
 67     this.isVisible = function () {
 68         return (handle.style.display === displayShown);
 69     };
 70     /**
 71      * @return {undefined}
 72      */
 73     this.show = function () {
 74         if (pendingImageUrl) {
 75             image.src = pendingImageUrl;
 76             pendingImageUrl = undefined;
 77         }
 78         handle.style.display = displayShown;
 79     };
 80     /**
 81      * @return {undefined}
 82      */
 83     this.hide = function () {
 84         handle.style.display = displayHidden;
 85     };
 86     /**
 87      * @param {boolean} isFocussed
 88      * @return {undefined}
 89      */
 90     this.markAsFocussed = function (isFocussed) {
 91         if (isFocussed) {
 92             handle.classList.add("active");
 93         } else {
 94             handle.classList.remove("active");
 95         }
 96     };
 97 
 98     /**
 99      * @param {!function(!Error=)} callback, passing an error object in case of error
100      * @return {undefined}
101      */
102     this.destroy = function (callback) {
103         parentElement.removeChild(handle);
104         callback();
105     };
106 
107     function init() {
108         var document = /**@type{!Document}*/(parentElement.ownerDocument);
109 
110         handle = /**@type{!HTMLDivElement}*/(document.createElement("div"));
111         image = /**@type{!HTMLImageElement}*/(document.createElement("img"));
112         handle.appendChild(image);
113         handle.style.display = avatarInitiallyVisible ? displayShown : displayHidden;
114         handle.className = "handle";
115         parentElement.appendChild(handle);
116     }
117 
118     init();
119 };
120