jquery-watcher 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +43 -0
- data/Rakefile +3 -0
- data/assets/javascripts/jquery-watcher.js +2 -0
- data/assets/javascripts/jquery-watcher/dom_observer.coffee +49 -0
- data/assets/javascripts/jquery-watcher/watcher.coffee +27 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/jquery-watcher.gemspec +30 -0
- data/lib/jquery/watcher.rb +2 -0
- data/lib/jquery/watcher/engine.rb +9 -0
- data/lib/jquery/watcher/version.rb +5 -0
- metadata +102 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: bc63721acbf0056d9617f0ce3a09236010152319
|
4
|
+
data.tar.gz: ccdbc55f39df6b848b37d4f37b4ed4fbf2a6f9ab
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: dec719e0adcf85681cb7873d1a3f851b88098a5237328ac8d388bb0bd6b41a6054e722c8e651faec34d85cf546b2d0bd978a6d52507ae60cb7e68fb9ded67693
|
7
|
+
data.tar.gz: 6c6b9887a419040c2c3a60805da72a6fe0484a62638feb434c0a20d55a79de221433c4aa07b010ac53b15d4517beea4ba6e2112fe33d0ad9546fc71615d4ca4e
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Florian Schwab
|
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
|
13
|
+
all 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
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
[](https://rubygems.org/gems/jquery-watcher)
|
2
|
+
[](https://gemnasium.com/ydkn/jquery-watcher)
|
3
|
+
[](https://codeclimate.com/github/ydkn/jquery-watcher)
|
4
|
+
|
5
|
+
|
6
|
+
# Jquery::Watcher
|
7
|
+
|
8
|
+
Allow to to register callbacks on elements regardless if they are loaded with _document.ready_, _turbolinks:load_ or DOM manipulation (e.g. through Ajax)
|
9
|
+
|
10
|
+
## Installation
|
11
|
+
|
12
|
+
Add this line to your application's Gemfile:
|
13
|
+
|
14
|
+
```ruby
|
15
|
+
gem 'jquery-watcher'
|
16
|
+
```
|
17
|
+
|
18
|
+
And then execute:
|
19
|
+
|
20
|
+
$ bundle
|
21
|
+
|
22
|
+
Or install it yourself as:
|
23
|
+
|
24
|
+
$ gem install jquery-watcher
|
25
|
+
|
26
|
+
Finally add it to your _application.js_:
|
27
|
+
|
28
|
+
//= require jquery-watcher
|
29
|
+
|
30
|
+
## Usage
|
31
|
+
|
32
|
+
$.watch 'a.fancy', (elem) ->
|
33
|
+
elem.click ->
|
34
|
+
alert('Awesome')
|
35
|
+
|
36
|
+
## Contributing
|
37
|
+
|
38
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/ydkn/jquery-watcher. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
39
|
+
|
40
|
+
|
41
|
+
## License
|
42
|
+
|
43
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# This code was adapted from:
|
2
|
+
# https://jsfiddle.net/javan/rxyd0tts/ which was discussed here:
|
3
|
+
# https://github.com/turbolinks/turbolinks/issues/159
|
4
|
+
|
5
|
+
class @JqueryDOMObserver
|
6
|
+
constructor: ->
|
7
|
+
@selectors = {}
|
8
|
+
@observer = new MutationObserver(@processMutations)
|
9
|
+
|
10
|
+
@observer.observe(document, childList: true, subtree: true, attributes: true)
|
11
|
+
|
12
|
+
add: (selector, callback) ->
|
13
|
+
@selectors[selector] ?= []
|
14
|
+
@selectors[selector].push(callback)
|
15
|
+
|
16
|
+
processMutations: (mutations) =>
|
17
|
+
elements = new Set
|
18
|
+
|
19
|
+
for mutation in mutations
|
20
|
+
switch mutation.type
|
21
|
+
when 'childList'
|
22
|
+
for node in mutation.addedNodes when node.nodeType is Node.ELEMENT_NODE
|
23
|
+
elements.add(node)
|
24
|
+
when 'attributes'
|
25
|
+
elements.add(mutation.target)
|
26
|
+
|
27
|
+
elements.forEach(@processElement)
|
28
|
+
|
29
|
+
processElement: (element) =>
|
30
|
+
for selector, callbacks of @selectors
|
31
|
+
for matchingElement in @findMatchingElements(element, selector)
|
32
|
+
for callback in callbacks
|
33
|
+
callback(matchingElement)
|
34
|
+
|
35
|
+
findMatchingElements: (element, selector) ->
|
36
|
+
elements = []
|
37
|
+
elements.push(element) if matches(element, selector)
|
38
|
+
elements.concat(element.querySelectorAll(selector)...)
|
39
|
+
|
40
|
+
matches = do ->
|
41
|
+
element = document.documentElement
|
42
|
+
method = element.matches ?
|
43
|
+
element.matchesSelector ?
|
44
|
+
element.webkitMatchesSelector ?
|
45
|
+
element.mozMatchesSelector ?
|
46
|
+
element.msMatchesSelector
|
47
|
+
|
48
|
+
(element, selector) ->
|
49
|
+
method.call(element, selector)
|
@@ -0,0 +1,27 @@
|
|
1
|
+
class @JqueryWatcher
|
2
|
+
@observer: new JqueryDOMObserver
|
3
|
+
|
4
|
+
@watch: (selector, callback) ->
|
5
|
+
id = Math.floor(Math.random() * 10000000000000001)
|
6
|
+
|
7
|
+
register = (element) ->
|
8
|
+
return if element.data('jquery-watcher-id-' + id + '-watching') == 'true'
|
9
|
+
|
10
|
+
element.data(('jquery-watcher-id-' + id + '-watching'), 'true')
|
11
|
+
|
12
|
+
callback(element)
|
13
|
+
|
14
|
+
$(document).ready ->
|
15
|
+
register($(selector))
|
16
|
+
|
17
|
+
document.addEventListener 'turbolinks:load', ->
|
18
|
+
register($(selector))
|
19
|
+
|
20
|
+
this.observer.add selector, (node) ->
|
21
|
+
element = $(node)
|
22
|
+
|
23
|
+
register(element)
|
24
|
+
|
25
|
+
|
26
|
+
jQuery['watch'] = (selector, callback) ->
|
27
|
+
JqueryWatcher.watch(selector, callback)
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'jquery/watcher'
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require 'irb'
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'jquery/watcher/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'jquery-watcher'
|
8
|
+
spec.version = Jquery::Watcher::VERSION
|
9
|
+
spec.authors = ['Florian Schwab']
|
10
|
+
spec.email = ['me@ydkn.de']
|
11
|
+
|
12
|
+
spec.summary = %q{Allow initialization of javascript code regardless of document.ready, turbolinks or dom changes (e.g. Ajax).}
|
13
|
+
spec.description = %q{Allow initialization of javascript code regardless of document.ready, turbolinks or dom changes (e.g. Ajax).}
|
14
|
+
spec.homepage = 'https://github.com/ydkn/jquery-watcher'
|
15
|
+
spec.license = 'MIT'
|
16
|
+
|
17
|
+
spec.required_ruby_version = '>= 2.4.0'
|
18
|
+
|
19
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
20
|
+
f.match(%r{^(test|spec|features)/})
|
21
|
+
end
|
22
|
+
spec.bindir = 'exe'
|
23
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
24
|
+
spec.require_paths = ['lib']
|
25
|
+
|
26
|
+
spec.add_runtime_dependency 'coffee-rails', '>= 4.0'
|
27
|
+
|
28
|
+
spec.add_development_dependency 'bundler', '~> 1.14'
|
29
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
30
|
+
end
|
metadata
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: jquery-watcher
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Florian Schwab
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-01-30 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: coffee-rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '4.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '4.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.14'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.14'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
description: Allow initialization of javascript code regardless of document.ready,
|
56
|
+
turbolinks or dom changes (e.g. Ajax).
|
57
|
+
email:
|
58
|
+
- me@ydkn.de
|
59
|
+
executables: []
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- ".gitignore"
|
64
|
+
- Gemfile
|
65
|
+
- LICENSE.txt
|
66
|
+
- README.md
|
67
|
+
- Rakefile
|
68
|
+
- assets/javascripts/jquery-watcher.js
|
69
|
+
- assets/javascripts/jquery-watcher/dom_observer.coffee
|
70
|
+
- assets/javascripts/jquery-watcher/watcher.coffee
|
71
|
+
- bin/console
|
72
|
+
- bin/setup
|
73
|
+
- jquery-watcher.gemspec
|
74
|
+
- lib/jquery/watcher.rb
|
75
|
+
- lib/jquery/watcher/engine.rb
|
76
|
+
- lib/jquery/watcher/version.rb
|
77
|
+
homepage: https://github.com/ydkn/jquery-watcher
|
78
|
+
licenses:
|
79
|
+
- MIT
|
80
|
+
metadata: {}
|
81
|
+
post_install_message:
|
82
|
+
rdoc_options: []
|
83
|
+
require_paths:
|
84
|
+
- lib
|
85
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 2.4.0
|
90
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
91
|
+
requirements:
|
92
|
+
- - ">="
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
95
|
+
requirements: []
|
96
|
+
rubyforge_project:
|
97
|
+
rubygems_version: 2.6.8
|
98
|
+
signing_key:
|
99
|
+
specification_version: 4
|
100
|
+
summary: Allow initialization of javascript code regardless of document.ready, turbolinks
|
101
|
+
or dom changes (e.g. Ajax).
|
102
|
+
test_files: []
|