Project

General

Profile

1
$(function() {
2
    // characters/words counter
3
    altair_form_adv.char_words_counter();
4
    // ionrangeSlider
5
    altair_form_adv.rangeSlider();
6
    // advanced selects
7
    altair_form_adv.adv_selects();
8
    // masked_inputs
9
    altair_form_adv.masked_inputs();
10
    // date range
11
    altair_form_adv.date_range();
12
    // select2 template
13
    altair_form_adv.select2_template();
14
    // clear button
15
    altair_form_adv.clear_button();
16
});
17

    
18
altair_form_adv = {
19
    // characters/words counter
20
    char_words_counter: function() {
21
        var $imputCount = $('.input-count');
22
        if($imputCount.length) {
23
            /* http://qwertypants.github.io/jQuery-Word-and-Character-Counter-Plugin/ */
24
            (function($){"use strict";$.fn.extend({counter:function(options){var defaults={type:"char",count:"down",goal:140,text:true,target:false,append:true,translation:"",msg:"",container_class:""};var $countObj="",countIndex="",noLimit=false,options=$.extend({},defaults,options);var methods={init:function($obj){var objID=$obj.attr("id"),counterID=objID+"_count";methods.isLimitless();$countObj=$("<span id="+counterID+"/>");var counterDiv=$("<div/>").attr("id",objID+"_counter").append($countObj).append(" "+methods.setMsg());if(options.container_class&&options.container_class.length){counterDiv.addClass(options.container_class)}if(!options.target||!$(options.target).length){options.append?counterDiv.insertAfter($obj):counterDiv.insertBefore($obj)}else{options.append?$(options.target).append(counterDiv):$(options.target).prepend(counterDiv)}methods.bind($obj)},bind:function($obj){$obj.bind("keypress.counter keydown.counter keyup.counter blur.counter focus.counter change.counter paste.counter",methods.updateCounter);$obj.bind("keydown.counter",methods.doStopTyping);$obj.trigger("keydown")},isLimitless:function(){if(options.goal==="sky"){options.count="up";noLimit=true;return noLimit}},setMsg:function(){if(options.msg!==""){return options.msg}if(options.text===false){return""}if(noLimit){if(options.msg!==""){return options.msg}else{return""}}this.text=options.translation||"character word left max";this.text=this.text.split(" ");this.chars="s ( )".split(" ");this.msg=null;switch(options.type){case"char":if(options.count===defaults.count&&options.text){this.msg=this.text[0]+this.chars[1]+this.chars[0]+this.chars[2]+" "+this.text[2]}else if(options.count==="up"&&options.text){this.msg=this.text[0]+this.chars[0]+" "+this.chars[1]+options.goal+" "+this.text[3]+this.chars[2]}break;case"word":if(options.count===defaults.count&&options.text){this.msg=this.text[1]+this.chars[1]+this.chars[0]+this.chars[2]+" "+this.text[2]}else if(options.count==="up"&&options.text){this.msg=this.text[1]+this.chars[1]+this.chars[0]+this.chars[2]+" "+this.chars[1]+options.goal+" "+this.text[3]+this.chars[2]}break;default:}return this.msg},getWords:function(val){if(val!==""){return $.trim(val).replace(/\s+/g," ").split(" ").length}else{return 0}},updateCounter:function(e){var $this=$(this);if(countIndex<0||countIndex>options.goal){methods.passedGoal($this)}if(options.type===defaults.type){if(options.count===defaults.count){countIndex=options.goal-$this.val().length;if(countIndex<=0){$countObj.text("0")}else{$countObj.text(countIndex)}}else if(options.count==="up"){countIndex=$this.val().length;$countObj.text(countIndex)}}else if(options.type==="word"){if(options.count===defaults.count){countIndex=methods.getWords($this.val());if(countIndex<=options.goal){countIndex=options.goal-countIndex;$countObj.text(countIndex)}else{$countObj.text("0")}}else if(options.count==="up"){countIndex=methods.getWords($this.val());$countObj.text(countIndex)}}return},doStopTyping:function(e){var keys=[46,8,9,35,36,37,38,39,40,32];if(methods.isGoalReached(e)){if(e.keyCode!==keys[0]&&e.keyCode!==keys[1]&&e.keyCode!==keys[2]&&e.keyCode!==keys[3]&&e.keyCode!==keys[4]&&e.keyCode!==keys[5]&&e.keyCode!==keys[6]&&e.keyCode!==keys[7]&&e.keyCode!==keys[8]){if(options.type===defaults.type){return false}else if(e.keyCode!==keys[9]&&e.keyCode!==keys[1]&&options.type!=defaults.type){return true}else{return false}}}},isGoalReached:function(e,_goal){if(noLimit){return false}if(options.count===defaults.count){_goal=0;return countIndex<=_goal?true:false}else{_goal=options.goal;return countIndex>=_goal?true:false}},wordStrip:function(numOfWords,text){var wordCount=text.replace(/\s+/g," ").split(" ").length;text=$.trim(text);if(numOfWords<=0||numOfWords===wordCount){return text}else{text=$.trim(text).split(" ");text.splice(numOfWords,wordCount,"");return $.trim(text.join(" "))}},passedGoal:function($obj){var userInput=$obj.val();if(options.type==="word"){$obj.val(methods.wordStrip(options.goal,userInput))}if(options.type==="char"){$obj.val(userInput.substring(0,options.goal))}if(options.type==="down"){$countObj.val("0")}if(options.type==="up"){$countObj.val(options.goal)}}};return this.each(function(){methods.init($(this))})}})})(jQuery);
25

    
26
            $imputCount.each(function() {
27
                var $this = $(this);
28

    
29
                var $thisGoal = $(this).attr('maxlength') ? $(this).attr('maxlength') : 80 ;
30

    
31
                $this.counter({
32
                    container_class: 'text-count-wrapper',
33
                    msg: ' / '+$thisGoal,
34
                    goal: $thisGoal,
35
                    count: 'up'
36
                });
37

    
38
                if($this.closest('.md-input-wrapper').length) {
39
                    $this.closest('.md-input-wrapper').addClass('md-input-wrapper-count')
40
                }
41
            })
42
        }
43
    },
44
    // range slider
45
    rangeSlider: function() {
46

    
47
        $('.ion-slider').each(function() {
48
            $(this).val('').ionRangeSlider();
49
        });
50

    
51
        $("#ionslider_movement_limit").ionRangeSlider({
52
            type: "double",
53
            min: 0,
54
            max: 100,
55
            from: 20,
56
            from_min: 10,
57
            from_max: 30,
58
            from_shadow: true,
59
            to: 80,
60
            to_min: 70,
61
            to_max: 90,
62
            to_shadow: true,
63
            grid: true,
64
            grid_num: 10
65
        });
66

    
67
        $("#ionslider_date").ionRangeSlider({
68
            min: +moment().subtract(1, "years").format("X"),
69
            max: +moment().format("X"),
70
            from: +moment().subtract(6, "months").format("X"),
71
            force_edges: true,
72
            prettify: function (num) {
73
                return moment(num, "X").format("LL");
74
            }
75
        });
76
    },
77
    // advanced selects (selectizejs)
78
    adv_selects: function() {
79
        $('#select_adv_single').selectize({
80
            plugins: {
81
                'remove_button': {
82
                    label: ''
83
                }
84
            },
85
            onDropdownOpen: function($dropdown) {
86
                $dropdown
87
                    .hide()
88
                    .velocity('slideDown', {
89
                        begin: function() {
90
                            $dropdown.css({'margin-top':'0'})
91
                        },
92
                        duration: 200,
93
                        easing: easing_swiftOut
94
                    })
95
            },
96
            onDropdownClose: function($dropdown) {
97
                $dropdown
98
                    .show()
99
                    .velocity('slideUp', {
100
                        complete: function() {
101
                            $dropdown.css({'margin-top':''})
102
                        },
103
                        duration: 200,
104
                        easing: easing_swiftOut
105
                    })
106
            }
107
        });
108

    
109
        $('#select_adv_1').selectize({
110
            plugins: {
111
                'remove_button': {
112
                    label: ''
113
                },
114
                'drag_drop': {}
115
            },
116
            options: [
117
                {class: 'planet', id: 1, title: 'Mercury', url: 'http://en.wikipedia.org/wiki/Mercury_(planet)'},
118
                {class: 'planet', id: 2, title: 'Venus', url: 'http://en.wikipedia.org/wiki/Venus'},
119
                {class: 'planet', id: 3, title: 'Earth', url: 'http://en.wikipedia.org/wiki/Earth'},
120
                {class: 'planet', id: 4, title: 'Mars', url: 'http://en.wikipedia.org/wiki/Mars'},
121
                {class: 'planet', id: 5, title: 'Jupiter', url: 'http://en.wikipedia.org/wiki/Jupiter'},
122
                {class: 'planet', id: 6, title: 'Saturn', url: 'http://en.wikipedia.org/wiki/Saturn'},
123
                {class: 'planet', id: 7, title: 'Uranus', url: 'http://en.wikipedia.org/wiki/Uranus'},
124
                {class: 'planet', id: 8, title: 'Neptune', url: 'http://en.wikipedia.org/wiki/Neptune'},
125
                {class: 'star', id: 9, title: 'UY Scuti', url: 'https://en.wikipedia.org/wiki/UY_Scuti'},
126
                {class: 'star', id: 10, title: 'WOH G64', url: 'https://en.wikipedia.org/wiki/WOH_G64'},
127
                {class: 'star', id: 11, title: 'RW Cephei', url: 'https://en.wikipedia.org/wiki/RW_Cephei'},
128
                {class: 'star', id: 12, title: 'Westerlund 1-26', url: 'https://en.wikipedia.org/wiki/Westerlund_1-26'}
129
            ],
130
            optgroups: [
131
                {value: 'planet', label: 'Planets'},
132
                {value: 'star', label: 'Stars'}
133
            ],
134
            optgroupField: 'class',
135
            maxItems: null,
136
            valueField: 'id',
137
            labelField: 'title',
138
            searchField: 'title',
139
            create: false,
140
            render: {
141
                option: function(data, escape) {
142
                    return  '<div class="option">' +
143
                                '<span class="title">' + escape(data.title) + '</span>' +
144
                            '</div>';
145
                },
146
                item: function(data, escape) {
147
                    return '<div class="item"><a href="' + escape(data.url) + '" target="_blank">' + escape(data.title) + '</a></div>';
148
                },
149
                optgroup_header: function(data, escape) {
150
                    return '<div class="optgroup-header">' + escape(data.label) + '</div>';
151
                }
152
            },
153
            onDropdownOpen: function($dropdown) {
154
                $dropdown
155
                    .hide()
156
                    .velocity('slideDown', {
157
                        begin: function() {
158
                            $dropdown.css({'margin-top':'0'})
159
                        },
160
                        duration: 200,
161
                        easing: easing_swiftOut
162
                    })
163
            },
164
            onDropdownClose: function($dropdown) {
165
                $dropdown
166
                    .show()
167
                    .velocity('slideUp', {
168
                        complete: function() {
169
                            $dropdown.css({'margin-top':''})
170
                        },
171
                        duration: 200,
172
                        easing: easing_swiftOut
173
                    })
174
            }
175
        });
176

    
177
        var REGEX_EMAIL = '([a-z0-9!#$%&\'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&\'*+/=?^_`{|}~-]+)*@' +
178
            '(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)';
179
        $('#select_adv_2').selectize({
180
            plugins: {
181
                'remove_button': {
182
                    label: ''
183
                },
184
                'drag_drop': {}
185
            },
186
            persist: false,
187
            maxItems: null,
188
            valueField: 'email',
189
            labelField: 'name',
190
            searchField: ['name', 'email'],
191
            options: [
192
                {email: 'brian@thirdroute.com', name: 'Brian Reavis'},
193
                {email: 'nikola@tesla.com', name: 'Nikola Tesla'},
194
                {email: 'someone@gmail.com'}
195
            ],
196
            render: {
197
                item: function(item, escape) {
198
                    return '<div>' +
199
                        (item.name ? '<span class="name">' + escape(item.name) + '</span>' : '') +
200
                        (item.email ? '<span class="email">' + escape(item.email) + '</span>' : '') +
201
                        '</div>';
202
                },
203
                option: function(item, escape) {
204
                    var label = item.name || item.email;
205
                    var caption = item.name ? item.email : null;
206
                    return '<div>' +
207
                        '<span class="label">' + escape(label) + '</span>' +
208
                        (caption ? '<span class="caption">' + escape(caption) + '</span>' : '') +
209
                        '</div>';
210
                }
211
            },
212
            createFilter: function(input) {
213
                var match, regex;
214

    
215
                // email@address.com
216
                regex = new RegExp('^' + REGEX_EMAIL + '$', 'i');
217
                match = input.match(regex);
218
                if (match) return !this.options.hasOwnProperty(match[0]);
219

    
220
                // name <email@address.com>
221
                regex = new RegExp('^([^<]*)\<' + REGEX_EMAIL + '\>$', 'i');
222
                match = input.match(regex);
223
                if (match) return !this.options.hasOwnProperty(match[2]);
224

    
225
                return false;
226
            },
227
            create: function(input) {
228
                if ((new RegExp('^' + REGEX_EMAIL + '$', 'i')).test(input)) {
229
                    return {email: input};
230
                }
231
                var match = input.match(new RegExp('^([^<]*)\<' + REGEX_EMAIL + '\>$', 'i'));
232
                if (match) {
233
                    return {
234
                        email : match[2],
235
                        name  : $.trim(match[1])
236
                    };
237
                }
238
                alert('Invalid email address.');
239
                return false;
240
            },
241
            onDropdownOpen: function($dropdown) {
242
                $dropdown
243
                    .hide()
244
                    .velocity('slideDown', {
245
                        begin: function() {
246
                            $dropdown.css({'margin-top':'0'})
247
                        },
248
                        duration: 200,
249
                        easing: easing_swiftOut
250
                    })
251
            },
252
            onDropdownClose: function($dropdown) {
253
                $dropdown
254
                    .show()
255
                    .velocity('slideUp', {
256
                        complete: function() {
257
                            $dropdown.css({'margin-top':''})
258
                        },
259
                        duration: 200,
260
                        easing: easing_swiftOut
261
                    })
262
            }
263
        });
264

    
265
    },
266
    // masked_inputs
267
    masked_inputs: function() {
268
        $maskedInput = $('.masked_input');
269
        if($maskedInput.length) {
270
            $maskedInput.inputmask();
271
        }
272
    },
273
    // date range
274
    date_range: function() {
275
        var $dp_start = $('#uk_dp_start'),
276
            $dp_end = $('#uk_dp_end');
277

    
278
        var start_date = UIkit.datepicker($dp_start, {
279
            format:'DD.MM.YYYY'
280
        });
281

    
282
        var end_date = UIkit.datepicker($dp_end, {
283
            format:'DD.MM.YYYY'
284
        });
285

    
286
        $dp_start.on('change',function() {
287
            end_date.options.minDate = $dp_start.val();
288
            setTimeout(function() {
289
                $dp_end.focus();
290
            },300);
291
        });
292

    
293
        $dp_end.on('change',function() {
294
            start_date.options.maxDate = $dp_end.val();
295
        });
296
    },
297
    // select 2
298
    select2_template: function () {
299
        function formatState (state) {
300
            if (!state.id) {
301
                return state.text;
302
            }
303
            var baseUrl = "/user/pages/images/flags";
304
            var $state = $(
305
                '<span><i class="uk-margin-small-right flag flag-' + state.element.value + '"></i>' + state.text + '</span>'
306
            );
307
            return $state;
308
        };
309

    
310
        $(".js-select2-template").select2({
311
            templateResult: formatState,
312
            templateSelection: formatState
313
        });
314
    },
315
    // clear button
316
    clear_button: function () {
317
        $("#clear_button").addClear({
318
            closeSymbol: '<i class="material-icons">close</i>'
319
        });
320
    }
321
};
(21-21/114)