I have one year of JavaScript/jQuery experience but I feel a bit messy in my code. The point is that I make some code that works but I feel that it can be improved like hell. I look for that, but even if it's sometimes cleaner I don't feel that it's good.
This is a double timepicker component. The selects are customized and I push the hours with a for
loop. I then create a template in order to push them in the DOM and on click the select value changes. I feel that there are a lot a mistakes here.
Here is an example. I tried to make a reusable timepicker component in jQuery.
var DoubleTimepicker = function(ctx) {
//Get context
var _ctx = ctx;
//Create array of hours
function arrayHour() {
var demiHours = ["00", "30"];
var times = [];
for (var i = 0; i < 24; i++) {
for (var j = 0; j < 2; j++) {
var time = i + ":" + demiHours[j];
if (i < 10) {
time = "0" + time;
}
times.push(time);
}
}
return times;
};
//get hours and push them into options select template
var getHours = function(hours) {
return hours.map(function(hour, key) {
var templateTime = "<option value='hour-" + key + "'>" + hour + "</option>";
return templateTime;
});
}
//get ehours and push them into ul li template
var fillSelect = function(hours) {
return hours.map(function(hour, key) {
var templateSelect = "<li class='time' data-hour='hour-" + key + "'>" + hour + "</li>";
return templateSelect;
});
}
//toggle hide/show
$('.drop-time', ctx).hide();
var inputTime = $('.input-time', ctx);
inputTime.click(function() {
$('.drop-time', inputTime).toggle();
});
$('.drop-time', ctx).click(function(e) {
e.stopPropagation();
})
var temp = getHours(arrayHour());
var tempSelect = fillSelect(arrayHour());
//transform for the DOM
var divTime = tempSelect.join('');
var optionsTime = temp.join('');
//var for selects
var timeSelect = $('.select_time', ctx);
//var for dropdown div
var divSelect = $('.fromTo', ctx);
timeSelect.append(optionsTime);
divSelect.append(divTime);
var from = $('.input-time .time-from', ctx);
var to = $('.input-time .time-to', ctx);
var fromSelect = $('.select_time-from', ctx);
var toSelect = $('.select_time-to', ctx);
$('.drop-time .from li', ctx).click(function() {
var dataFrom = $(this).data('hour');
$('option', fromSelect).each(function(index, element) {
var v = this.value;
if (v == dataFrom) {
var strFrom = "";
$('option', fromSelect).removeAttr('selected');
$(this).attr('selected', 'selected');
strFrom = $(this).text() + " ";
from.text(strFrom);
}
})
})
$('.drop-time .to li', ctx).click(function() {
var dataTo = $(this).data('hour');
$('option', toSelect).each(function(index, element) {
var v = this.value;
if (v == dataTo) {
var strTo = "";
$('option', toSelect).removeAttr('selected');
$(this).attr('selected', 'selected');
strTo = $(this).text() + " ";
to.text(strTo);
}
})
})
};
var timepick = $('.timepicker_component');
timepick.each(function(index, element) {
new DoubleTimepicker($(timepick).eq(index));
});
.timepicker_component p {
margin: 0;
}
.timepicker_component .input-time {
padding: 10px 15px;
position: relative;
border: 2px solid #DCDCDC;
cursor: pointer;
}
.timepicker_component .time-container {
position: relative;
}
.fromTo{
float: left;
width: 50%;
}
.timepicker_component .time-container .drop-time {
background: #fff;
width: 100%;
overflow-y: auto;
overflow-x: hidden;
height: 150px;
padding-top: 10px;
border-top: 2px dotted #dcdcdc;
margin-top: 6px;
}
.timepicker_component .time-container .drop-time .fromTo li {
list-style: none;
cursor: pointer;
font-size: 14px;
}
.timepicker_component .time-container .drop-time .fromTo li:hover {
background: #DCDCDC;
}
.timepicker_component .time-container .drop-time .fromTo:first-child {
border-right: 2px solid #DCDCDC;
}
.timepicker_component select {
display: none;
}
.timepicker_component .bs-caret {
position: absolute;
top: 0;
right: 2px;
width: 28px;
height: 28px;
margin-top: 4px;
display: block;
padding: 8px 0px 0px 2px;
background: -webkit-linear-gradient(left, #00519E, #009EE0);
background: linear-gradient(to right, #00519E, #009EE0);
border-radius: 5px;
border: none;
}
.timepicker_component .bs-caret span {
transform: rotate(-90deg);
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="timepicker_component col-md-4" style="height:400px">
<div class="col-md-12">
<div class="time-container">
<div class="input-time">
<p><span class="time-from">00:00</span> à <span class="time-to">00:00</span>
</p>
<span class="btn bs-caret"><span class="frh frh-chevron-gauche"></span></span>
<div class="drop-time">
<div class="row">
<div class="fromTo from col-md-6"></div>
<div class="fromTo to col-md-6"></div>
</div>
</div>
<select class="select_time select_time-from"></select>
<select class="select_time select_time-to"></select>
</div>
</div>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>