Skip to content

Commit 8d263b9

Browse files
Merge branch '2.1'
2 parents 9a144e5 + acc8765 commit 8d263b9

File tree

10 files changed

+157
-25
lines changed

10 files changed

+157
-25
lines changed

.babelrc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"presets": [ "es2015" ],
3+
"plugins": [
4+
"babel-polyfill"
5+
]
6+
}

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
package-lock.json

README.md

Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Install from [NPM](https://nodei.co/npm/gpslab-controller/):
1717
$ npm install gpslab-controller
1818
```
1919

20-
Or download the script [here](https://github.com/gpslab/gpslab-controller/blob/master/src/controller.js) and include it (unless you are packaging scripts somehow else):
20+
Or download the script [here](https://github.com/gpslab/gpslab-controller/blob/master/dist/controller.min.js) and include it (unless you are packaging scripts somehow else):
2121

2222
```html
2323
<script src="/path/to/controller.js"></script>
@@ -26,9 +26,14 @@ Or download the script [here](https://github.com/gpslab/gpslab-controller/blob/m
2626
Or include it via [jsDelivr CDN](https://www.jsdelivr.com/package/npm/gpslab-controller):
2727

2828
```html
29-
<script src="https://cdn.jsdelivr.net/npm/gpslab-controller@2/src/controller.js"></script>
29+
<script src="https://cdn.jsdelivr.net/npm/gpslab-controller@2/dist/controller.min.js"></script>
3030
```
3131

32+
## ECMAScript 2016
33+
34+
This framework is written for ECMAScript 2016, but you can use the recompiled version for
35+
[ECMAScript 2015](dist/controller.es2015.min.js).
36+
3237
## Methods
3338

3439
The library exposes these methods: `registerControl()`, `registerControls()`, `singleBind()`, `bind()`.
@@ -68,7 +73,7 @@ Binding the control for single specific element.
6873

6974
#### Arguments
7075

71-
1. `element` (**HTMLElement**) HTMLElement for binding.
76+
1. `element` (**Element**) Element for binding.
7277

7378
#### Returns
7479

@@ -82,7 +87,7 @@ Find the controls in element and children elements and binding it.
8287

8388
#### Arguments
8489

85-
1. `element` (**?HTMLElement**) HTMLElement for binding. The `BODY` element as a default.
90+
1. `element` (**Element**) Element for binding.
8691

8792
#### Returns
8893

@@ -94,10 +99,10 @@ Create new control for bind [the jQuery datepicker](https://jqueryui.com/datepic
9499
controller:
95100

96101
```js
97-
Controller.registerControl('form-date', element => $(element).datepicker({dateFormat: 'yy-mm-dd'}));
102+
Controller.registerControl('date-picker', element => $(element).datepicker({dateFormat: 'yy-mm-dd'}));
98103

99104
document.addEventListener('DOMContentLoaded', function() {
100-
Controller.bind(); // bind datepicker control
105+
Controller.bind(document.getElementsByTagName('body').item(0)); // find input and bind datepicker control to it
101106
});
102107
```
103108

@@ -106,7 +111,7 @@ Use in HTML:
106111
```html
107112
<form>
108113
<!-- after document loaded Datepicker will be binded to this element -->
109-
<input type="date" name="date" data-control="form-date">
114+
<input type="date" name="date" data-control="date-picker">
110115
<button type="submit">Submit</button>
111116
</form>
112117
```
@@ -119,7 +124,7 @@ You can bind controls for a new added elements:
119124
const input = document.createElement('input');
120125
input.setAttribute('type', 'date');
121126
input.setAttribute('name', 'date');
122-
input.setAttribute('data-control', 'form-date');
127+
input.setAttribute('data-control', 'date-picker');
123128

124129
// add element to document first
125130
// sometimes controls incorrectly works if you binding them before add element to a document
@@ -137,18 +142,28 @@ Use spaces (` `) or commas (`,`) for separate control names in the `data` attrib
137142

138143
```html
139144
<form>
140-
<input
141-
type="date"
142-
name="date"
143-
required="required"
144-
data-control="form-date form-required form-related"
145-
data-related-target="#date_related"
146-
>
147-
<input type="date" name="date_related" data-control="form-date" id="date_related">
145+
<!-- set password and repeat it for sign up -->
146+
<input type="password" name="password" required="required" data-control="show-password input-equal-to" data-equal-to="#repeat_password">
147+
<input type="password" name="repeat_password" required="required" data-control="show-password" id="repeat_password">
148148
<button type="submit">Submit</button>
149149
</form>
150150
```
151151

152+
```js
153+
Controller.registerControl('input-equal-to', element => {
154+
const target = document.querySelectorAll(element.getAttribute('data-equal-to'));
155+
// check that value of input element equal to value of target element
156+
});
157+
Controller.registerControl('show-password', element => {
158+
// for example, you can add button for show password
159+
});
160+
161+
// bind all controls for all elements
162+
document.addEventListener('DOMContentLoaded', function() {
163+
Controller.bind(document.getElementsByTagName('body').item(0));
164+
});
165+
```
166+
152167
### Use classes for controls
153168

154169
It will be better create new classes for define control and encapsulate all logic in them:
@@ -193,10 +208,10 @@ class AppendControl {
193208
}
194209

195210
Controller.registerControls({
196-
'form-date': element => $(element).datepicker({dateFormat: 'yy-mm-dd'}),
211+
'date-picker': element => $(element).datepicker({dateFormat: 'yy-mm-dd'}),
197212
'append': element => new AppendControl(element),
198213
});
199-
Controller.bind();
214+
Controller.bind(document.getElementsByTagName('body')[0]);
200215
```
201216

202217
Use in HTML:
@@ -205,10 +220,22 @@ Use in HTML:
205220
<button
206221
type="button"
207222
data-control="append"
208-
data-prototype="<input type='date' name='date' data-control='form-date' />"
223+
data-prototype="<input type='date' name='date' data-control='date-picker'>"
209224
>Append</button>
210225
```
211226

227+
## Building
228+
229+
For contributors:
230+
231+
* Run `npm install` to install all the dependencies.
232+
* Run `gulp`. The default task will build minify files.
233+
234+
For repo owners, after a code change:
235+
236+
* Run `npm version` to tag the new release.
237+
* Run `npm login`, `npm publish` to release on npm.
238+
212239
## License
213240

214241
This bundle is under the [MIT license](http://opensource.org/licenses/MIT). See the complete license in the file: LICENSE

dist/controller.es2015.min.js

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/controller.es2015.min.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/controller.min.js

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/controller.min.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gulpfile.babel.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import gulp from 'gulp';
2+
import uglify from 'gulp-uglify';
3+
import uglify_es from 'gulp-uglify-es';
4+
import rename from 'gulp-rename';
5+
import sourcemaps from 'gulp-sourcemaps';
6+
import buffer from 'gulp-buffer';
7+
import headerComment from 'gulp-header-comment';
8+
import babelify from 'babelify';
9+
import browserify from 'browserify';
10+
import source from 'vinyl-source-stream';
11+
12+
const copyright = `
13+
<%= pkg.title %>
14+
<%= pkg.homepage %>
15+
16+
Copyright <%= moment().format('YYYY') %> by <%= pkg.author %>
17+
18+
Licensed under the <%= pkg.license %> license:
19+
http://www.opensource.org/licenses/<%= pkg.license %>
20+
`;
21+
22+
function es2015() {
23+
return browserify('src/controller.js', { debug: false })
24+
.transform(babelify)
25+
.bundle()
26+
.pipe(source('controller.es2015.min.js'))
27+
.pipe(buffer())
28+
.pipe(uglify({
29+
compress: { drop_console: true },
30+
}))
31+
.pipe(headerComment(copyright))
32+
.pipe(sourcemaps.init({ loadMaps: true })) // load and init sourcemaps
33+
.pipe(sourcemaps.write('.')) // write sourcemaps
34+
.pipe(gulp.dest('dist'));
35+
}
36+
function build() {
37+
return gulp.src('src/controller.js')
38+
.pipe(buffer())
39+
.pipe(uglify_es({
40+
compress: { drop_console: true },
41+
}))
42+
.pipe(headerComment(copyright))
43+
.pipe(rename({ suffix: '.min' }))
44+
.pipe(sourcemaps.init({ loadMaps: true })) // load and init sourcemaps
45+
.pipe(sourcemaps.write('.')) // write sourcemaps
46+
.pipe(gulp.dest('dist'));
47+
}
48+
49+
gulp.task('es2015', es2015);
50+
gulp.task('build', build);
51+
gulp.task('default', ['build', 'es2015']);

package.json

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "gpslab-controller",
33
"version": "2.0.1",
44
"license": "MIT",
5-
"title": "Micro framework for JavaScript",
5+
"title": "GpsLab Controller",
66
"description": "GpsLab Controller is a JavaScript micro framework",
77
"keywords": [
88
"controller",
@@ -17,5 +17,20 @@
1717
},
1818
"bugs": {
1919
"url": "https://github.com/gpslab/gpslab-controller/issues"
20+
},
21+
"devDependencies": {
22+
"babel-core": "^6.26.3",
23+
"babel-polyfill": "^6.26.0",
24+
"babel-preset-es2015": "^6.24.1",
25+
"babelify": "^7.3.0",
26+
"browserify": "^14.4.0",
27+
"gulp": "^3.9.1",
28+
"gulp-buffer": "0.0.2",
29+
"gulp-header-comment": "^0.4.0",
30+
"gulp-rename": "^1.4.0",
31+
"gulp-sourcemaps": "^2.6.4",
32+
"gulp-uglify": "^2",
33+
"gulp-uglify-es": "^1.0.4",
34+
"vinyl-source-stream": "^1.1.2"
2035
}
2136
}

src/controller.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
* GpsLab Controller
33
* https://github.com/gpslab/gpslab-controller
44
*
5-
* Copyright 2017, Peter Gribanov
6-
* http://peter-gribanov.ru
5+
* Copyright 2018 by Peter Gribanov (http://peter-gribanov.ru)
76
*
87
* Licensed under the MIT license:
98
* http://www.opensource.org/licenses/MIT
@@ -64,10 +63,14 @@
6463

6564
/**
6665
* Binding the control for single specific element.
67-
* @param {HTMLElement} element
66+
* @param {Element} element
6867
* @returns {boolean}
6968
*/
7069
static singleBind(element) {
70+
if (!(element instanceof Element)) {
71+
throw new Error(`The element to be binding must be instance of Element, now it is "${typeof element}".`);
72+
}
73+
7174
if (!element.getAttribute('data-control')) {
7275
return false;
7376
}
@@ -89,11 +92,13 @@
8992

9093
/**
9194
* Find the controls in element and children elements and binding it.
92-
* @param {?HTMLElement} [element=null]
95+
* @param {Element} element
9396
* @returns {boolean}
9497
*/
9598
static bind(element) {
96-
element = element || document.getElementsByTagName('body')[0];
99+
if (!(element instanceof Element)) {
100+
throw new Error(`The element to be binding must be instance of Element, now it is "${typeof element}".`);
101+
}
97102

98103
let binded = Controller.singleBind(element);
99104

0 commit comments

Comments
 (0)