Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Select events by using mobile(#26) #27

Merged
merged 3 commits into from
Mar 26, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 13 additions & 15 deletions src/js/setTouchClickEvent.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,17 @@

'use strict';

var snippet = require('tui-code-snippet');

var $ = require('jquery');

/**
* Detect mobile browser
* @private
* @returns {boolean} Whether using Mobile browser
*/
function isMobile() {
return /Android|BlackBerry|iPhone|iPad|iPod|Opera Mini|IEMobile|WPDesktop/i.test(navigator.userAgent);
}

/**
* For using one - Touch or Mouse Events
* @param {jQuery|string|Element} target - Target element
Expand All @@ -19,25 +26,16 @@ var $ = require('jquery');
*/
module.exports = function(target, handler, option) {
var $target = $(target);
var eventList = ['touchend', 'click'];
var selector, namespace, events;
var eventType = isMobile() ? 'touchend' : 'click';
var selector, namespace;

option = option || {};
selector = option.selector || null;
namespace = option.namespace || '';

if (namespace) {
eventList = snippet.map(eventList, function(eventName) {
return eventName + '.' + namespace;
});
eventType = eventType + '.' + namespace;
}

events = eventList.join(' ');
$target.on(events, selector, function onceHandler(ev) {
var newEventName = ev.type + '.' + namespace;

handler(ev);
$target.off(events, selector, onceHandler)
.on(newEventName, selector, handler);
});
$target.on(eventType, selector, handler);
};