-
Notifications
You must be signed in to change notification settings - Fork 7
/
duration-picker.js
153 lines (141 loc) · 5.19 KB
/
duration-picker.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
148
149
150
151
152
153
/**
* Created by Tartarus762 on 10/13/16.
*/
(function ($) {
// Constructor for durationpicker 'class'
var durationPicker = function (element, options) {
this.settings = options;
this.stages = get_stages(this.settings);
this.template = generate_template(this.settings, this.stages);
this.jqitem = $(this.template);
this.jqchildren = this.jqitem.children();
this.element = $(element);
this.setup();
this.resize();
this.jqchildren.find(".durationpicker-duration").trigger('change');
var _self = this;
};
durationPicker.prototype = {
constructor: durationPicker,
setup: function () {
this.element.before(this.jqitem);
this.element.hide();
this.jqchildren.find(".durationpicker-duration").on('change', {ths: this}, function (ev) {
var element = ev.data.ths.element;
var value = "";
$(this).parent().parent().find('input').each(function () {
var input = $(this);
var val = 0;
if (input.val() != null && input.val() != ""){
val = input.val();
}
value += val + " " + input.next().text() + ",";
});
value = value.slice(0, -1);
element.val(value);
});
$(".durationpicker-duration").trigger('change');
window.addEventListener('resize', this.resize.bind(this));
},
resize: function() {
if (!this.settings.responsive) {
return
}
var padding = parseInt(this.jqitem.css('padding-left').split('px')[0]) + parseInt(this.jqitem.css('padding-right').split('px')[0]);
var minwidth = padding;
var minheight = padding;
this.jqchildren.each(function () {
var ths = $(this);
minwidth = minwidth + ths.outerWidth();
minheight = minheight + ths.outerHeight();
});
if (this.jqitem.parent().width() < minwidth) {
this.jqchildren.each(function () {
var ths = $(this);
ths.css('display', 'block');
});
this.jqitem.css('height', minheight)
}
else {
this.jqchildren.each(function () {
var ths = $(this);
ths.css('display', 'inline-block');
});
}
},
getitem: function () {
return this.jqitem;
},
setvalues: function (values) {
set_values(values, this)
$(".durationpicker-duration").trigger('change');
},
disable: function () {
this.jqchildren.children("input").each(function (index, item) {
item.readOnly = true;
});
},
enable: function () {
this.jqchildren.children("input").each(function (index, item) {
item.readOnly = false;
});
}
};
$.fn.durationPicker = function(options){
if (options == undefined) {
var settings = $.extend(true, {}, $.fn.durationPicker.defaults, options);
}
else {
var settings = $.extend(true, {}, {classname: 'form-control', responsive: true, type:'number'}, options);
}
// return this.each(function () {
return new durationPicker(this, settings);
// })
};
function set_values(values, self){
for (var value in Object.keys(values)){
if (self.stages.indexOf(Object.keys(values)[value]) != -1){
self.jqitem.find("#duration-" + (Object.keys(values)[value])).val(values[(Object.keys(values)[value])]);
}
}
}
function get_stages(settings){
var stages = [];
for (var key in Object.keys(settings)){
if (['classname', 'responsive', 'type'].indexOf(Object.keys(settings)[key]) == -1) {
stages.push(Object.keys(settings)[key]);
}
}
return stages
}
function generate_template (settings, stages) {
var html = '<div class="durationpicker-container ' + settings.classname + '">';
var type = settings.type;
for (var item in stages){
html += '<div class="durationpicker-innercontainer"><input min="' + settings[stages[item]]['min'] + '" max="' + settings[stages[item]]['max'] + '" placeholder="0" type="' + type + '" id="duration-' + stages[item] + '" class="durationpicker-duration" ><span class="durationpicker-label">' + settings[stages[item]]['label'] + '</span></div>';
}
html += '</div>';
return html
}
$.fn.durationPicker.defaults = {
hours: {
label: "h",
min: 0,
max: 24
},
minutes: {
label: "m",
min: 0,
max: 59
},
seconds: {
label: "s",
min: 0,
max: 59
},
classname: 'form-control',
type: 'number',
responsive: true
};
$.fn.durationPicker.Constructor = durationPicker;
})(jQuery);