Skip to content

Commit 3dd4669

Browse files
committedMay 21, 2016
Bundler and sourcemaps, made test commonjs aware.
1 parent 6446162 commit 3dd4669

7 files changed

+206
-208
lines changed
 

‎dist/rmodal.cjs.js

-192
This file was deleted.

‎dist/rmodal.cjs.js.map

-1
This file was deleted.

‎gulpfile.js

+3-10
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,9 @@ gulp.task('css', () => {
5353
gulp.task('cjs', () => {
5454
return bundle('cjs')
5555
.pipe(rename((path) => {
56-
path.basename += '.cjs';
56+
path.basename = 'index';
5757
}))
58-
.pipe(sourcemaps.write('.'))
59-
.pipe(gulp.dest('dist'));
58+
.pipe(gulp.dest('./'));
6059
})
6160

6261
gulp.task('js', [ 'cjs' ], () => {
@@ -84,13 +83,7 @@ gulp.task('build', [
8483
'css', 'jsmin'
8584
]);
8685

87-
gulp.task('pretest', () => {
88-
return bundle('iife')
89-
.pipe(sourcemaps.write())
90-
.pipe(gulp.dest('test'));
91-
});
92-
93-
gulp.task('test', [ 'pretest' ], (done) => {
86+
gulp.task('test', [ 'cjs' ], (done) => {
9487
new karma(
9588
{
9689
configFile: `${__dirname}/karma.conf.js`

‎index.js

+191-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,191 @@
1-
module.exports = require('./dist/rmodal.cjs.js');
1+
var is = function (obj, type) { return Object.prototype.toString.call(obj).toLowerCase() === ("[object " + type + "]"); };
2+
3+
var addClass = function (el, cls) {
4+
var arr = el.className
5+
.split(/\s+/)
6+
.filter(function (c) { return !!c && c == cls; });
7+
8+
if (!arr.length) {
9+
el.className += " " + cls;
10+
}
11+
}
12+
13+
var removeClass = function (el, cls) {
14+
el.className = el.className
15+
.split(/\s+/)
16+
.filter(function (c) { return !!c && c != cls; })
17+
.join(' ');
18+
}
19+
20+
var RModal = function RModal(el, opts) {
21+
var this$1 = this;
22+
23+
this.opened = false;
24+
25+
this.opts = {
26+
bodyClass: 'modal-open'
27+
, dialogClass: 'modal-dialog'
28+
, dialogOpenClass: 'bounceInDown'
29+
, dialogCloseClass: 'bounceOutUp'
30+
31+
, focus: true
32+
, focusElements: [
33+
'a[href]', 'area[href]', 'input:not([disabled]):not([type=hidden])'
34+
, 'button:not([disabled])', 'select:not([disabled])'
35+
, 'textarea:not([disabled])', 'iframe', 'object', 'embed'
36+
, '*[tabindex]', '*[contenteditable]'
37+
]
38+
39+
, escapeClose: true
40+
, content: null
41+
};
42+
43+
Object.keys(opts || {})
44+
.forEach(function (key) {
45+
/* istanbul ignore else */
46+
if (opts[key] !== undefined) {
47+
this$1.opts[key] = opts[key];
48+
}
49+
});
50+
51+
this.overlay = el;
52+
this.dialog = el.querySelector(("." + (this.opts.dialogClass)));
53+
54+
if (this.opts.content) {
55+
this.content(this.opts.content);
56+
}
57+
};
58+
59+
RModal.prototype.open = function open(content) {
60+
var this$1 = this;
61+
62+
this.content(content);
63+
64+
if (!is(this.opts.beforeOpen, 'function')) {
65+
return this._doOpen();
66+
}
67+
68+
this.opts.beforeOpen(function () {
69+
this$1._doOpen();
70+
});
71+
};
72+
73+
RModal.prototype._doOpen = function _doOpen() {
74+
addClass(document.body, this.opts.bodyClass);
75+
76+
removeClass(this.dialog, this.opts.dialogCloseClass);
77+
addClass(this.dialog, this.opts.dialogOpenClass);
78+
79+
this.overlay.style.display = 'block';
80+
81+
if (this.opts.focus) {
82+
this.focusOutElement = document.activeElement;
83+
this.focus();
84+
}
85+
86+
if (is(this.opts.afterOpen, 'function')) {
87+
this.opts.afterOpen();
88+
}
89+
this.opened = true;
90+
};
91+
92+
RModal.prototype.close = function close() {
93+
var this$1 = this;
94+
95+
if (!is(this.opts.beforeClose, 'function')) {
96+
return this._doClose();
97+
}
98+
99+
this.opts.beforeClose(function () {
100+
this$1._doClose();
101+
});
102+
};
103+
104+
RModal.prototype._doClose = function _doClose() {
105+
var this$1 = this;
106+
107+
removeClass(this.dialog, this.opts.dialogOpenClass);
108+
addClass(this.dialog, this.opts.dialogCloseClass);
109+
110+
removeClass(document.body, this.opts.bodyClass);
111+
112+
if (this.opts.focus) {
113+
this.focus(this.focusOutElement);
114+
}
115+
116+
if (is(this.opts.afterClose, 'function')) {
117+
this.opts.afterClose();
118+
}
119+
120+
this.opened = false;
121+
setTimeout(function () {
122+
this$1.overlay.style.display = 'none';
123+
}, 500);
124+
};
125+
126+
RModal.prototype.content = function content(content) {
127+
if (content === undefined) {
128+
return this.dialog.innerHTML;
129+
}
130+
131+
this.dialog.innerHTML = content;
132+
};
133+
134+
RModal.prototype.elements = function elements(selector, fallback) {
135+
fallback = fallback || window.navigator.appVersion.indexOf('MSIE 9.0') > -1;
136+
selector = is(selector, 'array') ? selector.join(',') : selector;
137+
138+
return [].filter.call(
139+
this.dialog.querySelectorAll(selector)
140+
, function (element) {
141+
if (fallback) {
142+
var style = window.getComputedStyle(element);
143+
return style.display !== 'none' && style.visibility !== 'hidden';
144+
}
145+
146+
return element.offsetParent !== null;
147+
}
148+
);
149+
};
150+
151+
RModal.prototype.focus = function focus(el) {
152+
el = el || this.elements(this.opts.focusElements)[0] || this.dialog.firstChild;
153+
154+
if (el && is(el.focus, 'function')) {
155+
el.focus();
156+
}
157+
};
158+
159+
RModal.prototype.keydown = function keydown(ev) {
160+
if (this.opts.escapeClose && ev.which == 27) {
161+
this.close();
162+
}
163+
164+
function stopEvent() {
165+
ev.preventDefault();
166+
ev.stopPropagation();
167+
}
168+
169+
if (this.opened && ev.which == 9 && this.dialog.contains(ev.target)) {
170+
var elements = this.elements(this.opts.focusElements)
171+
, first = elements[0]
172+
, last = elements[elements.length - 1];
173+
174+
if (first == last) {
175+
stopEvent();
176+
}
177+
else if (ev.target == first && ev.shiftKey) {
178+
stopEvent();
179+
last.focus();
180+
}
181+
else if (ev.target == last && !ev.shiftKey) {
182+
stopEvent();
183+
first.focus();
184+
}
185+
}
186+
};
187+
188+
RModal.prototype.version = '1.0.20';
189+
RModal.version = '1.0.20';
190+
191+
module.exports = RModal;

‎karma.conf.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,22 @@ module.exports = function(config) {
88
'mocha'
99
, 'chai'
1010
, 'sinon'
11+
, 'commonjs'
1112
]
1213

1314
, files: [
14-
'test/rmodal.js'
15-
, 'test/rmodal.test.js'
15+
'test/*.test.js'
16+
, './index.js'
1617
]
1718

1819
, exclude: []
1920

2021
, preprocessors: {
21-
'test/rmodal.js': [ 'coverage' ]
22+
'test/*.test.js': [ 'commonjs' ]
23+
, './index.js': [
24+
'coverage'
25+
, 'commonjs'
26+
]
2227
}
2328

2429
, reporters: [

‎package.json

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"istanbul-coveralls": "^1.0.3",
4141
"karma": "^0.13.22",
4242
"karma-chai": "^0.1.0",
43+
"karma-commonjs": "^1.0.0",
4344
"karma-coverage": "^1.0.0",
4445
"karma-coveralls": "^1.1.2",
4546
"karma-mocha": "^1.0.1",

‎test/rmodal.test.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
'use strict';
22

3+
var RModal = require(__dirname + '/../index.js');
4+
35
describe('RModal', function() {
46
var elBody
57
, elOverlay
@@ -629,6 +631,6 @@ describe('RModal', function() {
629631
});
630632

631633
it('should export RModal constructor', function() {
632-
expect(window.RModal).to.be.a('function');
634+
expect(RModal).to.be.a('function');
633635
});
634636
});

0 commit comments

Comments
 (0)
Please sign in to comment.