Skip to content

Commit 823ee2f

Browse files
Merge pull request #11 from jonataswalker/some-changes
Some changes
2 parents 300b3e7 + 2c4c028 commit 823ee2f

File tree

14 files changed

+200
-197
lines changed

14 files changed

+200
-197
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/node_modules/
2+
/build/timestamps/

.travis.yml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
sudo: required
2+
dist: trusty
3+
14
language: node_js
25

36
node_js:
4-
- "4"
7+
- "6"
58

69
cache:
710
bundler: true
@@ -16,8 +19,6 @@ before_install:
1619
- "if [ $(phantomjs --version) != '2.1.1' ]; then sudo ln -sf $PWD/travis-phantomjs/phantomjs-builds-2.1.1/bin/phantomjs $(which phantomjs); fi"
1720
- "phantomjs --version"
1821

19-
before_script:
20-
2122
script:
2223
- xvfb-run -a make ci
2324

Makefile

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,46 @@ endef
4444
export HEADER
4545

4646
# targets
47+
.PHONY: default
48+
default: help
49+
50+
.PHONY: help
51+
help:
52+
@echo
53+
@echo "The most common targets are:"
54+
@echo
55+
@echo "- install Install node dependencies"
56+
@echo "- build Build JavaScript and CSS files"
57+
@echo "- build-watch Build files and watch for modifications"
58+
@echo "- test Run unit tests in the console"
59+
@echo "- help Display this help message"
60+
@echo
61+
@echo "Other less frequently used targets are:"
62+
@echo
63+
@echo "- lint Check the code with the linter"
64+
@echo "- build-js Build JavaScript files"
65+
@echo "- ci Run the full continuous integration process"
66+
@echo
67+
4768
.PHONY: ci
48-
ci: build test
69+
ci: test
70+
71+
.PHONY: npm-install
72+
npm-install: install
73+
74+
$(BUILD_DIR)/timestamps/node-modules-timestamp: package.json
75+
@mkdir -p $(@D)
76+
npm install
77+
@touch $@
78+
79+
.PHONY: install
80+
install: $(BUILD_DIR)/timestamps/node-modules-timestamp
81+
82+
.PHONY: clean
83+
clean:
84+
@rm -f $(BUILD_DIR)/timestamps/eslint-timestamp
85+
@rm -f $(JS_FINAL)
86+
@rm -f $(JS_DEBUG)
4987

5088
.PHONY: build-watch
5189
build-watch: build watch
@@ -54,23 +92,30 @@ build-watch: build watch
5492
watch: watch-js
5593

5694
.PHONY: build
57-
build: build-js
95+
build: install clean build-js
5896

5997
.PHONY: build-js
6098
build-js: bundle-js lint uglifyjs add-js-header
6199
@echo `date +'%H:%M:%S'` "Build JS ... OK"
62100

63101
.PHONY: test
64-
test:
102+
test: build
65103
@$(CASPERJS) $(CASPERJSFLAGS)
66104

67105
.PHONY: bundle-js
68106
bundle-js:
69107
@$(ROLLUP) $(ROLLUPFLAGS)
70108

71-
.PHONY: lint
72-
lint: $(JS_DEBUG)
109+
$(BUILD_DIR)/timestamps/eslint-timestamp: $(SRC_DIR) \
110+
$(ROOT_DIR)/test/ \
111+
$(ROOT_DIR)/examples/
112+
@mkdir -p $(@D)
113+
@echo "Running eslint ..."
73114
@$(ESLINT) $^
115+
@touch $@
116+
117+
.PHONY: lint
118+
lint: $(BUILD_DIR)/timestamps/eslint-timestamp
74119

75120
.PHONY: uglifyjs
76121
uglifyjs: $(JS_DEBUG)
@@ -91,4 +136,4 @@ add-js-header: add-js-header-debug add-js-header-min
91136
watch-js: $(SRC_DIR)
92137
@$(NODEMON) --on-change-only --watch $^ --ext js --exec "make build-js"
93138

94-
.DEFAULT_GOAL := build
139+
.DEFAULT_GOAL := default

examples/example.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
hljs.configure({
2-
tabReplace: ' ',
3-
})
4-
hljs.initHighlightingOnLoad();
1+
/* global hljs, WatchElementResize */
52

63
var size_el = document.getElementById('size');
74
var size2_el = document.getElementById('size2');
@@ -10,7 +7,7 @@ var size = { x: 100, y: 80 };
107

118
var watchResize = new WatchElementResize(['id', 'id2']);
129

13-
click_el.addEventListener('click', function() {
10+
click_el.addEventListener('click', function () {
1411
if (size.x > 180) {
1512
size = { x: 100, y: 80 };
1613

@@ -22,18 +19,20 @@ click_el.addEventListener('click', function() {
2219
size.x += 40;
2320
size.y += 40;
2421
}
25-
22+
2623
click_el.style.width = size.x + 'px';
2724
click_el.style.height = size.y + 'px';
2825
});
2926

30-
watchResize.on('resize', function(evt) {
27+
watchResize.on('resize', function (evt) {
3128
var offset = evt.element.offset;
32-
33-
if (evt.element.target.id == 'id') {
29+
30+
if (evt.element.target.id === 'id') {
3431
size_el.innerHTML = offset.width + ' x ' + offset.height;
3532
} else {
3633
size2_el.innerHTML = offset.width + ' x ' + offset.height;
3734
}
3835
});
3936

37+
hljs.configure({ tabReplace: ' ' });
38+
hljs.initHighlightingOnLoad();

package.json

Lines changed: 9 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -21,52 +21,27 @@
2121
},
2222
"keywords": [
2323
"resize",
24+
"watch",
25+
"watcher",
26+
"element",
2427
"events"
2528
],
2629
"devDependencies": {
2730
"casperjs": "^1.1.1",
2831
"eslint": "^3.4.0",
32+
"eslint-config-jwalker": "^1.11.0",
2933
"nodemon": "^1.9.2",
3034
"rollup": "^0.36.0",
3135
"rollup-plugin-buble": "^0.14.0",
3236
"rollup-plugin-json": "^2.0.0",
3337
"uglify-js": "^2.6.2"
3438
},
3539
"eslintConfig": {
36-
"env": {
37-
"browser": true,
38-
"amd": true,
39-
"commonjs": true,
40-
"node": false
41-
},
42-
"rules": {
43-
"block-scoped-var": 1,
44-
"dot-notation": 1,
45-
"no-alert": 2,
46-
"no-caller": 2,
47-
"no-eval": 2,
48-
"no-proto": 1,
49-
"no-redeclare": 2,
50-
"valid-typeof": 2,
51-
"no-unreachable": 2,
52-
"no-irregular-whitespace": 1,
53-
"no-invalid-regexp": 1,
54-
"no-func-assign": 2,
55-
"no-extra-semi": 2,
56-
"no-dupe-keys": 2,
57-
"no-undef": 2,
58-
"no-use-before-define": 2,
59-
"no-unused-vars": 1,
60-
"strict": [
61-
1,
62-
"global"
63-
],
64-
"key-spacing": 0,
65-
"new-cap": 1,
66-
"quotes": [
67-
1,
68-
"single"
69-
]
40+
"extends": "jwalker",
41+
"globals": {
42+
"casper": true,
43+
"patchRequire": true,
44+
"phantom": true
7045
}
7146
}
7247
}

src/base.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,25 @@ import utils from './utils';
99
export default class Base extends Emitter {
1010
/**
1111
* @constructor
12-
* @param {String|Array<String>|Element|Array<Element>} target String or
12+
* @param {String|Array<String>|Element|Array<Element>} target String or
1313
* array of string, DOM node or array of nodes.
1414
* @param {Object|undefined} opt_options Options.
1515
*/
1616
constructor(target, opt_options = {}) {
17-
utils.assert(Array.isArray(target) ||
18-
utils.typeOf(target) == 'string' ||
19-
utils.isElement(target),
20-
'@param `target` should be Element, String or Array.');
21-
17+
utils.assert(Array.isArray(target)
18+
|| utils.typeOf(target) === 'string'
19+
|| utils.isElement(target),
20+
'`target` should be Element, <Array>Element, String or <Array>String.'
21+
);
2222
super();
23-
2423
this.target = target;
2524
Base.Internal = new Internal(this);
2625
}
27-
26+
2827
reAddListener() {
2928
Base.Internal.setListener(this.target);
3029
}
31-
30+
3231
removeListener() {
3332
Base.Internal.removeListener();
3433
}

src/emitter.js

Lines changed: 14 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,71 +3,54 @@
33
* @class Emitter
44
*/
55
export class Emitter {
6-
76
/**
87
* @constructor
98
*/
109
constructor() {
1110
// set events hash
1211
this._events = this._events || {};
13-
1412
// set onceEvents hash
15-
this._onceEvents = this._onceEvents || {}
13+
this._onceEvents = this._onceEvents || {};
1614
}
17-
15+
1816
on(eventName, listener) {
19-
if (!eventName || !listener) {
20-
return;
21-
}
17+
if (!eventName || !listener) return;
2218
// set listeners array
2319
let listeners = this._events[eventName] = this._events[eventName] || [];
2420
// only add once
25-
if (listeners.indexOf(listener) == -1) {
26-
listeners.push(listener);
27-
}
28-
21+
if (listeners.indexOf(listener) === -1) listeners.push(listener);
2922
return this;
3023
}
31-
24+
3225
once(eventName, listener) {
33-
if (!eventName || !listener) {
34-
return;
35-
}
26+
if (!eventName || !listener) return;
3627
// add event
3728
this.on(eventName, listener);
3829
// set onceListeners object
39-
let onceListeners = this._onceEvents[eventName] =
40-
this._onceEvents[eventName] || {};
30+
let onceListeners =
31+
this._onceEvents[eventName] =
32+
this._onceEvents[eventName] || {};
4133
// set flag
4234
onceListeners[listener] = true;
43-
4435
return this;
4536
}
4637
off(eventName, listener) {
4738
let listeners = this._events && this._events[eventName];
48-
if (!listeners || !listeners.length) {
49-
return;
50-
}
39+
if (!listeners || !listeners.length) return;
5140
const index = listeners.indexOf(listener);
52-
if (index != -1) {
53-
listeners.splice(index, 1);
54-
}
55-
41+
if (index !== -1) listeners.splice(index, 1);
5642
return this;
5743
}
58-
44+
5945
dispatchEvent(eventName, obj = {}) {
6046
let listeners = this._events && this._events[eventName];
61-
62-
if (!listeners || !listeners.length) {
63-
return;
64-
}
47+
if (!listeners || !listeners.length) return;
6548

6649
let i = 0;
6750
let listener = listeners[i];
6851
// once stuff
6952
const onceListeners = this._onceEvents && this._onceEvents[eventName];
70-
53+
7154
while (listener) {
7255
let isOnce = onceListeners && onceListeners[listener];
7356
if (isOnce) {
@@ -83,7 +66,6 @@ export class Emitter {
8366
i += isOnce ? 0 : 1;
8467
listener = listeners[i];
8568
}
86-
8769
return this;
8870
}
8971
}

0 commit comments

Comments
 (0)