├── .babelrc
├── .gitignore
├── .npmignore
├── LICENSE
├── README.md
├── dist
└── clickHelper.js
├── package.json
└── src
└── clickHelper.js
/.babelrc:
--------------------------------------------------------------------------------
1 | {
2 | "presets": ["es2015", "stage-2"]
3 | }
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | .idea/
2 |
--------------------------------------------------------------------------------
/.npmignore:
--------------------------------------------------------------------------------
1 | .idea/
2 |
3 | node_modules
4 |
5 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2017 Huang ShuWei
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # vue-click-helper
2 |
3 | Vue2.x directive to handle click event and dblclick event on same element
4 |
5 | ## Introduction
6 |
7 | By default, two click interval of less than 300 ms will execute double click event, otherwise click events will execute click event
8 |
9 |
10 | # Installation
11 | ```
12 | npm i vue-click-helper -D
13 | ```
14 |
15 | # Basic Usage
16 |
17 | ```html
18 |
19 | click me
20 |
21 |
22 | ```
23 |
24 | ```javascript
25 | import Vue from 'vue'
26 | import vueClickHelper from 'vue-click-helper'
27 |
28 | export default{
29 | directives: {
30 | 'click-helper': vueClickHelper
31 | },
32 | methods:{
33 |
34 | clickHelper(e,isDoubleClick){
35 |
36 | if (isDoubleClick){
37 |
38 | // execute double click logic...
39 | }else{
40 |
41 | // execute normal click logic...
42 | }
43 | }
44 | }
45 |
46 | }
47 | ```
48 |
49 |
50 | # Advanced
51 |
52 | If you want to set, execute double click within 290 milliseconds,just:
53 |
54 | ```html
55 |
56 | click me
57 |
58 | ```
59 |
60 | # License
61 | http://www.opensource.org/licenses/mit-license.php
62 |
--------------------------------------------------------------------------------
/dist/clickHelper.js:
--------------------------------------------------------------------------------
1 | /**
2 | * v-clickHelper
3 | *
4 | * desc: A Vue directive to distinguish between click and double click
5 | *
6 | * author:https://github.com/huangshuwei
7 | *
8 | */
9 |
10 | 'use strict';
11 |
12 | Object.defineProperty(exports, "__esModule", {
13 | value: true
14 | });
15 |
16 | exports.default = {
17 | bind: function bind(el, binding, vNode) {
18 |
19 | var msg = '',
20 | clicks = 0,
21 | delay = 300,
22 | timer = null;
23 |
24 | if (typeof binding.value !== 'function') {
25 |
26 | msg = 'in [clickHelper] directives, provided expression \'' + binding.expression + '\' is not a function. ';
27 |
28 | var compName = vNode.context.name;
29 |
30 | if (compName) {
31 | msg += 'in ' + compName;
32 | }
33 | }
34 |
35 | if (binding.arg) {
36 |
37 | if (/^\+?(0|[1-9]\d*)$/.test(binding.arg)) {
38 |
39 | delay = binding.arg;
40 | } else {
41 |
42 | msg = 'in [clickHelper] directives, provided arg \'' + binding.arg + '\' is not a positive integer. ';
43 | }
44 | }
45 |
46 | if (msg.length > 0) {
47 |
48 | console.error(msg);
49 | }
50 |
51 | var handler = function handler(e) {
52 |
53 | clicks++;
54 |
55 | if (clicks === 1) {
56 |
57 | timer = setTimeout(function () {
58 |
59 | binding.value(e, false);
60 |
61 | clicks = 0;
62 | }, delay);
63 | } else {
64 |
65 | clearTimeout(timer);
66 |
67 | binding.value(e, true);
68 |
69 | clicks = 0;
70 | }
71 | };
72 |
73 | el.__clickHelper__ = handler;
74 |
75 | el.addEventListener('click', handler, true);
76 | },
77 |
78 | unbind: function unbind(el) {
79 | el.removeEventListener('click', el.__clickHelper__, true);
80 | el.__clickHelper__ = null;
81 | }
82 | };
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "vue-click-helper",
3 | "version": "0.0.2",
4 | "description": "A Vue directive to distinguish between click and double click",
5 | "main": "dist/clickHelper.js",
6 | "devDependencies": {
7 | "babel-cli": "^6.16.0",
8 | "babel-polyfill": "^6.23.0",
9 | "babel-preset-es2015": "^6.16.0",
10 | "babel-preset-stage-2": "^6.18.0"
11 | },
12 | "scripts": {
13 | "dist":"babel src --out-dir dist"
14 | },
15 | "repository": {
16 | "type": "git",
17 | "url": "https://github.com/huangshuwei/vue-click-helper"
18 | },
19 | "homepage": "https://github.com/huangshuwei/vue-click-helper/blob/master/README.md",
20 | "keywords": [
21 | "vue",
22 | "click",
23 | "dblclick",
24 | "double click",
25 | "directive"
26 | ],
27 | "author": "https://github.com/huangshuwei",
28 | "license": "MIT"
29 | }
30 |
--------------------------------------------------------------------------------
/src/clickHelper.js:
--------------------------------------------------------------------------------
1 | /**
2 | * v-clickHelper
3 | *
4 | * desc: A Vue directive to distinguish between click and double click
5 | *
6 | * author:https://github.com/huangshuwei
7 | *
8 | */
9 | export default {
10 | bind: function (el, binding, vNode) {
11 |
12 | let msg = '',
13 | clicks = 0, delay = 300, timer = null;
14 |
15 | if (typeof binding.value !== 'function') {
16 |
17 | msg = `in [clickHelper] directives, provided expression '${binding.expression}' is not a function. `
18 |
19 | const compName = vNode.context.name
20 |
21 | if (compName) {
22 | msg += `in ${compName}`
23 | }
24 | }
25 |
26 | if (binding.arg){
27 |
28 | if(/^\+?(0|[1-9]\d*)$/.test(binding.arg)){
29 |
30 | delay = binding.arg;
31 |
32 | }else{
33 |
34 | msg = `in [clickHelper] directives, provided arg '${binding.arg}' is not a positive integer. `
35 | }
36 | }
37 |
38 | if (msg.length > 0){
39 |
40 | console.error(msg)
41 | }
42 |
43 |
44 | var handler = (e) => {
45 |
46 | clicks++;
47 |
48 | if (clicks === 1) {
49 |
50 | timer = setTimeout(function () {
51 |
52 | binding.value(e, false);
53 |
54 | clicks = 0;
55 |
56 | }, delay);
57 | } else {
58 |
59 | clearTimeout(timer);
60 |
61 | binding.value(e, true);
62 |
63 | clicks = 0;
64 | }
65 | }
66 |
67 |
68 | el.__clickHelper__ = handler;
69 |
70 | el.addEventListener('click', handler, true);
71 | },
72 |
73 | unbind: function (el) {
74 | el.removeEventListener('click', el.__clickHelper__, true);
75 | el.__clickHelper__ = null;
76 |
77 | }
78 | };
--------------------------------------------------------------------------------