1/** 2 * Copyright © Magento, Inc. All rights reserved. 3 * See COPYING.txt for license details. 4 */ 5/** Creates outerClick binding and registers in to ko.bindingHandlers object */ 6define([ 7 'ko', 8 'jquery', 9 'underscore', 10 '../template/renderer' 11], function (ko, $, _, renderer) { 12 'use strict'; 13 14 var defaults = { 15 onlyIfVisible: true 16 }; 17 18 /** 19 * Checks if element sis visible. 20 * 21 * @param {Element} el 22 * @returns {Boolean} 23 */ 24 function isVisible(el) { 25 var style = window.getComputedStyle(el), 26 visibility = { 27 display: 'none', 28 visibility: 'hidden', 29 opacity: '0' 30 }, 31 visible = true; 32 33 _.each(visibility, function (val, key) { 34 if (style[key] === val) { 35 visible = false; 36 } 37 }); 38 39 return visible; 40 } 41 42 /** 43 * Document click handler which in case if event target is not 44 * a descendant of provided container element, 45 * invokes specified in configuration callback. 46 * 47 * @param {HTMLElement} container 48 * @param {Object} config 49 * @param {EventObject} e 50 */ 51 function onOuterClick(container, config, e) { 52 var target = e.target, 53 callback = config.callback; 54 55 if (container === target || container.contains(target)) { 56 return; 57 } 58 59 if (config.onlyIfVisible) { 60 if (!_.isNull(container.offsetParent) && isVisible(container)) { 61 callback(); 62 } 63 } else { 64 callback(); 65 } 66 } 67 68 /** 69 * Prepares configuration for the binding based 70 * on a default properties and provided options. 71 * 72 * @param {(Object|Function)} [options={}] 73 * @returns {Object} 74 */ 75 function buildConfig(options) { 76 var config = {}; 77 78 if (_.isFunction(options)) { 79 options = { 80 callback: options 81 }; 82 } else if (!_.isObject(options)) { 83 options = {}; 84 } 85 86 return _.extend(config, defaults, options); 87 } 88 89 ko.bindingHandlers.outerClick = { 90 91 /** 92 * Initializes outer click binding. 93 */ 94 init: function (element, valueAccessor) { 95 var config = buildConfig(valueAccessor()), 96 outerClick = onOuterClick.bind(null, element, config), 97 isTouchDevice = typeof document.ontouchstart !== 'undefined'; 98 99 if (isTouchDevice) { 100 $(document).on('touchstart', outerClick); 101 102 ko.utils.domNodeDisposal.addDisposeCallback(element, function () { 103 $(document).off('touchstart', outerClick); 104 }); 105 } else { 106 $(document).on('click', outerClick); 107 108 ko.utils.domNodeDisposal.addDisposeCallback(element, function () { 109 $(document).off('click', outerClick); 110 }); 111 } 112 } 113 }; 114 115 renderer.addAttribute('outerClick'); 116});
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.