1 /**
  2  * Copyright (C) 2014 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 runtime, gui, core*/
 26 
 27 /**
 28  * Drop-in workaround for several bugs/quirks
 29  * found in iOS Safari.
 30  * @constructor
 31  * @implements {core.Destroyable}
 32  * @param {!gui.EventManager} eventManager
 33  */
 34 gui.IOSSafariSupport = function (eventManager) {
 35     "use strict";
 36     var window = runtime.getWindow(),
 37         eventTrap = eventManager.getEventTrap();
 38 
 39     /**
 40      * Mobile browsers tend to automatically scroll to a focussed element
 41      * However we do not want this uncontrollable animated scroll when
 42      * the keyboard is already open. Therefore, we try to guess if it is
 43      * open, and in that case momentarily hide the eventTrap,
 44      * and open it after the next animation frame. This causes the
 45      * keyboard to not know where the event trap is when it is opening,
 46      * thus resulting in no scroll, just like we wanted.
 47      * @return {undefined}
 48      */
 49     function suppressFocusScrollIfKeyboardOpen() {
 50         // This keyboard detection trick seems to work on iOS
 51         if (window.innerHeight !== window.outerHeight) {
 52             eventTrap.style.display = "none";
 53             runtime.requestAnimationFrame(function () {
 54                 eventTrap.style.display = "block";
 55             });
 56         }
 57     }
 58 
 59     /**
 60      * @param {!function(!Error=)} callback
 61      * @return {undefined}
 62      */
 63     this.destroy = function (callback) {
 64         eventManager.unsubscribe('focus', suppressFocusScrollIfKeyboardOpen);
 65         eventTrap.removeAttribute("autocapitalize");
 66         eventTrap.style.WebkitTransform = "";
 67         callback();
 68     };
 69 
 70     function init() {
 71         eventManager.subscribe('focus', suppressFocusScrollIfKeyboardOpen);
 72         // FIXME: This can be removed once we have better/any iOS IME support.
 73         // Till then, we need to avoid typing all-caps because the keyboard
 74         // thinks that the eventTrap is empty and therefore a new line has begun.
 75         eventTrap.setAttribute("autocapitalize", "off");
 76         // On iOS the caret is not hideable even if the text color is made transparent
 77         // and opacity is set to 0. However, we do not have a positional IME
 78         // on that platform, so just CSS-translate it offscreen.
 79         eventTrap.style.WebkitTransform = "translateX(-10000px)";
 80     }
 81     init();
 82 };
 83