Thomas Bruederli
2014-08-14 29f7b272a54ccb268148c48c2eefb00748dc2512
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/** Zen Forms 1.0.3 | MIT License | git.io/zen-form */
 
(function ($) {
 
    $.fn.zenForm = function (settings) {
 
        settings = $.extend({
            trigger: '.go-zen',
            theme: 'dark'
        }, settings);
 
        /**
         * Helper functions
         */
        var Utils = {
 
            /**
             * (Un)Wrap body content to hide overflow
             */
            bodyWrap: function () {
 
                var $body = $('body'),
                    $wrap = $body.children('.zen-forms-body-wrap');
 
                if ($wrap.length) {
                    $wrap.children().unwrap();
                } else {
                    $body.wrapInner('<div class="zen-forms-body-wrap"/>');
                }
 
            }, // bodyWrap
 
            /**
             * Watch inputs and add "empty" class if needed
             */
            watchEmpty: function () {
 
                App.Environment.find('input, textarea, select').each(function () {
 
                   $(this).on('change', function () {
 
                        $(this)[$(this).val() ? 'removeClass' : 'addClass']('empty');
 
                   }).trigger('change');
 
                });
 
            },
 
            /**
             * Custom styled selects
             */
            customSelect: function ($select, $customSelect) {
 
                var $selected;
 
                $customSelect.on('click', function (event) {
 
                    event.stopPropagation();
 
                    $selected = $customSelect.find('.selected');
 
                    $customSelect.toggleClass('is-open');
 
                    if ($customSelect.hasClass('is-open')) {
                        $customSelect.scrollTop(
                            $selected.position().top - $selected.outerHeight()
                        );
                    }
 
 
                }).find('a').on('click', function () {
 
                    $(this).addClass('selected').siblings().removeClass('selected');
 
                    $select.val($(this).data('value'));
 
                });
 
            }, // customSelect
 
            /**
             * Hide any elements(mostly selects) when clicked outside them
             */
            manageSelects: function () {
 
                $(document).on('click', function () {
                    $('.is-open').removeClass('is-open');
                });
 
            }, // manageSelects
 
            /**
             * Hide any elements(mostly selects) when clicked outside them
             */
            focusFirst: function () {
 
                var $first = App.Environment.find('input').first();
 
                // we need to re-set value to remove focus selection
                $first.focus().val($first.val());
 
            } // focusFirst
 
        }, // Utils
 
        /**
         * Core functionality
         */
        App = {
 
            /**
             * Orginal form element
             */
            Form: null,
 
            /**
             * Wrapper element
             */
            Environment: null,
 
            /**
             * Functions to create and manipulate environment
             */
            env: {
 
 
                /**
                 * Object where elements created with App.env.addObject are appended
                 */
                wrapper: null,
 
                create: function () {
 
                    // Callback: zf-initialize
                    App.Form.trigger('zf-initialize');
 
                    Utils.bodyWrap();
 
                    App.Environment = $('<div>', {
                        class: 'zen-forms' + (settings.theme == 'dark' ? '' : ' light-theme')
                    }).hide().appendTo('body').fadeIn(200);
 
                    // ESC to exit. Thanks @ktmud
                    $('body').on('keydown', function (event) {
 
                        if (event.which == 27)
                            App.env.destroy($elements);
 
                    });
 
                    return App.Environment;
 
                }, // create
 
                /**
                 * Update orginal inputs with new values and destroy Environment
                 */
                destroy: function ($elements) {
 
                    // Callback: zf-destroy
                    App.Form.trigger('zf-destroy', App.Environment);
 
                    $('body').off('keydown');
 
                    // Update orginal inputs with new values
                    $elements.each(function (i) {
 
                        var $el = $('#zen-forms-input' + i);
 
                        if ($el.length) {
                            $(this).val($el.val());
                        }
 
                    });
 
                    Utils.bodyWrap();
 
                    // Hide and remove Environment
                    App.Environment.fadeOut(200, function () {
 
                        App.env.wrapper = null;
 
                        App.Environment.remove();
 
                    });
 
                    // Callback: zf-destroyed
                    App.Form.trigger('zf-destroyed');
 
                }, // destroy
 
                /**
                 * Append inputs, textareas to Environment
                 */
                add: function ($elements) {
 
                    var $el, $label, value, id, ID, label;
 
                    $elements.each(function (i) {
 
                        App.env.wrapper = App.env.createObject('div', {
                            class: 'zen-forms-input-wrap'
                        }).appendTo(App.Environment);
 
                        $el = $(this);
 
                        value = $el.val();
 
                        id = $el.attr('id');
 
                        ID = 'zen-forms-input' + i;
 
                        label = $el.data('label') || $("label[for=" + id + "]").text() || $el.attr('placeholder') || '';
 
                        // Exclude specified elements
                        if ($.inArray( $el.attr('type'), ['checkbox', 'radio', 'submit']) == -1) {
 
                            if ($el.is('input') )
                                App.env.addInput($el, ID, value);
                            else if ($el.is('select') )
                                App.env.addSelect($el, ID);
                            else
                                App.env.addTextarea($el, ID, value);
 
                            $label = App.env.addObject('label', {
                                for: ID,
                                text: label
                            });
 
                            if ($el.is('select') )
                                $label.prependTo(App.env.wrapper);
 
                        }
 
                    });
 
                    // Callback: zf-initialized
                    App.Form.trigger('zf-initialized', App.Environment);
 
                }, // add
 
                addInput: function ($input, ID, value) {
 
                    return App.env.addObject('input', {
                        id: ID,
                        value: value,
                        class: 'input',
                        type: $input.attr('type')
                    });
 
                }, // addInput
 
                addTextarea: function ($textarea, ID, value) {
 
                    return App.env.addObject('textarea', {
                        id: ID,
                        text: value,
                        rows: 5,
                        class: 'input'
                    });
 
                }, // addTextarea
 
                addSelect: function ($orginalSelect, ID) {
 
                    var $select = App.env.addObject('select', {
                            id: ID,
                            class: 'select'
                        }),
                        $options = $orginalSelect.find('option'),
                        $customSelect = App.env.addObject('div', {
                            class: 'custom-select-wrap',
                            html: '<div class="custom-select"></div>'
                        }).children();
 
                    $select.append($options.clone());
 
                    $.each($options, function (i, option) {
 
                        App.env.createObject('a', {
                            href: '#',
                            html: '<span>' + $(option).text() + '</span>' ,
                            'data-value': $(option).attr('value'),
                            class: $(option).prop('selected') ? 'selected' : ''
                        }).appendTo($customSelect);
 
                    });
 
                    $select.val($orginalSelect.val());
 
                    Utils.customSelect($select, $customSelect);
 
                    return $customSelect;
 
                }, // addSelect
 
                /**
                 * Wrapper for creating jQuery objects
                 */
                createObject: function (type, params, fn, fnMethod) {
 
                    return $('<'+type+'>', params).on(fnMethod || 'click', fn);
 
                }, // createObject
 
                /**
                 * Wrapper for adding jQuery objects to wrapper
                 */
                addObject: function (type, params, fn, fnMethod) {
 
                    return App.env.createObject(type, params, fn, fnMethod).appendTo(App.env.wrapper || App.Environment);
 
                }, // addObject
 
                switchTheme: function () {
 
                    App.Environment.toggleClass('light-theme');
 
                } // switchTheme
 
            }, // env
 
            zen: function ($elements) {
 
                // Create environment
                App.env.create();
 
                // Add wrapper div for close and theme buttons
                App.env.wrapper = App.env.createObject('div', {
                    class: 'zen-forms-header'
                }).appendTo(App.Environment);
 
                // Add close button
                App.env.addObject('a', {
                    class: 'zen-forms-close-button',
                    html: '<i class="zen-icon zen-icon--close"></i> Exit Zen Mode'
                }, function () {
                    App.env.destroy($elements);
                });
 
                // Add theme switch button
                App.env.addObject('a', {
                    class: 'zen-forms-theme-switch',
                    html: '<i class="zen-icon zen-icon--theme"></i> Switch theme'
                }, function () {
                    App.env.switchTheme();
                });
 
                // Add inputs and textareas from form
                App.env.add($elements);
 
                // Additional select functionality
                Utils.manageSelects();
 
                // Select first input
                Utils.focusFirst();
 
                // Add .empty class for empty inputs
                Utils.watchEmpty();
 
            } // zen
 
        }; // App
 
        App.Form = $(this);
 
        var $elements = App.Form.is('form') ? App.Form.find('input, textarea, select') : App.Form;
 
        $(settings.trigger).on('click', function (event) {
 
            event.preventDefault();
 
            App.zen($elements);
 
        });
 
        // Command: destroy
        App.Form.on('destroy', function () {
            App.env.destroy($elements);
        });
 
        // Command: init
        App.Form.on('init', function () {
            App.zen($elements);
        });
 
        return this;
 
    };
 
})(jQuery);