uikit-rails 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,133 @@
1
+
2
+ /**
3
+ * Expose `Confirmation`.
4
+ */
5
+
6
+ exports.Confirmation = Confirmation;
7
+
8
+ /**
9
+ * Return a new `Confirmation` dialog with the given
10
+ * `title` and `msg`.
11
+ *
12
+ * @param {String} title or msg
13
+ * @param {String} msg
14
+ * @return {Dialog}
15
+ * @api public
16
+ */
17
+
18
+ exports.confirm = function(title, msg){
19
+ switch (arguments.length) {
20
+ case 2:
21
+ return new Confirmation({ title: title, message: msg });
22
+ case 1:
23
+ return new Confirmation({ message: title });
24
+ }
25
+ };
26
+
27
+ /**
28
+ * Initialize a new `Confirmation` dialog.
29
+ *
30
+ * Options:
31
+ *
32
+ * - `title` dialog title
33
+ * - `message` a message to display
34
+ *
35
+ * Emits:
36
+ *
37
+ * - `cancel` the user pressed cancel or closed the dialog
38
+ * - `ok` the user clicked ok
39
+ * - `show` when visible
40
+ * - `hide` when hidden
41
+ *
42
+ * @param {Object} options
43
+ * @api public
44
+ */
45
+
46
+ function Confirmation(options) {
47
+ ui.Dialog.call(this, options);
48
+ };
49
+
50
+ /**
51
+ * Inherit from `Dialog.prototype`.
52
+ */
53
+
54
+ Confirmation.prototype = new ui.Dialog;
55
+
56
+ /**
57
+ * Change "cancel" button `text`.
58
+ *
59
+ * @param {String} text
60
+ * @return {Confirmation}
61
+ * @api public
62
+ */
63
+
64
+ Confirmation.prototype.cancel = function(text){
65
+ this.el.find('.cancel').text(text);
66
+ return this;
67
+ };
68
+
69
+ /**
70
+ * Change "ok" button `text`.
71
+ *
72
+ * @param {String} text
73
+ * @return {Confirmation}
74
+ * @api public
75
+ */
76
+
77
+ Confirmation.prototype.ok = function(text){
78
+ this.el.find('.ok').text(text);
79
+ return this;
80
+ };
81
+
82
+ /**
83
+ * Show the confirmation dialog and invoke `fn(ok)`.
84
+ *
85
+ * @param {Function} fn
86
+ * @return {Confirmation} for chaining
87
+ * @api public
88
+ */
89
+
90
+ Confirmation.prototype.show = function(fn){
91
+ ui.Dialog.prototype.show.call(this);
92
+ this.el.find('.ok').focus();
93
+ this.callback = fn || function(){};
94
+ return this;
95
+ };
96
+
97
+ /**
98
+ * Render with the given `options`.
99
+ *
100
+ * Emits "cancel" event.
101
+ * Emits "ok" event.
102
+ *
103
+ * @param {Object} options
104
+ * @api public
105
+ */
106
+
107
+ Confirmation.prototype.render = function(options){
108
+ ui.Dialog.prototype.render.call(this, options);
109
+ var self = this
110
+ , actions = $(html);
111
+
112
+ this.el.addClass('confirmation');
113
+ this.el.append(actions);
114
+
115
+ this.on('close', function(){
116
+ self.emit('cancel');
117
+ self.callback(false);
118
+ });
119
+
120
+ actions.find('.cancel').click(function(e){
121
+ e.preventDefault();
122
+ self.emit('cancel');
123
+ self.callback(false);
124
+ self.hide();
125
+ });
126
+
127
+ actions.find('.ok').click(function(e){
128
+ e.preventDefault();
129
+ self.emit('ok');
130
+ self.callback(true);
131
+ self.hide();
132
+ });
133
+ };
@@ -0,0 +1,252 @@
1
+
2
+ /**
3
+ * Active dialog.
4
+ */
5
+
6
+ var active;
7
+
8
+ /**
9
+ * Expose `Dialog`.
10
+ */
11
+
12
+ exports.Dialog = Dialog;
13
+
14
+ /**
15
+ * Return a new `Dialog` with the given
16
+ * (optional) `title` and `msg`.
17
+ *
18
+ * @param {String} title or msg
19
+ * @param {String} msg
20
+ * @return {Dialog}
21
+ * @api public
22
+ */
23
+
24
+ exports.dialog = function(title, msg){
25
+ switch (arguments.length) {
26
+ case 2:
27
+ return new Dialog({ title: title, message: msg });
28
+ case 1:
29
+ return new Dialog({ message: title });
30
+ }
31
+ };
32
+
33
+ /**
34
+ * Initialize a new `Dialog`.
35
+ *
36
+ * Options:
37
+ *
38
+ * - `title` dialog title
39
+ * - `message` a message to display
40
+ *
41
+ * Emits:
42
+ *
43
+ * - `show` when visible
44
+ * - `hide` when hidden
45
+ *
46
+ * @param {Object} options
47
+ * @api public
48
+ */
49
+
50
+ function Dialog(options) {
51
+ ui.Emitter.call(this);
52
+ options = options || {};
53
+ this.template = html;
54
+ this.el = $(this.template);
55
+ this.render(options);
56
+ if (active && !active.hiding) active.hide();
57
+ if (Dialog.effect) this.effect(Dialog.effect);
58
+ active = this;
59
+ };
60
+
61
+ /**
62
+ * Inherit from `Emitter.prototype`.
63
+ */
64
+
65
+ Dialog.prototype = new ui.Emitter;
66
+
67
+ /**
68
+ * Render with the given `options`.
69
+ *
70
+ * @param {Object} options
71
+ * @api public
72
+ */
73
+
74
+ Dialog.prototype.render = function(options){
75
+ var el = this.el
76
+ , title = options.title
77
+ , msg = options.message
78
+ , self = this;
79
+
80
+ el.find('.close').click(function(){
81
+ self.emit('close');
82
+ self.hide();
83
+ return false;
84
+ });
85
+
86
+ el.find('h1').text(title);
87
+ if (!title) el.find('h1').remove();
88
+
89
+ // message
90
+ if ('string' == typeof msg) {
91
+ el.find('p').text(msg);
92
+ } else if (msg) {
93
+ el.find('p').replaceWith(msg.el || msg);
94
+ }
95
+
96
+ setTimeout(function(){
97
+ el.removeClass('hide');
98
+ }, 0);
99
+ };
100
+
101
+ /**
102
+ * Enable the dialog close link.
103
+ *
104
+ * @return {Dialog} for chaining
105
+ * @api public
106
+ */
107
+
108
+ Dialog.prototype.closable = function(){
109
+ this.el.addClass('closable');
110
+ return this;
111
+ };
112
+
113
+ /**
114
+ * Set the effect to `type`.
115
+ *
116
+ * @param {String} type
117
+ * @return {Dialog} for chaining
118
+ * @api public
119
+ */
120
+
121
+ Dialog.prototype.effect = function(type){
122
+ this._effect = type;
123
+ this.el.addClass(type);
124
+ return this;
125
+ };
126
+
127
+ /**
128
+ * Make it modal!
129
+ *
130
+ * @return {Dialog} for chaining
131
+ * @api public
132
+ */
133
+
134
+ Dialog.prototype.modal = function(){
135
+ this._overlay = ui.overlay();
136
+ return this;
137
+ };
138
+
139
+ /**
140
+ * Add an overlay.
141
+ *
142
+ * @return {Dialog} for chaining
143
+ * @api public
144
+ */
145
+
146
+ Dialog.prototype.overlay = function(){
147
+ var self = this;
148
+ this._overlay = ui
149
+ .overlay({ closable: true })
150
+ .on('hide', function(){
151
+ self.closedOverlay = true;
152
+ self.hide();
153
+ });
154
+ return this;
155
+ };
156
+
157
+ /**
158
+ * Close the dialog when the escape key is pressed.
159
+ *
160
+ * @api private
161
+ */
162
+
163
+ Dialog.prototype.escapable = function(){
164
+ var self = this;
165
+ $(document).bind('keydown.dialog', function(e){
166
+ if (27 != e.which) return;
167
+ $(this).unbind('keydown.dialog');
168
+ self.hide();
169
+ });
170
+ };
171
+
172
+ /**
173
+ * Show the dialog.
174
+ *
175
+ * Emits "show" event.
176
+ *
177
+ * @return {Dialog} for chaining
178
+ * @api public
179
+ */
180
+
181
+ Dialog.prototype.show = function(){
182
+ var overlay = this._overlay;
183
+
184
+ this.emit('show');
185
+
186
+ if (overlay) {
187
+ overlay.show();
188
+ this.el.addClass('modal');
189
+ }
190
+
191
+ // escape
192
+ if (!overlay || overlay.closable) this.escapable();
193
+
194
+ this.el.appendTo('body');
195
+ this.el.css({ marginLeft: -(this.el.width() / 2) + 'px' });
196
+ this.emit('show');
197
+ return this;
198
+ };
199
+
200
+ /**
201
+ * Hide the dialog with optional delay of `ms`,
202
+ * otherwise the dialog is removed immediately.
203
+ *
204
+ * Emits "hide" event.
205
+ *
206
+ * @return {Number} ms
207
+ * @return {Dialog} for chaining
208
+ * @api public
209
+ */
210
+
211
+ Dialog.prototype.hide = function(ms){
212
+ var self = this;
213
+
214
+ // prevent thrashing
215
+ this.hiding = true;
216
+
217
+ // duration
218
+ if (ms) {
219
+ setTimeout(function(){
220
+ self.hide();
221
+ }, ms);
222
+ return this;
223
+ }
224
+
225
+ // hide / remove
226
+ this.el.addClass('hide');
227
+ if (this._effect) {
228
+ setTimeout(function(){
229
+ self.remove();
230
+ }, 500);
231
+ } else {
232
+ self.remove();
233
+ }
234
+
235
+ // modal
236
+ if (this._overlay && !self.closedOverlay) this._overlay.hide();
237
+
238
+ return this;
239
+ };
240
+
241
+ /**
242
+ * Hide the dialog without potential animation.
243
+ *
244
+ * @return {Dialog} for chaining
245
+ * @api public
246
+ */
247
+
248
+ Dialog.prototype.remove = function(){
249
+ this.emit('hide');
250
+ this.el.remove();
251
+ return this;
252
+ };
@@ -0,0 +1,99 @@
1
+ /**
2
+ * Expose `Emitter`.
3
+ */
4
+
5
+ exports.Emitter = Emitter;
6
+
7
+ /**
8
+ * Initialize a new `Emitter`.
9
+ *
10
+ * @api public
11
+ */
12
+
13
+ function Emitter() {
14
+ this.callbacks = {};
15
+ };
16
+
17
+ /**
18
+ * Listen on the given `event` with `fn`.
19
+ *
20
+ * @param {String} event
21
+ * @param {Function} fn
22
+ * @return {Emitter}
23
+ * @api public
24
+ */
25
+
26
+ Emitter.prototype.on = function(event, fn){
27
+ (this.callbacks[event] = this.callbacks[event] || [])
28
+ .push(fn);
29
+ return this;
30
+ };
31
+
32
+ /**
33
+ * Adds an `event` listener that will be invoked a single
34
+ * time then automatically removed.
35
+ *
36
+ * @param {String} event
37
+ * @param {Function} fn
38
+ * @return {Emitter}
39
+ * @api public
40
+ */
41
+
42
+ Emitter.prototype.once = function(event, fn){
43
+ var self = this;
44
+
45
+ function on() {
46
+ self.off(event, on);
47
+ fn.apply(this, arguments);
48
+ }
49
+
50
+ this.on(event, on);
51
+ return this;
52
+ };
53
+
54
+ /**
55
+ * Remove the given callback for `event` or all
56
+ * registered callbacks.
57
+ *
58
+ * @param {String} event
59
+ * @param {Function} fn
60
+ * @return {Emitter}
61
+ * @api public
62
+ */
63
+
64
+ Emitter.prototype.off = function(event, fn){
65
+ var callbacks = this.callbacks[event];
66
+ if (!callbacks) return this;
67
+
68
+ // remove all handlers
69
+ if (1 == arguments.length) {
70
+ delete this.callbacks[event];
71
+ return this;
72
+ }
73
+
74
+ // remove specific handler
75
+ var i = callbacks.indexOf(fn);
76
+ callbacks.splice(i, 1);
77
+ return this;
78
+ };
79
+
80
+ /**
81
+ * Emit `event` with the given args.
82
+ *
83
+ * @param {String} event
84
+ * @param {Mixed} ...
85
+ * @return {Emitter}
86
+ */
87
+
88
+ Emitter.prototype.emit = function(event){
89
+ var args = [].slice.call(arguments, 1)
90
+ , callbacks = this.callbacks[event];
91
+
92
+ if (callbacks) {
93
+ for (var i = 0, len = callbacks.length; i < len; ++i) {
94
+ callbacks[i].apply(this, args);
95
+ }
96
+ }
97
+
98
+ return this;
99
+ };