Change disabledHours during time of current day when other select option is changed - eonasdan-datetimepicker

The task is a booking form for a restaurant. The disabled time is the same in every Restaurant except in one of them. All Restaurants can be chosen via select options. The select field already has a js function called show for different functions.
I have created js to set the time between 17 and and 23 to be disabled when the Restaurant has been Picked beforehand.
However I cannot get the picker to reload on change. I've tried it with function show() and $('#Restaurant').change(function(). I realize I am not telling the picker to change the values, but I'm not sure how.
<select name="Restaurant" id="Restaurant" onchange="show()" required data-msg-required="Please choose the restaurant">
<?php if (empty($_GET['Restaurant'])){echo '<option disabled selected
value="">Choose restaurant</option>';}?>
<?php booking(); ?>
</select>
<script>
$(function () {
$('#DatumZeit').datetimepicker({
locale: 'en',
minDate: moment().add(2, 'hours'),
disabledHours: disabledtime,
format: 'DD.MM.YYYY HH:mm',
showClose: true,
icons: {
close: 'OK'
},
widgetPositioning: {
horizontal: 'auto',
vertical: 'bottom'
},
});
});
function show(){
if(document.getElementById('Restaurant').value == "Düsseldorf") {
var currenttime = moment();
var timestart = moment().startOf('day').hour(10).add(7, 'hours');
var timeend = moment().startOf('day').hour(10).add(13, 'hours');
console.log(timestart);
console.log(timeend);
if (currenttime.isBetween(timestart, timeend)) {
console.log('is between');
var disabledtime= [10,9,8,7,6,5,4,3,2,1,00,24,23,17,18,19,20,21,22];
} else {
console.log('is not between');
var disabledtime= [10,9,8,7,6,5,4,3,2,1,00,24,23];
}
};
}
</script>

Maybe it can help someone else:
Basically I used $('#DatumZeit').data("DateTimePicker").disabledHours.
function show(){
if(document.getElementById('Restaurant').value == "Düsseldorf") {
var currenttime = moment();
var timestart = moment().startOf('day').hour(10).add(7, 'hours');
var timeend = moment().startOf('day').hour(10).add(13, 'hours');
if (currenttime.isBetween(timestart, timeend)) {
$('#DatumZeit').data("DateTimePicker").disabledHours([
10,9,8,7,6,5,4,3,2,1,00,24,23,17,18,19,20,21,22
]);
console.log('is between');
} else {
$('#DatumZeit').data("DateTimePicker").disabledHours([
10,9,8,7,6,5,4,3,2,1,00,24,23
]);
console.log('is not between');
}
};
if(document.getElementById('Restaurant').value != "Düsseldorf") {
$('#DatumZeit').data("DateTimePicker").disabledHours([
10,9,8,7,6,5,4,3,2,1,00,24,23
]);
}
}

I actually have a better version for my own needs that might work for other people as well.
function show(){
if(document.getElementById('Restaurant').value == "Düsseldorf") {
var myDate = new Date();
var currenttime = moment(myDate);
var timestart = moment().startOf('day').hour(17);
var timeend = moment().startOf('day').hour(23);
if (currenttime.isBetween(timestart, timeend)) {
$('#DatumZeit').data("DateTimePicker").date(moment().add(1, 'days')).disabledHours(
[0,1,2,3,4,5,6,7,8,9,10,23,24]
)
}
else {
$('#DatumZeit').data("DateTimePicker").date(moment(myDate)).disabledHours(
[0,1,2,3,4,5,6,7,8,9,10,23,24]
)
}
} else {
$('#DatumZeit').data("DateTimePicker").date(moment().add(2, 'hours')).enabledHours(
[11, 12 ,13 ,14 ,15, 17,16, 18, 19, 20, 21, 22]
)
}
}

Related

Problem inserting checkbox in select using DataTables

I'm looking for a way to insert a checkbox into a select and fetch each column individually, all using DataTables. I found an excellent example in https://jsfiddle.net/Lxytynm3/2/ but for some reason, when selecting all records, filtering does not display the data as expected. Would anyone have a solution to work properly?
Thanks in advance.
The link application code is as follows:
$(document).ready(function() {
function cbDropdown(column) {
return $('<ul>', {
'class': 'cb-dropdown'
}).appendTo($('<div>', {
'class': 'cb-dropdown-wrap'
}).appendTo(column));
}
$('#example').DataTable({
initComplete: function() {
this.api().columns().every(function() {
var column = this;
var ddmenu = cbDropdown($(column.header()))
// -------------------------------------------------------
.on('change', ':checkbox', function() {
var active;
var vals = $(':checked', ddmenu).map(function(index, element) {
active = true;
return $.fn.dataTable.util.escapeRegex($(element).val());
}).toArray().join('|');
column
.search(vals.length > 0 ? '^(' + vals + ')$' : '', true, false)
.draw();
// -------------------------------------------------------
// Highlight the current item if selected.
if (this.checked) {
$(this).closest('li').addClass('active');
// If 'Select all / none' clicked ON
if ($(this).val() === "1") {
$(ddmenu).find('input[type="checkbox"]').prop('checked', this.checked)
//$(".cb-dropdown li").prop('checked', true);
//$('.cb-dropdown').closest('li').find('input[type="checkbox"]').prop('checked', true);
// $('this input[type="checkbox"]').prop('checked', true); works!
// $( 'input[type="checkbox"]' ).prop('checked', this.checked);
// $(this).find('input[type="checkbox"]').prop('checked', this.checked)
//$('div.cb-dropdown-wrap.active').children().find('input[type="checkbox"]').prop('checked', this.checked)
}
} else // 'Select all / none' clicked OFF
{
$(this).closest('li').removeClass('active');
// test if select none
if ($(this).val() === "1") {
// code to untick all
$(ddmenu).find('input[type="checkbox"]').prop('checked', false)
}
}
// Highlight the current filter if selected.
var active2 = ddmenu.parent().is('.active');
if (active && !active2) {
ddmenu.parent().addClass('active');
// Change Container title to "Filter on" and green
//$(this).parent().find('.cb-dropdown li:nth-child(n+1)').css('color','red');
$('active2 li label:contains("Filter OFF")').text('Yeees');
} else if (!active && active2) {
ddmenu.parent().removeClass('active');
}
});
// -------------------------------------------------------
var mytopcount = '0'; // Counter that ensures only 1 entry per container
// loop to assign all options in container filter
column.data().unique().sort().each(function(d, j) {
// Label
var $label = $('<label>'),
$text = $('<span>', {
text: d
}),
// Checkbox
$cb = $('<input>', {
type: 'checkbox',
value: d
});
$text.appendTo($label);
$cb.appendTo($label);
ddmenu.append($('<li>').append($label));
// -----------------
// Add 'Select all / none' to each dropdown container
if (mytopcount == '0') // Counter that ensures only 1 entry per container
{
$label = $('<label>'), $text = $('<span>', {
text: "Select all / none"
}),
$cb = $('<input>', {
type: 'checkbox',
value: 1
});
$text.prependTo($label).css('margin-bottom', '6px');
$cb.prependTo($label);
ddmenu.prepend($('<li>').prepend($label).css({
'border-bottom': '1px solid grey',
'margin-bottom': '6px'
}));
mytopcount = '1' // This stops this section running again in cotainer
}
});
});
}
});
});
It seems as though the issue was with the select all checkbox. One solution would be to check for "1" within the vals initialisation, this seems to work:
var vals = $(':checked', ddmenu).map(function(index, element) {
if($(element).val() !== "1"){
return $.fn.dataTable.util.escapeRegex($(element).val());
}
}).toArray().join('|');
That should see you with some results upon the top checkbox being checked. Hope that helps.

Google Forms Script to open form on specific times on weekdays & weekends

I am using the following script to have the form available between 6am & 8am every day, however, I'd like it to be available 6am-8am on weekdays & 6am-11am on weekends... Suggestions?
function initTrigger(){
ScriptApp.newTrigger('oc').timeBased().atHour(0).everyDays(1).create();
}
function oc() {
ScriptApp.getProjectTriggers().forEach(function(e){
if(e.getHandlerFunction() == "openForm" || e.getHandlerFunction() == "closeForm") {
ScriptApp.deleteTrigger(e)
}
});
var time = new Date()
time.setHours(6);
time.setMinutes(00);
ScriptApp.newTrigger("openForm").timeBased().at(time).create();
time.setHours(8);
time.setMinutes(0);
ScriptApp.newTrigger("closeForm").timeBased().at(time).create();
}
function openForm() {
var form = FormApp.getActiveForm();
form.setAcceptingResponses(true);
}
function closeForm() {
var form = FormApp.getActiveForm();
form.setAcceptingResponses(false);
// deleteTriggers_();
}
I believe I got it to work with the following:
function initTrigger(){
ScriptApp.newTrigger('oc').timeBased().atHour(0).everyDays(1).create();
}
function oc() {
ScriptApp.getProjectTriggers().forEach(function(e){
if(e.getHandlerFunction() == "openForm" || e.getHandlerFunction() == "closeForm") {
ScriptApp.deleteTrigger(e)
}
});
var day = new Date();
if (day.getDay()>5 || day.getDay()==0) {
var time = new Date()
time.setHours(6);
time.setMinutes(00);
ScriptApp.newTrigger("openForm").timeBased().at(time).create();
time.setHours(11);
time.setMinutes(00);
ScriptApp.newTrigger("closeForm").timeBased().at(time).create();
}
else {
var time = new Date()
time.setHours(6);
time.setMinutes(00);
ScriptApp.newTrigger("openForm").timeBased().at(time).create();
time.setHours(8);
time.setMinutes(00);
ScriptApp.newTrigger("closeForm").timeBased().at(time).create();
}
}
function openForm() {
var form = FormApp.getActiveForm();
form.setAcceptingResponses(true);
}
function closeForm() {
var form = FormApp.getActiveForm();
form.setAcceptingResponses(false);
// deleteTriggers_();
}

Salesforce lightning input's element.set not working anymore after summer 18 release

I am using below snippet to check for valid phone number format and then trying to set the formatted value to the current input element. But after summer 18 release I'm unable to set input with new formatted value.
TestApp
<aura:application extends="force:slds">
<lightning:input type="text" label="Num" aura:id="ele" onchange="
{!c.changeNum}" />
</aura:application>
Controller:
({
changeNum : function(component, event, helper) {
helper.changeNum(component, event);
}
})
Helper:
({
changeNum : function(component, event) {
var element = event.getSource();
var phonenumber = element.get("v.value");
if(phonenumber){
var updatedValue = phonenumber.replace(/-/g, "");
if(/^\d{10}$/.test(updatedValue)){
phonenumber = updatedValue.match(new RegExp('\\d{4}$|\\d{3}', 'g')).join("-");
}
else{
var x = phonenumber.replace(/[^0-9._-]/g, "").replace(/ +/, " ");
phonenumber = x;
if(!/^[0-9-]+$/.test(phonenumber.slice(-1))){
phonenumber = phonenumber.slice(0, -1);
}
}
}
console.log(phonenumber);
element.set('v.value', phonenumber);
}
})
element.set is not able to update the formatted value. The lightning input element is still able to accept alphabets.
We can solve the issue by using Promise
({
handleInputChange : function(component, event) {
try {
var element = event.getSource();
var inputValue = element.get("v.value");
var formattedValue;
var chkPattern = new Promise(
function (resolve, reject) {
if (inputValue) {
formattedValue = inputValue.replace(/[^0-9-]/g, "").replace(/ +/, " ");
resolve(formattedValue); // fulfilled
} else {
var reason = new Error('kitten is not happy');
reject(reason); // reject
}
}
);
chkPattern.then(function (fulfilled) {
element.set('v.value', fulfilled);
}).catch(function (error) {
console.log(error.message);
});
} catch(e) {
this.consoleLog(e.stack, true)
}
}
})

Scheduling multiple functions at different times of the day

I'm trying to set up my code to show this youtube playlist from 12am to 12pm then show a different playlist from 12pm to 12am repeating the process. Ive looked at SetIntervals and Date but still pretty new to using java so not sure how to implement them. If anyone could give me an example to go off of I would appreciate it. Here is the current code I have
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
var player;
function onYouTubeIframeAPIReady() {
player = new YT.Player('player', {
height: '390',
width: '640',
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
}
var oldNumber = 0;
var NewNumber = 0;
function newRandomNumber(min, max) {
oldNumber = NewNumber;
NewNumber = Math.floor(Math.random() * max) + min;
if (NewNumber == oldNumber) {
newRandomNumber(min, max);
} else {
return NewNumber;
}
}
function onPlayerReady(event) {
next();
}
function next() {
player.loadPlaylist({
'listType': 'playlist',
'list': 'PLBLjQxqJYmcdjxmoFURusJpwbKs0XTSOy',
'autoplay': 1,
'controls': 0,
'index': newRandomNumber(0, 200),
});
player.setShuffle({
'shufflePlaylist': true
});
}
function onPlayerStateChange(event) {
if (event.data == YT.PlayerState.ENDED) {
next();
}
}
</script>
here is the other playlist I've been trying to get it to change to
function next() {
player.loadPlaylist({
'listType': 'playlist',
'list': 'PLBLjQxqJYmcdjxmoFURusJpwbKs0XTSOy',
'autoplay': 1,
'controls': 0,
'index': newRandomNumber(0, 200),
});
player.setShuffle({
'shufflePlaylist': true
});
}

Trouble while disabling 7 days before active date

I'm trying to make the datepicker disable 1 week from the current date.
The code is for a datepicker for room reservation using an onRender function.
Please help me :(
$(function() {
var nowTemp = new Date();
var now = new Date(nowTemp.getFullYear(), nowTemp.getMonth(), nowTemp.getDate(), 0, 0, 0, 0);
var checkin = $('#ad').datepicker({
onRender: function(date) {
return date.valueOf() < now.valueOf() ? 'disabled' : '';
}
}).on('changeDate', function(ev) {
if (ev.date.valueOf() > checkout.date.valueOf()) {
var newDate = new Date(ev.date)
newDate.setDate(newDate.getDate() + 1);
checkout.setValue(newDate);
}
checkin.hide();
$('#dpd2')[0].focus();
}).data('datepicker');
var checkout = $('#dd').datepicker({
onRender: function(date) {
return date.valueOf() <= checkin.date.valueOf() ? 'disabled' : '';
}
}).on('changeDate', function(ev) {
checkout.hide();
}).data('datepicker');
});