Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

device-back-button-dispatcher.js 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
  2. import _createClass from 'babel-runtime/helpers/createClass';
  3. /*
  4. Copyright 2013-2015 ASIAL CORPORATION
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. */
  15. import platform from '../platform';
  16. var util = {
  17. _ready: false,
  18. _domContentLoaded: false,
  19. _onDOMContentLoaded: function _onDOMContentLoaded() {
  20. util._domContentLoaded = true;
  21. if (platform.isWebView()) {
  22. window.document.addEventListener('deviceready', function () {
  23. util._ready = true;
  24. }, false);
  25. } else {
  26. util._ready = true;
  27. }
  28. },
  29. addBackButtonListener: function addBackButtonListener(fn) {
  30. if (!this._domContentLoaded) {
  31. throw new Error('This method is available after DOMContentLoaded');
  32. }
  33. if (this._ready) {
  34. window.document.addEventListener('backbutton', fn, false);
  35. } else {
  36. window.document.addEventListener('deviceready', function () {
  37. window.document.addEventListener('backbutton', fn, false);
  38. });
  39. }
  40. },
  41. removeBackButtonListener: function removeBackButtonListener(fn) {
  42. if (!this._domContentLoaded) {
  43. throw new Error('This method is available after DOMContentLoaded');
  44. }
  45. if (this._ready) {
  46. window.document.removeEventListener('backbutton', fn, false);
  47. } else {
  48. window.document.addEventListener('deviceready', function () {
  49. window.document.removeEventListener('backbutton', fn, false);
  50. });
  51. }
  52. }
  53. };
  54. window.addEventListener('DOMContentLoaded', function () {
  55. return util._onDOMContentLoaded();
  56. }, false);
  57. var HandlerRepository = {
  58. _store: {},
  59. _genId: function () {
  60. var i = 0;
  61. return function () {
  62. return i++;
  63. };
  64. }(),
  65. set: function set(element, handler) {
  66. if (element.dataset.deviceBackButtonHandlerId) {
  67. this.remove(element);
  68. }
  69. var id = element.dataset.deviceBackButtonHandlerId = HandlerRepository._genId();
  70. this._store[id] = handler;
  71. },
  72. remove: function remove(element) {
  73. if (element.dataset.deviceBackButtonHandlerId) {
  74. delete this._store[element.dataset.deviceBackButtonHandlerId];
  75. delete element.dataset.deviceBackButtonHandlerId;
  76. }
  77. },
  78. get: function get(element) {
  79. if (!element.dataset.deviceBackButtonHandlerId) {
  80. return undefined;
  81. }
  82. var id = element.dataset.deviceBackButtonHandlerId;
  83. if (!this._store[id]) {
  84. throw new Error();
  85. }
  86. return this._store[id];
  87. },
  88. has: function has(element) {
  89. if (!element.dataset) {
  90. return false;
  91. }
  92. var id = element.dataset.deviceBackButtonHandlerId;
  93. return !!this._store[id];
  94. }
  95. };
  96. var DeviceBackButtonDispatcher = function () {
  97. function DeviceBackButtonDispatcher() {
  98. _classCallCheck(this, DeviceBackButtonDispatcher);
  99. this._isEnabled = false;
  100. this._boundCallback = this._callback.bind(this);
  101. }
  102. /**
  103. * Enable to handle 'backbutton' events.
  104. */
  105. _createClass(DeviceBackButtonDispatcher, [{
  106. key: 'enable',
  107. value: function enable() {
  108. if (!this._isEnabled) {
  109. util.addBackButtonListener(this._boundCallback);
  110. this._isEnabled = true;
  111. }
  112. }
  113. /**
  114. * Disable to handle 'backbutton' events.
  115. */
  116. }, {
  117. key: 'disable',
  118. value: function disable() {
  119. if (this._isEnabled) {
  120. util.removeBackButtonListener(this._boundCallback);
  121. this._isEnabled = false;
  122. }
  123. }
  124. /**
  125. * Fire a 'backbutton' event manually.
  126. */
  127. }, {
  128. key: 'fireDeviceBackButtonEvent',
  129. value: function fireDeviceBackButtonEvent() {
  130. var event = document.createEvent('Event');
  131. event.initEvent('backbutton', true, true);
  132. document.dispatchEvent(event);
  133. }
  134. }, {
  135. key: '_callback',
  136. value: function _callback() {
  137. this._dispatchDeviceBackButtonEvent();
  138. }
  139. /**
  140. * @param {HTMLElement} element
  141. * @param {Function} callback
  142. */
  143. }, {
  144. key: 'createHandler',
  145. value: function createHandler(element, callback) {
  146. if (!(element instanceof HTMLElement)) {
  147. throw new Error('element must be an instance of HTMLElement');
  148. }
  149. if (!(callback instanceof Function)) {
  150. throw new Error('callback must be an instance of Function');
  151. }
  152. var handler = {
  153. _callback: callback,
  154. _element: element,
  155. disable: function disable() {
  156. HandlerRepository.remove(element);
  157. },
  158. setListener: function setListener(callback) {
  159. this._callback = callback;
  160. },
  161. enable: function enable() {
  162. HandlerRepository.set(element, this);
  163. },
  164. isEnabled: function isEnabled() {
  165. return HandlerRepository.get(element) === this;
  166. },
  167. destroy: function destroy() {
  168. HandlerRepository.remove(element);
  169. this._callback = this._element = null;
  170. }
  171. };
  172. handler.enable();
  173. return handler;
  174. }
  175. }, {
  176. key: '_dispatchDeviceBackButtonEvent',
  177. value: function _dispatchDeviceBackButtonEvent() {
  178. var tree = this._captureTree();
  179. var element = this._findHandlerLeafElement(tree);
  180. var handler = HandlerRepository.get(element);
  181. handler._callback(createEvent(element));
  182. function createEvent(element) {
  183. return {
  184. _element: element,
  185. callParentHandler: function callParentHandler() {
  186. var parent = this._element.parentNode;
  187. while (parent) {
  188. handler = HandlerRepository.get(parent);
  189. if (handler) {
  190. return handler._callback(createEvent(parent));
  191. }
  192. parent = parent.parentNode;
  193. }
  194. }
  195. };
  196. }
  197. }
  198. /**
  199. * @return {Object}
  200. */
  201. }, {
  202. key: '_captureTree',
  203. value: function _captureTree() {
  204. return createTree(document.body);
  205. function createTree(element) {
  206. var tree = {
  207. element: element,
  208. children: Array.prototype.concat.apply([], arrayOf(element.children).map(function (childElement) {
  209. if (childElement.style.display === 'none' || childElement._isShown === false) {
  210. return [];
  211. }
  212. if (childElement.children.length === 0 && !HandlerRepository.has(childElement)) {
  213. return [];
  214. }
  215. var result = createTree(childElement);
  216. if (result.children.length === 0 && !HandlerRepository.has(result.element)) {
  217. return [];
  218. }
  219. return [result];
  220. }))
  221. };
  222. if (!HandlerRepository.has(tree.element)) {
  223. for (var i = 0; i < tree.children.length; i++) {
  224. var subTree = tree.children[i];
  225. if (HandlerRepository.has(subTree.element)) {
  226. return subTree;
  227. }
  228. }
  229. }
  230. return tree;
  231. }
  232. function arrayOf(target) {
  233. var result = [];
  234. for (var i = 0; i < target.length; i++) {
  235. result.push(target[i]);
  236. }
  237. return result;
  238. }
  239. }
  240. /**
  241. * @param {Object} tree
  242. * @return {HTMLElement}
  243. */
  244. }, {
  245. key: '_findHandlerLeafElement',
  246. value: function _findHandlerLeafElement(tree) {
  247. return find(tree);
  248. function find(node) {
  249. if (node.children.length === 0) {
  250. return node.element;
  251. }
  252. if (node.children.length === 1) {
  253. return find(node.children[0]);
  254. }
  255. return node.children.map(function (childNode) {
  256. return childNode.element;
  257. }).reduce(function (left, right) {
  258. if (!left) {
  259. return right;
  260. }
  261. var leftZ = parseInt(window.getComputedStyle(left, '').zIndex, 10);
  262. var rightZ = parseInt(window.getComputedStyle(right, '').zIndex, 10);
  263. if (!isNaN(leftZ) && !isNaN(rightZ)) {
  264. return leftZ > rightZ ? left : right;
  265. }
  266. throw new Error('Capturing backbutton-handler is failure.');
  267. }, null);
  268. }
  269. }
  270. }]);
  271. return DeviceBackButtonDispatcher;
  272. }();
  273. export default new DeviceBackButtonDispatcher();