├── .gitignore
├── CHANGELOG.md
├── .editorconfig
├── meta.json
├── component.json
├── package.json
├── docs
├── README.md
├── Examples.md
└── API.md
├── .jshintrc
├── test
├── test.css
├── test.js
└── index.html
├── CONTRIBUTING.md
├── README.md
├── Gruntfile.js
└── index.js
/.gitignore:
--------------------------------------------------------------------------------
1 | components
2 | node_modules
3 | build
4 | dist
--------------------------------------------------------------------------------
/CHANGELOG.md:
--------------------------------------------------------------------------------
1 | # Changelog
2 |
3 | **0.1.0** :: *20th Dec 2013*
4 |
5 | - Initial release.
--------------------------------------------------------------------------------
/.editorconfig:
--------------------------------------------------------------------------------
1 | # editorconfig.org
2 | root = true
3 |
4 | [*]
5 | indent_style = tab
6 | end_of_line = lf
7 | charset = utf-8
8 | trim_trailing_whitespace = true
9 | insert_final_newline = false
--------------------------------------------------------------------------------
/meta.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "tooltips",
3 | "description": "Tooltips for DOM elements.",
4 | "version": "0.1.0",
5 | "date": "2013-12-20 18:23:43 +00:00",
6 | "repository": {
7 | "type": "git",
8 | "url": "https://github.com/darsain/tooltip.git",
9 | "homepage": "https://github.com/darsain/tooltip"
10 | },
11 | "licenses": [
12 | {
13 | "type": "MIT",
14 | "url": "http://opensource.org/licenses/MIT"
15 | }
16 | ]
17 | }
--------------------------------------------------------------------------------
/component.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "tooltips",
3 | "repo": "darsain/tooltips",
4 | "description": "Tooltips for DOM elements.",
5 | "version": "0.1.0",
6 | "keywords": [
7 | "tooltip",
8 | "tip",
9 | "ui"
10 | ],
11 | "dependencies": {
12 | "darsain/tooltip": "*",
13 | "darsain/event": "*",
14 | "component/indexof": "*",
15 | "code42day/dataset": "*"
16 | },
17 | "development": {
18 | "components/jquery": "1.10.2"
19 | },
20 | "scripts": [
21 | "index.js"
22 | ],
23 | "license": "MIT"
24 | }
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "tooltips",
3 | "version": "0.0.0",
4 | "devDependencies": {
5 | "grunt-contrib-compress": "~0.5.3",
6 | "grunt-contrib-uglify": "~0.2.7",
7 | "grunt-contrib-jshint": "~0.7.2",
8 | "grunt-contrib-concat": "~0.3.0",
9 | "grunt-contrib-clean": "~0.5.0",
10 | "grunt-contrib-watch": "~0.5.3",
11 | "grunt-contrib-copy": "~0.4.1",
12 | "grunt-component-build": "~0.4.2",
13 | "grunt-tagrelease": "~0.3.0",
14 | "grunt-bumpup": "~0.4.2",
15 | "grunt": "~0.4.2"
16 | }
17 | }
--------------------------------------------------------------------------------
/docs/README.md:
--------------------------------------------------------------------------------
1 | # Documentation
2 |
3 | - **[Tooltips](API.md)**
4 | - **[Examples](Examples.md)**
5 |
6 | ## [Tooltip](https://github.com/darsain/tooltip)
7 |
8 | `Tooltips` is a wrapper around [`Tooltip`](https://github.com/darsain/tooltip), so you should also read the [`Tooltip documentation`](https://github.com/darsain/tooltip/tree/master/docs).
9 |
10 | `Tooltips` also exposes the `Tooltip` library via a static property:
11 |
12 | ```js
13 | var tip = new Tooltips.Tooltip('This is a Tooltip instance!');
14 | ```
--------------------------------------------------------------------------------
/.jshintrc:
--------------------------------------------------------------------------------
1 | {
2 | "predef" : [
3 | "module",
4 | "require"
5 | ],
6 |
7 | "bitwise": false,
8 | "camelcase": false,
9 | "curly": true,
10 | "eqeqeq": true,
11 | "forin": false,
12 | "immed": true,
13 | "latedef": false,
14 | "newcap": true,
15 | "noarg": true,
16 | "noempty": true,
17 | "nonew": false,
18 | "plusplus": false,
19 | "quotmark": false,
20 | "regexp": false,
21 | "undef": true,
22 | "unused": true,
23 | "strict": true,
24 | "trailing": true,
25 |
26 | "asi": false,
27 | "boss": false,
28 | "debug": false,
29 | "eqnull": true,
30 | "es5": false,
31 | "esnext": false,
32 | "evil": false,
33 | "expr": false,
34 | "funcscope": false,
35 | "globalstrict": true,
36 | "iterator": false,
37 | "lastsemic": false,
38 | "laxbreak": false,
39 | "laxcomma": true,
40 | "loopfunc": false,
41 | "multistr": false,
42 | "onecase": true,
43 | "proto": false,
44 | "regexdash": false,
45 | "scripturl": false,
46 | "smarttabs": true,
47 | "shadow": false,
48 | "sub": false,
49 | "supernew": false,
50 |
51 | "browser": true
52 | }
--------------------------------------------------------------------------------
/test/test.css:
--------------------------------------------------------------------------------
1 | html, body { margin: 0; padding: 1px 0; color: #444; font-size: 1em; font-family: sans-serif; background: #ddd; }
2 |
3 | .target {
4 | position: relative;
5 | width: 100px;
6 | /*height: 50px;*/
7 | line-height: 50px;
8 | margin: 10px 5px;
9 | text-align: center;
10 | text-shadow: 1px 1px rgba(255,255,255,.5);
11 | border: 1px solid rgba(255,255,255,.8);
12 | border-radius: 4px;
13 | background: inherit;
14 | }
15 | .target:after {
16 | content: '';
17 | position: absolute;
18 | top: -2px;
19 | left: -2px;
20 | width: 100%;
21 | height: 100%;
22 | border: 1px solid #666;
23 | border-radius: 4px;
24 | }
25 | .target.movable { cursor: move; border-style: dashed; }
26 | .target.movable,
27 | .target.movable:after { border-style: dashed; }
28 |
29 | /* Lists */
30 | .targets {
31 | list-style: none;
32 | margin: 0 auto;
33 | padding: 0;
34 | word-spacing: -0.3em;
35 | text-align: center;
36 | }
37 | .targets .target { display: inline-block; }
38 | .targets.vertical .target { display: block; }
39 | .targets.vertical.left { float: left; }
40 | .targets.vertical.right { float: right; }
41 | .targets.clear { clear: both; }
42 |
43 | .examples {
44 | width: 600px;
45 | margin: 120px auto;
46 | text-align: center;
47 | }
48 |
49 | .dynamic {
50 | margin: 20px 120px 0;
51 | text-align: center;
52 | }
53 | .dynamic button {
54 | width: 80px;
55 | height: 40px;
56 | font-size: 1em;
57 | }
--------------------------------------------------------------------------------
/CONTRIBUTING.md:
--------------------------------------------------------------------------------
1 | ## Submitting an issue
2 |
3 | When reporting a bug, please describe it thoroughly, with attached code showcasing how are you using this library. The
4 | best way how to make it easy for developers, and ensure that your issue will be looked at, is to replicate it on
5 | [jsfiddle](http://jsfiddle.net/) or a similar service.
6 |
7 | ## Contributions
8 |
9 | Contributions are welcome! But please, follow these few simple rules:
10 |
11 | **Maintain the coding style** used throughout the project, and defined in the `.editorconfig` file. You can use the
12 | [Editorconfig](http://editorconfig.org) plugin for your editor of choice:
13 |
14 | - [Sublime Text 2](https://github.com/sindresorhus/editorconfig-sublime)
15 | - [Textmate](https://github.com/Mr0grog/editorconfig-textmate)
16 | - [Notepad++](https://github.com/editorconfig/editorconfig-notepad-plus-plus)
17 | - [Emacs](https://github.com/editorconfig/editorconfig-emacs)
18 | - [Vim](https://github.com/editorconfig/editorconfig-vim)
19 | - [Visual Studio](https://github.com/editorconfig/editorconfig-visualstudio)
20 | - [... other editors](http://editorconfig.org/#download)
21 |
22 | ---
23 |
24 | **Code has to pass JSHint** with options defined in the `.jshintrc` file. You can use `grunt jshint` task to lint
25 | manually, or again, there are amazing plugins for a lot of popular editors consuming this file and linting as you code:
26 |
27 | - [Sublim Text 2](https://github.com/SublimeLinter/SublimeLinter)
28 | - [TextMate](http://rondevera.github.com/jslintmate/), or [alternative](http://fgnass.posterous.com/jslint-in-textmate)
29 | - [Notepad++](http://sourceforge.net/projects/jslintnpp/)
30 | - [Emacs](https://github.com/daleharvey/jshint-mode)
31 | - [Vim](https://github.com/walm/jshint.vim)
32 | - [Visual Studio](https://github.com/jamietre/SharpLinter), or [alternative](http://jslint4vs2010.codeplex.com/)
33 | - [... other editors](http://www.jshint.com/platforms/)
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # [Tooltips](http://darsa.in/tooltips)
2 |
3 | Wrapper for [`darsain/tooltip`](http://github.com/darsain/tooltip) library that provides simple bindings for DOM elements
4 | with `data-{key}` attributes.
5 |
6 | Tooltips can use mutation observers to handle dynamic elements, but also provides a fallback methods when you need to
7 | support older browsers.
8 |
9 | It works by attaching events defined in `showOn` & `hideOn` options to each element with `data-{key}` attribute. This is
10 | way better for performance than attaching these events to `container` and handling them for all elements on a page.
11 | Especially in case of events like `mouseenter` & `mouseleave`, which are the main focus of this library.
12 |
13 | To save memory, `Tooltip` instances are created only when first `showOn` event is fired.
14 |
15 | #### Compatibility
16 |
17 | Browser support starts at IE8+, with an exception of automatic binding to dynamic elements via Mutation Observers, which
18 | are used only when supported. Mutation Observers have been implemented in all modern browsers and IE11.
19 |
20 | If you want to support browsers without Mutation Observers, you can fall back to `.reload()` method, or manage dynamic
21 | elements as you add & remove them with `.add()` & `.remove()` methods.
22 |
23 | ## Install
24 |
25 | Tooltips is a [component](https://github.com/component/component):
26 |
27 | ```bash
28 | component install darsain/tooltips
29 | ```
30 |
31 | ## Download
32 |
33 | Standalone build of a latest stable version:
34 |
35 | - [`tooltips.zip`](http://darsain.github.io/tooltips/dist/tooltips.zip) - combined archive
36 | - [`tooltips.js`](http://darsain.github.io/tooltips/dist/tooltips.js) - 38 KB *sourcemapped*
37 | - [`tooltips.min.js`](http://darsain.github.io/tooltips/dist/tooltips.min.js) - 14 KB, 2.8KB gzipped
38 | - [`tooltips.css`](http://darsain.github.io/tooltips/dist/tooltips.css) - 4.5 KB *including transitions & types*
39 |
40 | When isolating issues on jsfiddle, use the [`tooltips.js`](http://darsain.github.io/tooltips/dist/tooltips.js) URL above.
41 |
42 | ## Documentation
43 |
44 | Can be found in the **[docs](docs)** directory.
45 |
46 | [Changelog](CHANGELOG.md).
47 |
48 | ## Contributing
49 |
50 | Please, read the [Contributing Guidelines](CONTRIBUTING.md) for this project.
51 |
52 | ## License
53 |
54 | MIT
--------------------------------------------------------------------------------
/docs/Examples.md:
--------------------------------------------------------------------------------
1 | # Examples
2 |
3 | Assuming:
4 |
5 | ```js
6 | var Tooltips = require('tooltips');
7 | ```
8 |
9 | Bind tooltips to all elements on a page with `data-tooltip` attribute:
10 |
11 | ```js
12 | var tips = new Tooltips(document.body); // Using all default options
13 | ```
14 |
15 | Tooltips will be attached to elements like this:
16 |
17 | ```html
18 |
target
19 | ```
20 |
21 | Tooltip text is used as tooltip's `innerHTML` content, so you can use HTML, but escape it beforehand.
22 |
23 | ---
24 |
25 | Use custom data key to create focus hints for form elements:
26 |
27 | ```js
28 | var hints = new Tooltips(document.body, {
29 | key: 'hint',
30 | showOn: 'focus',
31 | hideOn: 'blur'
32 | });
33 | ```
34 |
35 | The tooltips will than show on elements with `data-hint` attribute when they are focused:
36 |
37 | ```html
38 |
39 | ```
40 |
41 | ---
42 |
43 | To make Tooltips automatically keep track of dynamically added/removed elements, enable the `observe` option:
44 |
45 | ```js
46 | var tips = new Tooltips(document.body, {
47 | observe: 1
48 | });
49 | ```
50 |
51 | When you need to support browsers without Mutation Observer support, you can either use `.reload()` method on each DOM manipulation:
52 |
53 | ```js
54 | document.body.appendChild(newElement);
55 | tips.reload();
56 | ```
57 |
58 | But that is a nuclear option that destroys everything and rebuilds it again.
59 |
60 | Way more efficient is to notify current Tooltips instance about added or removed elements:
61 |
62 | ```js
63 | document.body.appendChild(newElement);
64 | tips.add(newElement);
65 | document.body.removeChild(newElement);
66 | tips.remove(newElement);
67 | ```
68 |
69 | This will tell tooltip to look for added/removed tooltips only within `newElement`. You need to notify about removed elements so Tooltips can clean up after them (unbind event listeners and destroy Tooltip instances).
70 |
71 | If `newElement` doesn't have `data-{key}` attribute, Tooltips will look on its children. This means that when you insert element like this into the DOM:
72 |
73 | ```html
74 |
60 |
61 |
62 |
63 |
64 |
65 |
--------------------------------------------------------------------------------
/docs/API.md:
--------------------------------------------------------------------------------
1 | # API
2 |
3 | ```js
4 | var tips = new Tooltips(container, [options]);
5 | ```
6 |
7 | ### container
8 |
9 | Type: `Element`
10 |
11 | Tooltips container. Tooltips will be bound to `data-{key}` elements that are descendants of this element.
12 |
13 | ### [options]
14 |
15 | Type: `Object`
16 |
17 | Tooltips options object.
18 |
19 | Default options are stored in `Tooltips.defaults`, and look like this:
20 |
21 | ```js
22 | Tooltips.defaults = {
23 | tooltip: {}, // Options for individual Tooltip instances.
24 | key: 'tooltip', // Tooltips data attribute key.
25 | showOn: 'mouseenter', // Show tooltip event.
26 | hideOn: 'mouseleave', // Hide tooltip event.
27 | observe: 0 // Enable mutation observer (used only when supported).
28 | };
29 | ```
30 |
31 | ##### tooltip
32 |
33 | Type `Object` Default `{}`
34 |
35 | [`Tooltip`](https://github.com/darsain/tooltip) options that will be used for every Tooltip in this instance, unless overridden by element's `data-{key}-{optionName}` attribute.
36 |
37 | ##### key
38 |
39 | Type `String` Default `tooltip`
40 |
41 | Data key name used to recognize elements with tooltips, and retrieve the Tooltip content & options.
42 |
43 | ```html
44 |
45 | ```
46 |
47 | ##### showOn
48 |
49 | Type `String` Default `mouseenter`
50 |
51 | Event that - when triggered on an element with `data-{key}` attribute - should show a tooltip.
52 |
53 | ##### hideOn
54 |
55 | Type `String` Default `mouseleave`
56 |
57 | Event that - when triggered on an element with `data-{key}` attribute - should hide a tooltip.
58 |
59 | ##### observe
60 |
61 | Type `Boolean` Default `false`
62 |
63 | Whether to use Mutation Observer to keep track of dynamic elements inside `container`.
64 |
65 | ## Methods
66 |
67 | Unless stated otherwise, all methods return tooltips object, making them chainable.
68 |
69 | #### #show(element)
70 |
71 | `Element` **element**
72 |
73 | Show Tooltip associated to an `element`. If Tooltip doesn't exist yet, it will be created using current instance's options.
74 |
75 | #### #hide(element)
76 |
77 | `Element` **element**
78 |
79 | Hide Tooltip associated to an `element`.
80 |
81 | #### #toggle(element)
82 |
83 | `Element` **element**
84 |
85 | Hide/Show Tooltip associated to an `element`.
86 |
87 | #### #get(element)
88 |
89 | `Element` **element**
90 |
91 | Get Tooltip associated to an `element`.
92 |
93 | #### #add(element)
94 |
95 | `Element` **element**
96 |
97 | When not using Mutation Observer, you can use this method to notify Tooltips instance that element has been added. Tooltips will than look at the element and its children for elements with `data-{key}` attributes and attach tooltips to them.
98 |
99 | #### #remove(element)
100 |
101 | `Element` **element**
102 |
103 | When not using Mutation Observer, you can use this method to notify Tooltips instance that element has been removed. Tooltips will than look at the element and its children for elements with `data-{key}` attributes and detach tooltips from them.
104 |
105 | #### #reload()
106 |
107 | Unbinds all current attached tooltips, clears event listeners, and than reapplies everything to elements with `data-{key}` attributes inside `container`.
108 |
109 | Use this when you are changing DOM, not using Mutation Observer, and don't want to bother with `.add()` and `.remove()` methods. Using this instead of `.add()` & `.remove()` is less efficient.
110 |
111 | #### #destroy()
112 |
113 | Destroys the Tooltips instance. Clears all event listeners, destroys all tooltips, and clears objects.
114 |
115 | ## Properties
116 |
117 | #### #container
118 |
119 | Container element of a current instance.
120 |
121 | #### #elements
122 |
123 | Array of elements with `data-{key}` attributes the current instance is keeping track of.
124 |
125 | #### #options
126 |
127 | Tooltips options object.
128 |
129 | ## HTML
130 |
131 | Tooltips looks for elements with `data-{key}` attributes and creates tooltips for them. We will assume that key is a default value `tooltip`.
132 |
133 | Tooltip content is retrieved from main `data-tooltip` attribute:
134 |
135 | ```html
136 |
target
137 | ```
138 |
139 | You can also override options for individual tooltips with additional attributes:
140 |
141 | ```html
142 |