Show confirmation box in dhtmlx scheduler onbeforeviewchange - scheduler

I want to show the confirmation box before view change. So when user change date for scheduler I want to show confirmation box whether user want to redirect to another date or not.

The simplest solution would be to use window.confirm
It pauses code execution until user choses any option, so you can use simple if-else statement:
scheduler.attachEvent("onBeforeViewChange", function (oldMode, oldDate, mode, date) {
if (oldMode && oldDate) {
if (oldMode !== mode || oldDate.valueOf() !== date.valueOf()) {
if (confirm("are you sure?")) {
return true;
}
return false;
}
}
return true;
});
demo: https://snippet.dhtmlx.com/140d2ff31
If you want a custom confirmation popup, which doesn't block the browser, you'll need to do a small workaround, since scheduler API doesn't support async event handlers:
1) when code enters onBeforeViewChange, you display the dialog and always return false from the handler in order to keep the same date
2) when the user confirms view change - you set some flag to temporary disable step 1 and call scheduler.setCurrentView from the callback. onBeforeViewChange runs again, you check the flag you've set and return true this time, allowing date change.
var callbackViewChange = false;
scheduler.attachEvent("onBeforeViewChange", function (oldMode, oldDate, mode, date) {
if (oldMode && oldDate) {
//
if (!callbackViewChange && (oldMode !== mode || oldDate.valueOf() !== date.valueOf())) {
dhtmlx.confirm({
text: "are you sure?",
callback: function (result) {
if (result) {
// set the flag in order to allow view change
callbackViewChange = true;
scheduler.setCurrentView(date, mode);
callbackViewChange = false;
}
}
});
// cancel view change while we wait for user action
return false;
}
}
return true;
});
demo: https://snippet.dhtmlx.com/a2b8b09b9

Related

UI5: Validate Whole Form's Required and Visible Fields for Null/Blank Values

onPress of submit button, I want to validate all SimpleForms' fields (ComboBox, Input, DatePicker, etc.) that are
required &
visible
to see if they are null or blank (""). If a targeted (required & visible) field is null/blank, set that control's state to "Error" and display an error message. If no targeted field is null/blank, pop up a success dialog box.
This method is automated so in the future, any fields added later will automatically be checked without need of manual additions to controller code.
Controller code:
requiredAndVisible: function(oControl) {
if (typeof oControl.getRequired === "function") { //certain ctrls like toolbars dont have getRequired as a method, so we want to skim those out, else itll throw an error later in the next check
if (oControl.getRequired() === true && oControl.getVisible() === true) {
return oControl;
}
}
},
onSubmit: function() {
var valid = true,
oView = this.getView(),
aFormInitial = oView.byId("formInitial").getContent(), // get all the controls of SimpleForm1
aFormConfig = oView.byId("formConfiguration").getContent(), // get all controls of SimpleForm2
aControls = aFormInitial.concat(aFormConfig), // combine the 2arrays together into 1
aFilteredControls = aControls.filter(this.requiredAndVisible); // check each element if it required & visible using the 1st function. return only the controls that are both req'd & visible
aFilteredControls.forEach(function(oControl) { // in resultant array, check each element if...
if (!oControl.getValue() || oControl.getValue().length < 1) { // its value is null or blank
oControl.setValueState("Error");
valid = false; // set valid to false if it is
} else {
oControl.setValueState("None");
}
});
if (valid === false) {
// **replace this code with w/e error handling code u want**
oView.byId("errorMsgStrip").setVisible(true);
} else if (valid === true) {
// **replace this code with whatever success handling code u want**
var oDialogConfirm = new sap.ui.xmlfragment("dialogID", "dialog.address.here", this);
oDialogConfirm.open();
}
},

Fire rule when Enter Key is pressed. Adobe DTM

I have this code in my Custom code section of an event based rule in DTM. I am trying to fire the rule upon the Enter key press. The input is not within a form element. How to I get the Keycode scoped into my custom page code? Any help would be appreciated!
jQuery(this).keypress(function (e) {
if (e.keyCode == 13) {
var frmData = 'search:new:submit';
var inpData = jQuery(this).siblings('input').val().trim().toLowerCase();
_satellite.setVar('frmData', frmData);
_satellite.setVar('inpData', inpData);
return true;
}
});
I got it to fire by switching the event type to Keypress and using this simple code. -cheers
if (event.keyCode == 13){
return true;
}

Event.stop within .each (prototype)

I am struggling with function that should check form fields before submitting.
I have some select (dropdown fields) and a text field. None of them should be empty for submit.
The script http://jsfiddle.net/6KY5J/2/ to reproduce.
I check dropdown fields within .each and additional text field. Here is the function:
function checkFields(e) {
$$('.dropdown').each(function (element) {
if (element.selectedIndex === 0) {
alert('Fill all dropdown fields!');
Event.stop(e);
throw $break;
return;
}
});
if ($('sometext').value == '') {
alert('Fill the input!');
Event.stop(e);
return;
}
alert('OK!');
}
But I am not able to prevent further execution of the script if one of dropdown is empty. Event.stop(e) seems to to work for the input field only in the second part.
Desired behaviour:
Check dropdowns, if one is empty, stop execution, do not make any
further checks.
Check text input field only if dropdowns are not empty.
Submit only if everything if filled.
The issue is in step 1. My script does not stop here, alerts, but does not stop. Any idea? Thank you!
function checkFields(e) {
var dropdownsokay = true;
$$('.dropdown').each(function (element) {
if (dropdownsokay && element.selectedIndex === 0) {
alert('Fill all dropdown fields!');
Event.stop(e);
dropdownsokay = false;
}
});
if(dropdownsokay) { //only check textfield if all the dropdowns are okay
if ($('sometext').value == '') {
alert('Fill the input!');
Event.stop(e);
return;
}
alert('OK!');
}
}

How to fire place_changed event for Google places auto-complete on Enter key

The click seems to fire the event and set the cookies but pressing enter to submit doesn't set the cookies and instead the page redirects without the cookies.
function locationAuto() {
$('.search-location').focus(function () {
autocomplete = new google.maps.places.Autocomplete(this);
searchbox = this;
google.maps.event.addListener(autocomplete, 'place_changed', function () {
var thisplace = autocomplete.getPlace();
if (thisplace.geometry.location != null) {
$.cookie.raw = true;
$.cookie('location', searchbox.value, { expires: 1 });
$.cookie('geo', thisplace.geometry.location, { expires: 1 });
}
});
});
The .search-location is a class on multiple textboxes.
There is a submit button that takes the values from the cookies and redirects (server side)
Adapted from Jonathan Caulfield's answer:
$('.search-location').keypress(function(e) {
if (e.which == 13) {
google.maps.event.trigger(autocomplete, 'place_changed');
return false;
}
});
I've encountered this problem as well, and came up with a good solution. In my website I wanted to save the autocomplete.getPlace().formatted_address in a hidden input prior to submission. This worked as expected when clicking the form's submit button, but not when pressing the Enter key on the selection in the autocomplete's dropdown menu. My solution was as follows:
$(document).ready(function() {
// Empty the value on page load
$("#formattedAddress").val("");
// variable to indicate whether or not enter has been pressed on the input
var enterPressedInForm = false;
var input = document.getElementById("inputName");
var options = {
componentRestrictions: {country: 'uk'}
};
autocomplete = new google.maps.places.Autocomplete(input, options);
$("#formName").submit(function(e) {
// Only submit the form if information has been stored in our hidden input
return $("#formattedAddress").val().length > 0;
});
$("#inputName").bind("keypress", function(e) {
if(e.keyCode == 13) {
// Note that simply triggering the 'place_changed' event in here would not suffice, as this would just create an object with the name as typed in the input field, and no other information, as that has still not been retrieved at this point.
// We change this variable to indicate that enter has been pressed in our input field
enterPressedInForm = true;
}
});
// This event seems to fire twice when pressing enter on a search result. The first time getPlace() is undefined, and the next time it has the data. This is why the following logic has been added.
google.maps.event.addListener(autocomplete, 'place_changed', function () {
// If getPlace() is not undefined (so if it exists), store the formatted_address (or whatever data is relevant to you) in the hidden input.
if(autocomplete.getPlace() !== undefined) {
$("#formattedAddress").val(autocomplete.getPlace().formatted_address);
}
// If enter has been pressed, submit the form.
if(enterPressedInForm) {
$("#formName").submit();
}
});
});
This solution seems to work well.
Both of the above responses are good answers for the general question of firing a question when the user presses "enter." However - I ran into a more specific problem when using Google Places Autocomplete, which might have been part of the OP's problem. For the place_changed event to do anything useful, the user needs to have selected one of the autocomplete options. If you just trigger 'place_changed', the if () block is skipped and the cookie isn't set.
There's a very good answer to the second part of the question here:
https://stackoverflow.com/a/11703018/1314762
NOTE: amirnissim's answer, not the chosen answer, is the one to use for reasons you'll run into if you have more than one autocomplete input on the same page.
Maybe not the most user friendly solution but you could use JQuery to disable the enter key press.
Something like this...
$('.search-location').keypress(function(e) {
if (e.which == 13) {
return false;
}
});

jquery mobile, browser back button navigation

I have a multipage form with #p1,#p2,#p3. Once I submit the form, and when I try to click back browser button, it should go to #p1 with empty form fields. it is possible wiith Jquery Mobile?
I would override the backbutton and check for which page is the active page then based on the page do whatever house cleaning you need...
I submitted an example to another question really similar to this:
BackButton Handler
Where I have Options, Popup and HomePage you might just need P3 and when the activePage is equal to P3 clear your form and show P1.
function pageinit() {
document.addEventListener("deviceready", deviceInfo, true);
}
function deviceInfo() {
document.addEventListener("backbutton", onBackButton, true);
}
function onBackButton(e) {
try{
var activePage = $.mobile.activePage.attr('id');
if(activePage == 'P3'){
clearForm(); // <-- Calls your function to clear the form...
window.location.href='index.html#P1';
} else if(activePage == 'P1'){
function checkButtonSelection(iValue){
if (iValue == 2){
navigator.app.exitApp();
}
}
e.preventDefault();
navigator.notification.confirm(
"Are you sure you want to EXIT the program?",
checkButtonSelection,
'EXIT APP:',
'Cancel,OK');
} else {
navigator.app.backHistory();
}
} catch(e){ console.log('Exception: '+e,3); }
}