mtl 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (158) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +12 -0
  3. data/Gemfile +9 -0
  4. data/LICENSE.txt +22 -0
  5. data/README.md +104 -0
  6. data/Rakefile +6 -0
  7. data/app/assets/javascripts/mtl/clickables.coffee +25 -0
  8. data/app/assets/javascripts/mtl/collapsible.coffee +43 -0
  9. data/app/assets/javascripts/mtl/configuration.coffee.erb +1 -0
  10. data/app/assets/javascripts/mtl/document_modal.coffee +130 -0
  11. data/app/assets/javascripts/mtl/dropdown.coffee +24 -0
  12. data/app/assets/javascripts/mtl/hooks.coffee +14 -0
  13. data/app/assets/javascripts/mtl/icon.coffee.erb +17 -0
  14. data/app/assets/javascripts/mtl/modal.coffee +32 -0
  15. data/app/assets/javascripts/mtl/select.coffee +44 -0
  16. data/app/assets/javascripts/mtl/templates.coffee +4 -0
  17. data/app/assets/javascripts/mtl/toc.coffee +31 -0
  18. data/app/assets/javascripts/mtl.js +42 -0
  19. data/app/assets/stylesheets/mtl/all.scss +69 -0
  20. data/app/assets/stylesheets/mtl/extend/_avatars.scss +37 -0
  21. data/app/assets/stylesheets/mtl/extend/_buttons.scss +31 -0
  22. data/app/assets/stylesheets/mtl/extend/_cards.scss +21 -0
  23. data/app/assets/stylesheets/mtl/extend/_chips.scss +3 -0
  24. data/app/assets/stylesheets/mtl/extend/_collection-files.scss +108 -0
  25. data/app/assets/stylesheets/mtl/extend/_document-modals.scss +95 -0
  26. data/app/assets/stylesheets/mtl/extend/_dropdown.scss +12 -0
  27. data/app/assets/stylesheets/mtl/extend/_forms.scss +66 -0
  28. data/app/assets/stylesheets/mtl/extend/_global.scss +77 -0
  29. data/app/assets/stylesheets/mtl/extend/_grid.scss +25 -0
  30. data/app/assets/stylesheets/mtl/extend/_material-icons.scss +37 -0
  31. data/app/assets/stylesheets/mtl/extend/_mixins.scss +10 -0
  32. data/app/assets/stylesheets/mtl/extend/_roboto-rails.scss +49 -0
  33. data/app/assets/stylesheets/mtl/extend/_side-nav.scss +22 -0
  34. data/app/assets/stylesheets/mtl/extend/_toc.scss +12 -0
  35. data/app/assets/stylesheets/mtl/extend/_typography.scss +32 -0
  36. data/app/assets/stylesheets/mtl/extend/forms/_input-fields.scss +50 -0
  37. data/app/assets/stylesheets/mtl/layouts/_default.scss +220 -0
  38. data/app/assets/stylesheets/mtl/layouts/_single.scss +25 -0
  39. data/app/views/mtl/header.html.erb +25 -0
  40. data/bin/coffeelint.rb +16 -0
  41. data/lib/generators/mtl/install_generator.rb +20 -0
  42. data/lib/generators/mtl/templates/_color.scss +410 -0
  43. data/lib/generators/mtl/templates/_variables.scss +330 -0
  44. data/lib/generators/mtl/templates/mtl.scss +8 -0
  45. data/lib/generators/mtl/templates/simple_form.rb +211 -0
  46. data/lib/mtl/rails/card_file_presenter.rb +70 -0
  47. data/lib/mtl/rails/view_helpers.rb +412 -0
  48. data/lib/mtl/simple_form/suffix.rb +52 -0
  49. data/lib/mtl/version.rb +6 -0
  50. data/lib/mtl.rb +38 -0
  51. data/mtl.gemspec +35 -0
  52. data/package.json +11 -0
  53. data/spec/mtl/rails/card_file_presenter_spec.rb +126 -0
  54. data/spec/mtl/rails/view_helpers_spec.rb +193 -0
  55. data/spec/mtl/simple_form/suffix_spec.rb +39 -0
  56. data/spec/mtl_spec.rb +34 -0
  57. data/spec/spec_helper.rb +18 -0
  58. data/spec/support/dom.rb +15 -0
  59. data/vendor/assets/fonts/material-icons/MaterialIcons-Regular.eot +0 -0
  60. data/vendor/assets/fonts/material-icons/MaterialIcons-Regular.ijmap +1 -0
  61. data/vendor/assets/fonts/material-icons/MaterialIcons-Regular.svg +2373 -0
  62. data/vendor/assets/fonts/material-icons/MaterialIcons-Regular.ttf +0 -0
  63. data/vendor/assets/fonts/material-icons/MaterialIcons-Regular.woff +0 -0
  64. data/vendor/assets/fonts/material-icons/MaterialIcons-Regular.woff2 +0 -0
  65. data/vendor/assets/fonts/roboto/Roboto-Bold.eot +0 -0
  66. data/vendor/assets/fonts/roboto/Roboto-Bold.ttf +0 -0
  67. data/vendor/assets/fonts/roboto/Roboto-Bold.woff +0 -0
  68. data/vendor/assets/fonts/roboto/Roboto-Bold.woff2 +0 -0
  69. data/vendor/assets/fonts/roboto/Roboto-Light.eot +0 -0
  70. data/vendor/assets/fonts/roboto/Roboto-Light.ttf +0 -0
  71. data/vendor/assets/fonts/roboto/Roboto-Light.woff +0 -0
  72. data/vendor/assets/fonts/roboto/Roboto-Light.woff2 +0 -0
  73. data/vendor/assets/fonts/roboto/Roboto-Medium.eot +0 -0
  74. data/vendor/assets/fonts/roboto/Roboto-Medium.ttf +0 -0
  75. data/vendor/assets/fonts/roboto/Roboto-Medium.woff +0 -0
  76. data/vendor/assets/fonts/roboto/Roboto-Medium.woff2 +0 -0
  77. data/vendor/assets/fonts/roboto/Roboto-Regular.eot +0 -0
  78. data/vendor/assets/fonts/roboto/Roboto-Regular.ttf +0 -0
  79. data/vendor/assets/fonts/roboto/Roboto-Regular.woff +0 -0
  80. data/vendor/assets/fonts/roboto/Roboto-Regular.woff2 +0 -0
  81. data/vendor/assets/fonts/roboto/Roboto-Thin.eot +0 -0
  82. data/vendor/assets/fonts/roboto/Roboto-Thin.ttf +0 -0
  83. data/vendor/assets/fonts/roboto/Roboto-Thin.woff +0 -0
  84. data/vendor/assets/fonts/roboto/Roboto-Thin.woff2 +0 -0
  85. data/vendor/assets/javascripts/lodash.js +16607 -0
  86. data/vendor/assets/javascripts/materialize/animation.js +9 -0
  87. data/vendor/assets/javascripts/materialize/buttons.js +91 -0
  88. data/vendor/assets/javascripts/materialize/cards.js +26 -0
  89. data/vendor/assets/javascripts/materialize/carousel.js +454 -0
  90. data/vendor/assets/javascripts/materialize/character_counter.js +72 -0
  91. data/vendor/assets/javascripts/materialize/chips.js +267 -0
  92. data/vendor/assets/javascripts/materialize/collapsible.js +160 -0
  93. data/vendor/assets/javascripts/materialize/date_picker/picker.date.js +1430 -0
  94. data/vendor/assets/javascripts/materialize/date_picker/picker.js +1123 -0
  95. data/vendor/assets/javascripts/materialize/dropdown.js +265 -0
  96. data/vendor/assets/javascripts/materialize/forms.js +681 -0
  97. data/vendor/assets/javascripts/materialize/global.js +45 -0
  98. data/vendor/assets/javascripts/materialize/hammer.min.js +1 -0
  99. data/vendor/assets/javascripts/materialize/init.js +173 -0
  100. data/vendor/assets/javascripts/materialize/initial.js +11 -0
  101. data/vendor/assets/javascripts/materialize/jquery.easing.1.3.js +205 -0
  102. data/vendor/assets/javascripts/materialize/jquery.hammer.js +33 -0
  103. data/vendor/assets/javascripts/materialize/jquery.timeago.min.js +1 -0
  104. data/vendor/assets/javascripts/materialize/leanModal.js +192 -0
  105. data/vendor/assets/javascripts/materialize/materialbox.js +269 -0
  106. data/vendor/assets/javascripts/materialize/parallax.js +58 -0
  107. data/vendor/assets/javascripts/materialize/prism.js +8 -0
  108. data/vendor/assets/javascripts/materialize/pushpin.js +71 -0
  109. data/vendor/assets/javascripts/materialize/scrollFire.js +48 -0
  110. data/vendor/assets/javascripts/materialize/scrollspy.js +283 -0
  111. data/vendor/assets/javascripts/materialize/sideNav.js +352 -0
  112. data/vendor/assets/javascripts/materialize/slider.js +321 -0
  113. data/vendor/assets/javascripts/materialize/tabs.js +148 -0
  114. data/vendor/assets/javascripts/materialize/toasts.js +136 -0
  115. data/vendor/assets/javascripts/materialize/tooltip.js +230 -0
  116. data/vendor/assets/javascripts/materialize/transitions.js +169 -0
  117. data/vendor/assets/javascripts/materialize/velocity.min.js +5 -0
  118. data/vendor/assets/javascripts/materialize/waves.js +338 -0
  119. data/vendor/assets/javascripts/pdfobject.js +254 -0
  120. data/vendor/assets/stylesheets/materialize/_buttons.scss +211 -0
  121. data/vendor/assets/stylesheets/materialize/_cards.scss +185 -0
  122. data/vendor/assets/stylesheets/materialize/_carousel.scss +85 -0
  123. data/vendor/assets/stylesheets/materialize/_chips.scss +74 -0
  124. data/vendor/assets/stylesheets/materialize/_collapsible.scss +90 -0
  125. data/vendor/assets/stylesheets/materialize/_color.scss +412 -0
  126. data/vendor/assets/stylesheets/materialize/_dropdown.scss +57 -0
  127. data/vendor/assets/stylesheets/materialize/_global.scss +781 -0
  128. data/vendor/assets/stylesheets/materialize/_grid.scss +147 -0
  129. data/vendor/assets/stylesheets/materialize/_icons-material-design.scss +5 -0
  130. data/vendor/assets/stylesheets/materialize/_materialbox.scss +42 -0
  131. data/vendor/assets/stylesheets/materialize/_mixins.scss +5 -0
  132. data/vendor/assets/stylesheets/materialize/_modal.scss +90 -0
  133. data/vendor/assets/stylesheets/materialize/_navbar.scss +182 -0
  134. data/vendor/assets/stylesheets/materialize/_normalize.scss +424 -0
  135. data/vendor/assets/stylesheets/materialize/_prefixer.scss +384 -0
  136. data/vendor/assets/stylesheets/materialize/_preloader.scss +334 -0
  137. data/vendor/assets/stylesheets/materialize/_roboto.scss +49 -0
  138. data/vendor/assets/stylesheets/materialize/_sideNav.scss +219 -0
  139. data/vendor/assets/stylesheets/materialize/_slider.scss +92 -0
  140. data/vendor/assets/stylesheets/materialize/_table_of_contents.scss +33 -0
  141. data/vendor/assets/stylesheets/materialize/_tabs.scss +56 -0
  142. data/vendor/assets/stylesheets/materialize/_toast.scss +65 -0
  143. data/vendor/assets/stylesheets/materialize/_tooltip.scss +32 -0
  144. data/vendor/assets/stylesheets/materialize/_typography.scss +61 -0
  145. data/vendor/assets/stylesheets/materialize/_variables.scss +313 -0
  146. data/vendor/assets/stylesheets/materialize/_waves.scss +177 -0
  147. data/vendor/assets/stylesheets/materialize/date_picker/_default.date.scss +435 -0
  148. data/vendor/assets/stylesheets/materialize/date_picker/_default.scss +201 -0
  149. data/vendor/assets/stylesheets/materialize/date_picker/_default.time.scss +125 -0
  150. data/vendor/assets/stylesheets/materialize/forms/_checkboxes.scss +220 -0
  151. data/vendor/assets/stylesheets/materialize/forms/_file-input.scss +38 -0
  152. data/vendor/assets/stylesheets/materialize/forms/_forms.scss +22 -0
  153. data/vendor/assets/stylesheets/materialize/forms/_input-fields.scss +273 -0
  154. data/vendor/assets/stylesheets/materialize/forms/_radio-buttons.scss +119 -0
  155. data/vendor/assets/stylesheets/materialize/forms/_range.scss +159 -0
  156. data/vendor/assets/stylesheets/materialize/forms/_select.scss +116 -0
  157. data/vendor/assets/stylesheets/materialize/forms/_switches.scss +78 -0
  158. metadata +309 -0
@@ -0,0 +1,681 @@
1
+ (function ($) {
2
+ $(document).ready(function() {
3
+
4
+ // Function to update labels of text fields
5
+ Materialize.updateTextFields = function() {
6
+ var input_selector = 'input[type=text], input[type=password], input[type=email], input[type=url], input[type=tel], input[type=number], input[type=search], textarea';
7
+ $(input_selector).each(function(index, element) {
8
+ if ($(element).val().length > 0 || element.autofocus ||$(this).attr('placeholder') !== undefined || $(element)[0].validity.badInput === true) {
9
+ $(this).siblings('label').addClass('active');
10
+ }
11
+ else {
12
+ $(this).siblings('label').removeClass('active');
13
+ }
14
+ });
15
+ };
16
+
17
+ // Text based inputs
18
+ var input_selector = 'input[type=text], input[type=password], input[type=email], input[type=url], input[type=tel], input[type=number], input[type=search], textarea';
19
+
20
+ // Add active if form auto complete
21
+ $(document).on('change', input_selector, function () {
22
+ if($(this).val().length !== 0 || $(this).attr('placeholder') !== undefined) {
23
+ $(this).siblings('label').addClass('active');
24
+ }
25
+ validate_field($(this));
26
+ });
27
+
28
+ // Add active if input element has been pre-populated on document ready
29
+ $(document).ready(function() {
30
+ Materialize.updateTextFields();
31
+ });
32
+
33
+ // HTML DOM FORM RESET handling
34
+ $(document).on('reset', function(e) {
35
+ var formReset = $(e.target);
36
+ if (formReset.is('form')) {
37
+ formReset.find(input_selector).removeClass('valid').removeClass('invalid');
38
+ formReset.find(input_selector).each(function () {
39
+ if ($(this).attr('value') === '') {
40
+ $(this).siblings('label').removeClass('active');
41
+ }
42
+ });
43
+
44
+ // Reset select
45
+ formReset.find('select.initialized').each(function () {
46
+ var reset_text = formReset.find('option[selected]').text();
47
+ formReset.siblings('input.select-dropdown').val(reset_text);
48
+ });
49
+ }
50
+ });
51
+
52
+ // Add active when element has focus
53
+ $(document).on('focus', input_selector, function () {
54
+ $(this).siblings('label, .prefix').addClass('active');
55
+ });
56
+
57
+ $(document).on('blur', input_selector, function () {
58
+ var $inputElement = $(this);
59
+ var selector = ".prefix";
60
+
61
+ if ($inputElement.val().length === 0 && $inputElement[0].validity.badInput !== true && $inputElement.attr('placeholder') === undefined) {
62
+ selector += ", label";
63
+ }
64
+
65
+ $inputElement.siblings(selector).removeClass('active');
66
+
67
+ validate_field($inputElement);
68
+ });
69
+
70
+ window.validate_field = function(object) {
71
+ var hasLength = object.attr('length') !== undefined;
72
+ var lenAttr = parseInt(object.attr('length'));
73
+ var len = object.val().length;
74
+
75
+ if (object.val().length === 0 && object[0].validity.badInput === false) {
76
+ if (object.hasClass('validate')) {
77
+ object.removeClass('valid');
78
+ object.removeClass('invalid');
79
+ }
80
+ }
81
+ else {
82
+ if (object.hasClass('validate')) {
83
+ // Check for character counter attributes
84
+ if ((object.is(':valid') && hasLength && (len <= lenAttr)) || (object.is(':valid') && !hasLength)) {
85
+ object.removeClass('invalid');
86
+ object.addClass('valid');
87
+ }
88
+ else {
89
+ object.removeClass('valid');
90
+ object.addClass('invalid');
91
+ }
92
+ }
93
+ }
94
+ };
95
+
96
+ // Radio and Checkbox focus class
97
+ var radio_checkbox = 'input[type=radio], input[type=checkbox]';
98
+ $(document).on('keyup.radio', radio_checkbox, function(e) {
99
+ // TAB, check if tabbing to radio or checkbox.
100
+ if (e.which === 9) {
101
+ $(this).addClass('tabbed');
102
+ var $this = $(this);
103
+ $this.one('blur', function(e) {
104
+
105
+ $(this).removeClass('tabbed');
106
+ });
107
+ return;
108
+ }
109
+ });
110
+
111
+ // Textarea Auto Resize
112
+ var hiddenDiv = $('.hiddendiv').first();
113
+ if (!hiddenDiv.length) {
114
+ hiddenDiv = $('<div class="hiddendiv common"></div>');
115
+ $('body').append(hiddenDiv);
116
+ }
117
+ var text_area_selector = '.materialize-textarea';
118
+
119
+ function textareaAutoResize($textarea) {
120
+ // Set font properties of hiddenDiv
121
+
122
+ var fontFamily = $textarea.css('font-family');
123
+ var fontSize = $textarea.css('font-size');
124
+ var lineHeight = $textarea.css('line-height');
125
+
126
+ if (fontSize) { hiddenDiv.css('font-size', fontSize); }
127
+ if (fontFamily) { hiddenDiv.css('font-family', fontFamily); }
128
+ if (lineHeight) { hiddenDiv.css('line-height', lineHeight); }
129
+
130
+ if ($textarea.attr('wrap') === "off") {
131
+ hiddenDiv.css('overflow-wrap', "normal")
132
+ .css('white-space', "pre");
133
+ }
134
+
135
+ hiddenDiv.text($textarea.val() + '\n');
136
+ var content = hiddenDiv.html().replace(/\n/g, '<br>');
137
+ hiddenDiv.html(content);
138
+
139
+
140
+ // When textarea is hidden, width goes crazy.
141
+ // Approximate with half of window size
142
+
143
+ if ($textarea.is(':visible')) {
144
+ hiddenDiv.css('width', $textarea.width());
145
+ }
146
+ else {
147
+ hiddenDiv.css('width', $(window).width()/2);
148
+ }
149
+
150
+ $textarea.css('height', hiddenDiv.height());
151
+ }
152
+
153
+ $(text_area_selector).each(function () {
154
+ var $textarea = $(this);
155
+ if ($textarea.val().length) {
156
+ textareaAutoResize($textarea);
157
+ }
158
+ });
159
+
160
+ $('body').on('keyup keydown autoresize', text_area_selector, function () {
161
+ textareaAutoResize($(this));
162
+ });
163
+
164
+ // File Input Path
165
+ $(document).on('change', '.file-field input[type="file"]', function () {
166
+ var file_field = $(this).closest('.file-field');
167
+ var path_input = file_field.find('input.file-path');
168
+ var files = $(this)[0].files;
169
+ var file_names = [];
170
+ for (var i = 0; i < files.length; i++) {
171
+ file_names.push(files[i].name);
172
+ }
173
+ path_input.val(file_names.join(", "));
174
+ path_input.trigger('change');
175
+ });
176
+
177
+ /****************
178
+ * Range Input *
179
+ ****************/
180
+
181
+ var range_type = 'input[type=range]';
182
+ var range_mousedown = false;
183
+ var left;
184
+
185
+ $(range_type).each(function () {
186
+ var thumb = $('<span class="thumb"><span class="value"></span></span>');
187
+ $(this).after(thumb);
188
+ });
189
+
190
+ var range_wrapper = '.range-field';
191
+ $(document).on('change', range_type, function(e) {
192
+ var thumb = $(this).siblings('.thumb');
193
+ thumb.find('.value').html($(this).val());
194
+ });
195
+
196
+ $(document).on('input mousedown touchstart', range_type, function(e) {
197
+ var thumb = $(this).siblings('.thumb');
198
+ var width = $(this).outerWidth();
199
+
200
+ // If thumb indicator does not exist yet, create it
201
+ if (thumb.length <= 0) {
202
+ thumb = $('<span class="thumb"><span class="value"></span></span>');
203
+ $(this).after(thumb);
204
+ }
205
+
206
+ // Set indicator value
207
+ thumb.find('.value').html($(this).val());
208
+
209
+ range_mousedown = true;
210
+ $(this).addClass('active');
211
+
212
+ if (!thumb.hasClass('active')) {
213
+ thumb.velocity({ height: "30px", width: "30px", top: "-20px", marginLeft: "-15px"}, { duration: 300, easing: 'easeOutExpo' });
214
+ }
215
+
216
+ if (e.type !== 'input') {
217
+ if(e.pageX === undefined || e.pageX === null){//mobile
218
+ left = e.originalEvent.touches[0].pageX - $(this).offset().left;
219
+ }
220
+ else{ // desktop
221
+ left = e.pageX - $(this).offset().left;
222
+ }
223
+ if (left < 0) {
224
+ left = 0;
225
+ }
226
+ else if (left > width) {
227
+ left = width;
228
+ }
229
+ thumb.addClass('active').css('left', left);
230
+ }
231
+
232
+ thumb.find('.value').html($(this).val());
233
+ });
234
+
235
+ $(document).on('mouseup touchend', range_wrapper, function() {
236
+ range_mousedown = false;
237
+ $(this).removeClass('active');
238
+ });
239
+
240
+ $(document).on('mousemove touchmove', range_wrapper, function(e) {
241
+ var thumb = $(this).children('.thumb');
242
+ var left;
243
+ if (range_mousedown) {
244
+ if (!thumb.hasClass('active')) {
245
+ thumb.velocity({ height: '30px', width: '30px', top: '-20px', marginLeft: '-15px'}, { duration: 300, easing: 'easeOutExpo' });
246
+ }
247
+ if (e.pageX === undefined || e.pageX === null) { //mobile
248
+ left = e.originalEvent.touches[0].pageX - $(this).offset().left;
249
+ }
250
+ else{ // desktop
251
+ left = e.pageX - $(this).offset().left;
252
+ }
253
+ var width = $(this).outerWidth();
254
+
255
+ if (left < 0) {
256
+ left = 0;
257
+ }
258
+ else if (left > width) {
259
+ left = width;
260
+ }
261
+ thumb.addClass('active').css('left', left);
262
+ thumb.find('.value').html(thumb.siblings(range_type).val());
263
+ }
264
+ });
265
+
266
+ $(document).on('mouseout touchleave', range_wrapper, function() {
267
+ if (!range_mousedown) {
268
+
269
+ var thumb = $(this).children('.thumb');
270
+
271
+ if (thumb.hasClass('active')) {
272
+ thumb.velocity({ height: '0', width: '0', top: '10px', marginLeft: '-6px'}, { duration: 100 });
273
+ }
274
+ thumb.removeClass('active');
275
+ }
276
+ });
277
+
278
+ /**************************
279
+ * Auto complete plugin *
280
+ *************************/
281
+ $.fn.autocomplete = function (options) {
282
+ // Defaults
283
+ var defaults = {
284
+ data: {}
285
+ };
286
+
287
+ options = $.extend(defaults, options);
288
+
289
+ return this.each(function() {
290
+ var $input = $(this);
291
+ var data = options.data,
292
+ $inputDiv = $input.closest('.input-field'); // Div to append on
293
+
294
+ // Check if data isn't empty
295
+ if (!$.isEmptyObject(data)) {
296
+ // Create autocomplete element
297
+ var $autocomplete = $('<ul class="autocomplete-content dropdown-content"></ul>');
298
+
299
+ // Append autocomplete element
300
+ if ($inputDiv.length) {
301
+ $inputDiv.append($autocomplete); // Set ul in body
302
+ } else {
303
+ $input.after($autocomplete);
304
+ }
305
+
306
+ var highlight = function(string, $el) {
307
+ var img = $el.find('img');
308
+ var matchStart = $el.text().toLowerCase().indexOf("" + string.toLowerCase() + ""),
309
+ matchEnd = matchStart + string.length - 1,
310
+ beforeMatch = $el.text().slice(0, matchStart),
311
+ matchText = $el.text().slice(matchStart, matchEnd + 1),
312
+ afterMatch = $el.text().slice(matchEnd + 1);
313
+ $el.html("<span>" + beforeMatch + "<span class='highlight'>" + matchText + "</span>" + afterMatch + "</span>");
314
+ if (img.length) {
315
+ $el.prepend(img);
316
+ }
317
+ };
318
+
319
+ // Perform search
320
+ $input.on('keyup', function (e) {
321
+ // Capture Enter
322
+ if (e.which === 13) {
323
+ $autocomplete.find('li').first().click();
324
+ return;
325
+ }
326
+
327
+ var val = $input.val().toLowerCase();
328
+ $autocomplete.empty();
329
+
330
+ // Check if the input isn't empty
331
+ if (val !== '') {
332
+ for(var key in data) {
333
+ if (data.hasOwnProperty(key) &&
334
+ key.toLowerCase().indexOf(val) !== -1 &&
335
+ key.toLowerCase() !== val) {
336
+ var autocompleteOption = $('<li></li>');
337
+ if(!!data[key]) {
338
+ autocompleteOption.append('<img src="'+ data[key] +'" class="right circle"><span>'+ key +'</span>');
339
+ } else {
340
+ autocompleteOption.append('<span>'+ key +'</span>');
341
+ }
342
+ $autocomplete.append(autocompleteOption);
343
+
344
+ highlight(val, autocompleteOption);
345
+ }
346
+ }
347
+ }
348
+ });
349
+
350
+ // Set input value
351
+ $autocomplete.on('click', 'li', function () {
352
+ $input.val($(this).text().trim());
353
+ $autocomplete.empty();
354
+ });
355
+ }
356
+ });
357
+ };
358
+
359
+ }); // End of $(document).ready
360
+
361
+ /*******************
362
+ * Select Plugin *
363
+ ******************/
364
+ $.fn.material_select = function (callback) {
365
+ $(this).each(function(){
366
+ var $select = $(this);
367
+
368
+ if ($select.hasClass('browser-default')) {
369
+ return; // Continue to next (return false breaks out of entire loop)
370
+ }
371
+
372
+ var multiple = $select.attr('multiple') ? true : false,
373
+ lastID = $select.data('select-id'); // Tear down structure if Select needs to be rebuilt
374
+
375
+ if (lastID) {
376
+ $select.parent().find('span.caret').remove();
377
+ $select.parent().find('input').remove();
378
+
379
+ $select.unwrap();
380
+ $('ul#select-options-'+lastID).remove();
381
+ }
382
+
383
+ // If destroying the select, remove the selelct-id and reset it to it's uninitialized state.
384
+ if(callback === 'destroy') {
385
+ $select.data('select-id', null).removeClass('initialized');
386
+ return;
387
+ }
388
+
389
+ var uniqueID = Materialize.guid();
390
+ $select.data('select-id', uniqueID);
391
+ var wrapper = $('<div class="select-wrapper"></div>');
392
+ wrapper.addClass($select.attr('class'));
393
+ var options = $('<ul id="select-options-' + uniqueID +'" class="dropdown-content select-dropdown ' + (multiple ? 'multiple-select-dropdown' : '') + '"></ul>'),
394
+ selectChildren = $select.children('option, optgroup'),
395
+ valuesSelected = [],
396
+ optionsHover = false;
397
+
398
+ var label = $select.find('option:selected').html() || $select.find('option:first').html() || "";
399
+
400
+ // Function that renders and appends the option taking into
401
+ // account type and possible image icon.
402
+ var appendOptionWithIcon = function(select, option, type) {
403
+ // Add disabled attr if disabled
404
+ var disabledClass = (option.is(':disabled')) ? 'disabled ' : '';
405
+ var optgroupClass = (type === 'optgroup-option') ? 'optgroup-option ' : '';
406
+
407
+ // add icons
408
+ var icon_url = option.data('icon');
409
+ var classes = option.attr('class');
410
+ if (!!icon_url) {
411
+ var classString = '';
412
+ if (!!classes) classString = ' class="' + classes + '"';
413
+
414
+ // Check for multiple type.
415
+ if (type === 'multiple') {
416
+ options.append($('<li class="' + disabledClass + '"><img src="' + icon_url + '"' + classString + '><span><input type="checkbox"' + disabledClass + '/><label></label>' + option.html() + '</span></li>'));
417
+ } else {
418
+ options.append($('<li class="' + disabledClass + optgroupClass + '"><img src="' + icon_url + '"' + classString + '><span>' + option.html() + '</span></li>'));
419
+ }
420
+ return true;
421
+ }
422
+
423
+ // Check for multiple type.
424
+ if (type === 'multiple') {
425
+ options.append($('<li class="' + disabledClass + '"><span><input type="checkbox"' + disabledClass + '/><label></label>' + option.html() + '</span></li>'));
426
+ } else {
427
+ options.append($('<li class="' + disabledClass + optgroupClass + '"><span>' + option.html() + '</span></li>'));
428
+ }
429
+ };
430
+
431
+ /* Create dropdown structure. */
432
+ if (selectChildren.length) {
433
+ selectChildren.each(function() {
434
+ if ($(this).is('option')) {
435
+ // Direct descendant option.
436
+ if (multiple) {
437
+ appendOptionWithIcon($select, $(this), 'multiple');
438
+
439
+ } else {
440
+ appendOptionWithIcon($select, $(this));
441
+ }
442
+ } else if ($(this).is('optgroup')) {
443
+ // Optgroup.
444
+ var selectOptions = $(this).children('option');
445
+ options.append($('<li class="optgroup"><span>' + $(this).attr('label') + '</span></li>'));
446
+
447
+ selectOptions.each(function() {
448
+ appendOptionWithIcon($select, $(this), 'optgroup-option');
449
+ });
450
+ }
451
+ });
452
+ }
453
+
454
+ options.find('li:not(.optgroup)').each(function (i) {
455
+ $(this).click(function (e) {
456
+ // Check if option element is disabled
457
+ if (!$(this).hasClass('disabled') && !$(this).hasClass('optgroup')) {
458
+ var selected = true;
459
+
460
+ if (multiple) {
461
+ $('input[type="checkbox"]', this).prop('checked', function(i, v) { return !v; });
462
+ selected = toggleEntryFromArray(valuesSelected, $(this).index(), $select);
463
+ $newSelect.trigger('focus');
464
+ } else {
465
+ options.find('li').removeClass('active');
466
+ $(this).toggleClass('active');
467
+ $newSelect.val($(this).text());
468
+ }
469
+
470
+ activateOption(options, $(this));
471
+ $select.find('option').eq(i).prop('selected', selected);
472
+ // Trigger onchange() event
473
+ $select.trigger('change');
474
+ if (typeof callback !== 'undefined') callback();
475
+ }
476
+
477
+ e.stopPropagation();
478
+ });
479
+ });
480
+
481
+ // Wrap Elements
482
+ $select.wrap(wrapper);
483
+ // Add Select Display Element
484
+ var dropdownIcon = $('<span class="caret">&#9660;</span>');
485
+ if ($select.is(':disabled'))
486
+ dropdownIcon.addClass('disabled');
487
+
488
+ // escape double quotes
489
+ var sanitizedLabelHtml = label.replace(/"/g, '&quot;');
490
+
491
+ var $newSelect = $('<input type="text" class="select-dropdown" readonly="true" ' + (($select.is(':disabled')) ? 'disabled' : '') + ' data-activates="select-options-' + uniqueID +'" value="'+ sanitizedLabelHtml +'"/>');
492
+ $select.before($newSelect);
493
+ $newSelect.before(dropdownIcon);
494
+
495
+ $newSelect.after(options);
496
+ // Check if section element is disabled
497
+ if (!$select.is(':disabled')) {
498
+ $newSelect.dropdown({'hover': false, 'closeOnClick': false});
499
+ }
500
+
501
+ // Copy tabindex
502
+ if ($select.attr('tabindex')) {
503
+ $($newSelect[0]).attr('tabindex', $select.attr('tabindex'));
504
+ }
505
+
506
+ $select.addClass('initialized');
507
+
508
+ $newSelect.on({
509
+ 'focus': function (){
510
+ if ($('ul.select-dropdown').not(options[0]).is(':visible')) {
511
+ $('input.select-dropdown').trigger('close');
512
+ }
513
+ if (!options.is(':visible')) {
514
+ $(this).trigger('open', ['focus']);
515
+ var label = $(this).val();
516
+ var selectedOption = options.find('li').filter(function() {
517
+ return $(this).text().toLowerCase() === label.toLowerCase();
518
+ })[0];
519
+ activateOption(options, selectedOption);
520
+ }
521
+ },
522
+ 'click': function (e){
523
+ e.stopPropagation();
524
+ }
525
+ });
526
+
527
+ $newSelect.on('blur', function() {
528
+ if (!multiple) {
529
+ $(this).trigger('close');
530
+ }
531
+ options.find('li.selected').removeClass('selected');
532
+ });
533
+
534
+ options.hover(function() {
535
+ optionsHover = true;
536
+ }, function () {
537
+ optionsHover = false;
538
+ });
539
+
540
+ $(window).on({
541
+ 'click': function () {
542
+ multiple && (optionsHover || $newSelect.trigger('close'));
543
+ }
544
+ });
545
+
546
+ // Add initial multiple selections.
547
+ if (multiple) {
548
+ $select.find("option:selected:not(:disabled)").each(function () {
549
+ var index = $(this).index();
550
+
551
+ toggleEntryFromArray(valuesSelected, index, $select);
552
+ options.find("li").eq(index).find(":checkbox").prop("checked", true);
553
+ });
554
+ }
555
+
556
+ // Make option as selected and scroll to selected position
557
+ var activateOption = function(collection, newOption) {
558
+ if (newOption) {
559
+ collection.find('li.selected').removeClass('selected');
560
+ var option = $(newOption);
561
+ option.addClass('selected');
562
+ options.scrollTo(option);
563
+ }
564
+ };
565
+
566
+ // Allow user to search by typing
567
+ // this array is cleared after 1 second
568
+ var filterQuery = [],
569
+ onKeyDown = function(e){
570
+ // TAB - switch to another input
571
+ if(e.which == 9){
572
+ $newSelect.trigger('close');
573
+ return;
574
+ }
575
+
576
+ // ARROW DOWN WHEN SELECT IS CLOSED - open select options
577
+ if(e.which == 40 && !options.is(':visible')){
578
+ $newSelect.trigger('open');
579
+ return;
580
+ }
581
+
582
+ // ENTER WHEN SELECT IS CLOSED - submit form
583
+ if(e.which == 13 && !options.is(':visible')){
584
+ return;
585
+ }
586
+
587
+ e.preventDefault();
588
+
589
+ // CASE WHEN USER TYPE LETTERS
590
+ var letter = String.fromCharCode(e.which).toLowerCase(),
591
+ nonLetters = [9,13,27,38,40];
592
+ if (letter && (nonLetters.indexOf(e.which) === -1)) {
593
+ filterQuery.push(letter);
594
+
595
+ var string = filterQuery.join(''),
596
+ newOption = options.find('li').filter(function() {
597
+ return $(this).text().toLowerCase().indexOf(string) === 0;
598
+ })[0];
599
+
600
+ if (newOption) {
601
+ activateOption(options, newOption);
602
+ }
603
+ }
604
+
605
+ // ENTER - select option and close when select options are opened
606
+ if (e.which == 13) {
607
+ var activeOption = options.find('li.selected:not(.disabled)')[0];
608
+ if(activeOption){
609
+ $(activeOption).trigger('click');
610
+ if (!multiple) {
611
+ $newSelect.trigger('close');
612
+ }
613
+ }
614
+ }
615
+
616
+ // ARROW DOWN - move to next not disabled option
617
+ if (e.which == 40) {
618
+ if (options.find('li.selected').length) {
619
+ newOption = options.find('li.selected').next('li:not(.disabled)')[0];
620
+ } else {
621
+ newOption = options.find('li:not(.disabled)')[0];
622
+ }
623
+ activateOption(options, newOption);
624
+ }
625
+
626
+ // ESC - close options
627
+ if (e.which == 27) {
628
+ $newSelect.trigger('close');
629
+ }
630
+
631
+ // ARROW UP - move to previous not disabled option
632
+ if (e.which == 38) {
633
+ newOption = options.find('li.selected').prev('li:not(.disabled)')[0];
634
+ if(newOption)
635
+ activateOption(options, newOption);
636
+ }
637
+
638
+ // Automaticaly clean filter query so user can search again by starting letters
639
+ setTimeout(function(){ filterQuery = []; }, 1000);
640
+ };
641
+
642
+ $newSelect.on('keydown', onKeyDown);
643
+ });
644
+
645
+ function toggleEntryFromArray(entriesArray, entryIndex, select) {
646
+ var index = entriesArray.indexOf(entryIndex),
647
+ notAdded = index === -1;
648
+
649
+ if (notAdded) {
650
+ entriesArray.push(entryIndex);
651
+ } else {
652
+ entriesArray.splice(index, 1);
653
+ }
654
+
655
+ select.siblings('ul.dropdown-content').find('li').eq(entryIndex).toggleClass('active');
656
+
657
+ // use notAdded instead of true (to detect if the option is selected or not)
658
+ select.find('option').eq(entryIndex).prop('selected', notAdded);
659
+ setValueToInput(entriesArray, select);
660
+
661
+ return notAdded;
662
+ }
663
+
664
+ function setValueToInput(entriesArray, select) {
665
+ var value = '';
666
+
667
+ for (var i = 0, count = entriesArray.length; i < count; i++) {
668
+ var text = select.find('option').eq(entriesArray[i]).text();
669
+
670
+ i === 0 ? value += text : value += ', ' + text;
671
+ }
672
+
673
+ if (value === '') {
674
+ value = select.find('option:disabled').eq(0).text();
675
+ }
676
+
677
+ select.siblings('input.select-dropdown').val(value);
678
+ }
679
+ };
680
+
681
+ }( jQuery ));