-
Notifications
You must be signed in to change notification settings - Fork 0
/
fdd.js
147 lines (118 loc) · 4.37 KB
/
fdd.js
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
jQuery.fn.extend({
dropDownFancy : function(opts) {
'use strict';
var _this = this;
this.$dropdown = jQuery(this);
this.optionsArgs = opts;
this.showImages = opts.showImages || false;
this.selectedValue = this.$dropdown.val();
this.$dropdownOptions = $('option', this.$dropdown);
this.cssClasses = {
mainClass : 'fancy-dropdown',
holderClass : 'container-fancy',
maskSelectedText: 'selected-text',
maskIconDropDown: 'selected-arrow',
maskImage : 'selected-image',
optionsList : 'list-options',
itemOfList : 'option'
};
this.$fancyWrapper = $('<div class="'+_this.cssClasses.mainClass + '"></div>');
this.$maskSelectedText = undefined;
this.$maskSelectedImg = undefined;
this.$maskDropDownIcon = undefined;
this.init = function() {
_this.$dropdown.hide();
_this.build();
_this.initialEvents();
_this.listPositionUpdate();
};
this.listPositionUpdate = function () {
$('.list-options', _this.$fancyWrapper).css({
'width':_this.$fancyWrapper.width(),
'top': _this.$fancyWrapper.position().top + parseInt(_this.$fancyWrapper.css('height'), 10),
'left': _this.$fancyWrapper.position().left
});
};
this.initialEvents = function() {
var $options = $('.option', _this.$fancyWrapper);
$options.each(function(index, item) {
$(item).on('click', function(evt) {
evt.preventDefault();
if (index !== 0) {
$(item).closest('.' + _this.cssClasses.holderClass).addClass('selected');
} else {
$(item).closest('.' + _this.cssClasses.holderClass).removeClass('selected');
}
_this.$maskSelectedText.html($(item).text());
_this.$maskSelectedImg.attr('src', 'assets/img/' + $(item).data('icon') );
_this.$dropdown.val($(item).text()).trigger('change');
_this.removeError();
});
});
_this.$fancyWrapper.on('click', function(evt) {
evt.preventDefault();
_this.listPositionUpdate();
var $container = $('.' + _this.cssClasses.holderClass, this);
if ($container.hasClass('active')) {
$container.removeClass('active');
} else {
$container.addClass('active');
}
});
$(this).on('mouseleave', function() {
var $container = $('.' + _this.cssClasses.holderClass, this);
$container.removeClass('active');
});
};
this.addError = function() {
var $container = $('.' + _this.cssClasses.holderClass, _this.$fancyWrapper);
$container.addClass('error');
};
this.removeError = function() {
var $container = $('.' + _this.cssClasses.holderClass, _this.$fancyWrapper);
$container.removeClass('error');
};
this.build = function () {
var $mask = '',
optionsList = [],
createOption = function ($item) {
var $option = '<li class="' + _this.cssClasses.itemOfList + '" data-icon="';
$option += $item.data('icon') + '">';
$option += $item.val() + '</li>';
return $option;
},
createSelectedItemMask = function ($item) {
var $selectedItemMask;
$selectedItemMask = '<div class="' + _this.cssClasses.holderClass + '">';
if (_this.optionsArgs.showImages === true) {
$selectedItemMask += '<img src="assets/img/';
$selectedItemMask += $item.data('icon') + '" class="' + _this.cssClasses.maskImage + '"/>';
}
$selectedItemMask += '<span class="' + _this.cssClasses.maskSelectedText + '">' + $item.val() + '</span>';
$selectedItemMask += '<span class="' + _this.cssClasses.maskIconDropDown + '"></span>';
$selectedItemMask += '</div>';
return $selectedItemMask;
},
collectSelectors = function() {
_this.$maskSelectedText = $('.' + _this.cssClasses.maskSelectedText, _this.$fancyWrapper);
_this.$maskSelectedImg = $('.' + _this.cssClasses.maskImage, _this.$fancyWrapper);
_this.$maskDropDownIcon = $('.' + _this.cssClasses.maskIconDropDown, _this.$fancyWrapper);
};
_this.$dropdownOptions.each(function (index, item) {
if ($(item).val() === _this.selectedValue) {
$mask = createSelectedItemMask($(item));
}
optionsList.push(createOption($(item)));
});
optionsList.unshift('<ul class="' + _this.cssClasses.optionsList + '">');
optionsList.push('</ul>');
_this.$fancyWrapper.html($mask);
_this.$fancyWrapper.insertAfter(_this.$dropdown);
$('.' + _this.cssClasses.holderClass ,_this.$fancyWrapper).append(optionsList.join(''));
collectSelectors();
return this;
};
this.init();
return this;
}
});