I am trying to implement the Sweet Alert javascript library in my application.
Here is the the relevant code:
<xp:link escape="true" text="" id="link2" style="color:#4A4A4A">
<i class="fa fa-trash-o fa-lg">
</i>
<xp:eventHandler event="onclick"
submit="true" refreshMode="partial" refreshId="assetList">
<xp:this.action><![CDATA[#{javascript:
var db:NotesDatabase = session.getDatabase(sessionScope.serverPath,sessionScope.dbName);
var id = viewRow.getUniversalID();
var doc:NotesDocument = db.getDocumentByUNID(id);
doc.remove(true);}]]></xp:this.action>
<xp:this.script><![CDATA[swal({
title: "Are you sure?",
text: "This asset will be permanently deleted",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#2196f3",
confirmButtonText: "Ok",
cancelButtonColor: "#607D8B",
cancelButtonText: "Cancel",
closeOnConfirm: true,
closeOnCancel: true,
confirmButtonClass: 'confirm-class',
cancelButtonClass: 'cancel-class'
},
function(isConfirm){
if (isConfirm) {
return true;
} else {
return false;
}
});
//if(window.confirm("Are you sure you want to delete the asset?") != true)
return false;]]></xp:this.script>
</xp:eventHandler>
</xp:link>
Clicking on the link deletes the row regardless of the choice I make (Cancel/Ok). I can see the delete happening even before I make my choice. I have a feeling it has something to do with the order of execution for the callback.
If I use the code that's commented out at the end (window.confirm), it works perfectly.
I would appreciate your guidance on this.
Thanks,
Dan
The issue is that 3rd party prompts don't block and wait for a response before continuing like the native browser prompts do.
This SO post shows an example of putting the code in a callback to try to work with Sweet Alert (but it looks like you're already doing something similar):
sweetalert blocking like normal prompt
Triggering the click event of a button that will run the server-side code will get the job done. If you want it to run asynchronously (but not need to update the page client-side), you could run a json rpc method in the callback or make a call to a custom REST service to run the server-side code instead of triggering the hidden button.
I resolved this by creating a hidden button. I removed the SSJS code from my link and from the CSJS of the link called the click event of the hidden button to execute the SSJS.
Here's the updated code:
<xp:link escape="true" text="" id="link2" style="color:#4A4A4A">
<i class="fa fa-trash-o fa-lg">
</i>
<xp:eventHandler event="onclick"
submit="true" refreshMode="partial" refreshId="assetList">
<xp:this.script><![CDATA[swal({
title: "Are you sure?",
text: "This asset will be permanently deleted",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#2196f3",
confirmButtonText: "Ok",
cancelButtonColor: "#607D8B",
cancelButtonText: "Cancel",
closeOnConfirm: true,
closeOnCancel: true,
confirmButtonClass: 'confirm-class',
cancelButtonClass: 'cancel-class'
},
function(response){
if (response == true) {
var deleteAssetBtn = '#{javascript:getClientId("deleteAsset")}';
document.getElementById(deleteAssetBtn).click();
return true;
} else {
return false;
}
});
]]></xp:this.script>
<xp:this.action><![CDATA[#{javascript:viewScope.docID = viewRow.getUniversalID();}]]>
</xp:this.action>
</xp:eventHandler>
</xp:link>
And here's the code for the hidden button that actually executes the deletion of the document:
<!-- Hidden button -->
<xp:button value="Delete Asset" id="deleteAsset" style="display:none">
<xp:eventHandler event="onclick" submit="true" refreshMode="complete">
<xp:this.action><![CDATA[#{javascript:try {
var db:NotesDatabase = session.getDatabase(sessionScope.serverPath,sessionScope.dbName);
var doc:NotesDocument = db.getDocumentByUNID(viewScope.docID);
doc.remove(true);
viewScope.docID = "";
} catch(e) {
requestScope.errstatus = e.toString();
}}]]></xp:this.action>
</xp:eventHandler>
</xp:button>
If anyone has a better way to achieve this outcome, I'm open to suggestions :)
You may add extra event instead of adding hidden button. I.e in the following code you have radio with 2 events:
- onchange - this is standard event where your CSJS is run
- onchangepost - this is extra event that contains your code that will be evaluated after onchange event (not avialable by Designer UI, you have to add it manually).
I assume that your onchange code is asynchronous. If you have some onEnd/onComplete section you my add some code that will be evaluated after it finishes its job (probably you click your button there). That's the place where you can run your onchangepost code using i.e. fire event snippet (https://openntf.org/XSnippets.nsf/snippet.xsp?id=manually-fire-attached-event)
<xp:radioGroup id="myRadio" required="false">
<xp:selectItem itemLabel="Option a"
itemValue="A">
</xp:selectItem>
<xp:selectItem itemLabel="Option b" itemValue="B">
</xp:selectItem>
<xp:eventHandler event="onchange" submit="false">
<xp:this.script>
<![CDATA[
doYourClientJS(
{onComplete: fireEvent(dojo.byId('#{id:myRadio}'), 'changepost')
});
]]></xp:this.script>
</xp:eventHandler>
<xp:eventHandler event="onchangepost"
submit="true" refreshMode="partial" refreshId="otherUserInnerRefreshSection"
execMode="partial" execId="otherUserRefreshSection"
disableValidators="true">
<xp:this.action><![CDATA[#{javascript:doYourSSJS;}]]></xp:this.action>
</xp:eventHandler>
</xp:radioGroup>
Related
I have a requirement for my app and I need to change the event handler of a common button depending on the status of the workflow.
Basically I need to change the function called when you press the button and vice-versa and was looking to achieve this by using the event handler functions detachPress and attachPress.
https://ui5.sap.com/#/api/sap.m.Button/methods/detachPress
https://ui5.sap.com/#/api/sap.m.Button/methods/attachPress
My Button (XML View):
<Button text="Edit" width="50%" id="_editButtonEmail" press="editPIN"/>
On my controller I want to change the function editPIN by cancelEditPIN.
Some things I've tried:
editPIN: function(oControlEvent) {
//change button
var editButton = this.getView().byId("_editButtonEmail");
//detach this function on press
editButton.detachPress(editButton.mEventRegistry.press[0].fFunction);
editButton.attachPress(this.cancelEditPIN());
}
cancelEditPIN: function() {
//do something else
}
Also
editPIN: function(oControlEvent) {
//change button
var src = oControlEvent.getSource();
src.detachPress(this.editPIN());
src.attachPress(this.cancelEditPIN());
}
None of these seem to work and if I check my console the function editPIN is still attached to my mEventRegistry press event.
There are few things worse than checking your GUI texts to determine what action should be done.
A different approach uses two buttons. Only one is visible at a time
<Button
text="{i18n>editPIN}"
visible="{= ${myModel>/State} === 'show' }"
press="editPIN" />
<Button
text="{i18n>editCancelPIN}"
visible="{= ${myModel>/State} === 'edit' }"
press="cancelEditPIN" />
In this case {myModel>/State} is a local JSON model where the current state of your workflow is stored.
If you really want to use your attach/detach approach: It probably didn't work because you were calling the methods while passing them as a parameter to attach/detach. So for example try src.detachPress(this.editPIN); instead of src.detachPress(this.editPIN());
Following the idea from #Jorg, I created another function checkPIN with an if statement that compares the text in the button and then fires the appropriate function depending on it.
I do have to phrase that I am using my i18n file to provide texts to my view, this way my textID will not change on whatever language the user is using.
My Button:
<Button text="Edit" width="50%" id="_editButtonEmail" press="checkPIN"/>
My Controller:
checkPIN: function(oControlEvent) {
var src = this.getView().byId("_editButtonEmail").getText();
var oBundle = this.getView().getModel("i18n").getResourceBundle();
//call cancelEditPIN
var editCancelPinText = oBundle.getText("editCancelPIN");
//call editPIN
var editPinText = oBundle.getText("editPIN");
//change button
if (src === editPinText) {
this.editPIN(oBundle);
} else if (src === editCancelPinText) {
this.cancelEditPIN(oBundle);
}
},
editPIN: function(oBundle) {
//do stuff here
//change button text
var editButton = this.getView().byId("_editButtonEmail");
editButton.setText(oBundle.getText("editCancelPIN"));
},
cancelEditPIN: function(oBundle) {
//do different stuff here
//change button text
var editButton = this.getView().byId("_editButtonEmail");
editButton.setText(oBundle.getText("editPIN"));
}
Not really the answer I was looking for because I would like to use detachPress and attachPress so if you know what I should have done in order to implement those please let me know.
When using the WorkList (and even Master-detail) templates you have the following event in the onInit function:
oTable.attachEventOnce("updateFinished", function() {
// Restore original busy indicator delay for worklist's table
oViewModel.setProperty("/tableBusyDelay", iOriginalBusyDelay);
});
In the view.xml you also have the eventHandler for updateFinished which you can set, so that you are able to do stuff when the data is received in your list.
In the PlanningCalendar you don't have such an eventhandler, how do we handle these kind of things for such a component?
The logic I'm trying to implement is the following:
<PlanningCalendar
id="PC1"
rows="{
path: '/DeveloperSet'
}"
viewKey="Day"
busyIndicatorDelay="{planningView>/calendarBusyDelay}"
noDataText="{planningView>/calendarNoDataText}"
appointmentSelect="onAppointmentSelect"
rowSelectionChange="onDeveloperRowChange"
startDateChange="onStartDateChange">
<toolbarContent>
<Title
text="Title"
titleStyle="H4" />
<ToolbarSpacer />
<Button
id="bLegend"
icon="sap-icon://legend"
type="Transparant"
press="onShowlegend" />
</toolbarContent>
<rows>
<PlanningCalendarRow
icon="{Pic}"
title="{Name}"
text="{Role}" />
</rows>
</PlanningCalendar>
I want to load and add the "appointments" only for the visible part (filter on start and endDate) of the calendar, so I want to perform the oDataModel.read-calls myself. But the rows (the DeveloperSet) should always remain the same. So I should be able to "wait" until the calendar has the data/rows filled in the calendar and then do my manual calls to retrieve the appointments.
So I need to be able to do something when the data is retrieved, but there is no updateFinished event for a calendar?
Does anybody have an idea on how to solve this?
the event "updateFinished" when used in the Table or List is triggered from method updateList, this method handles the update of aggregation "list"
PlanningCalendar does not have an updateRows method, therefore no event "updateFinished"
You could listen to the dataReceived event on the Row binding, if you have one
OnInit: function(){
...
this.oPlanningCalendar = this.byId("PC1")
var oBinding = oPlanningCalendar.getBinding("rows");
oBinding.attachDataReceived(this.fnDataReceived, this);
else you can extend the control and add your own updateRows method and fire "updateFinished", the hack test below shows it would work
OnInit: function(){
...
this.oPlanningCalendar = this.byId("PC1");
this.oPlanningCalendar.updateRows = function(sReason) {
this.oPlanningCalendar.updateAggregation("rows");
var oBinding = this.oPlanningCalendar.getBinding("rows");
if (oBinding) {
jQuery.sap.log.info("max rows = " + oBinding.getLength() || 0);
}
}.bind(this);
I am building a small landing page with a simple demo e-mail signup form. I want to have the form field open up when focused, and then shrink back down on blur.
However the problem I'm facing is when you click the submit button this instigates the blur function, hiding the button and shrinking the form. I need to find a way to stop the .blur() method only when the user is clicking to focus on the submit button. Is there any good workaround for this?
Would appreciate any help I can get!
I know this question is old but the simplest way to do it would be to check event.relatedTarget. The first part of the if statement is to prevent throwing an error if relatedTarget is null (the IF will short circuit because null is equivalent to false and the browser knows that it doesn't have to check the second condition if the first condition is false in an && statement).
So:
if(event.relatedTarget && event.relatedTarget.type!="submit"){
//do your animation
}
It isn't the prettiest solution, but it does work. Try this:
$("#submitbtn").mousedown(function() {
mousedownHappened = true;
});
$("#email").blur(function() {
if (mousedownHappened) // cancel the blur event
{
mousedownHappened = false;
}
else // blur event is okay
{
$("#email").animate({
opacity: 0.75,
width: '-=240px'
}, 500, function() {
});
// hide submit button
$("#submitbtn").fadeOut(400);
}
});
DEMO HERE
Try this inside .blur handler:
if ($(':focus').is('#submitbtn')) { return false; }
why not rely on submit event instead of click? http://jsbin.com/ehujup/5/edit
just couple changes into the html and js
wrap inputs into the form and add required for email as it obviously suppose to be
<form id="form">
<div id="signup">
<input type="email" name="email" id="email" placeholder="me#email.com" tabindex="1" required="required">
<input type="submit" name="submit" id="submitbtn" value="Signup" class="submit-btn" tabindex="2">
</div>
</form>
in js, remove handler which listen #submitbtn
$("#submitbtn").on("click", function(e){
e.stopImmediatePropagation();
$("#signup").fadeOut(220);
});
and use instead submit form listerer
$("#form").on("submit", function(e){
$("#signup").fadeOut(220);
return false;
});
you may use $.ajax() to make it even better.
Doing this you gain point in terms of validation and the native browser's HTML5 validator will make check email format where it is supported.
To change the status of a checkbox with javascript doesn't correspond to the spirit of MVVM. But I'm creating a general javascript library for better looking standard controls like checkbox, radio button or selectbox.
Based on the following viewmodel:
function MyViewModel() {
var self = this;
self.ok = ko.observable();
};
var vm = new MyViewModel();
ko.applyBindings(vm);
But I get a problem in conjunction with knockout when I change the checked status of a checkbox programmatically:
document.getElementById('chk').checked = true
The change will not appear in the property of the viewmodel. But when I click the checkbox all works fine.
Look at http://jsfiddle.net/KWdZB/1/
Is there any workaround?
Your problem is that ko subscribes on the click event inside the checked binding:
ko.utils.registerEventHandler(element, "click", updateHandler);
But changing the checked attribute won't trigger the click event so ko won't be notified.
If you manually trigger the click event after the attribute change it can work...
I don't know how to do it with pure javascript but with jQuery you can write:
$('#chk').attr('checked', true).triggerHandler('click')
You can test it in this JSFiddle.
This is normal because the checked binding handlers doesn't subscribe to the checked change event but subscribe to the click event handler (You can see on source file at the checked binding handlers code).
If you need to change value with click, you must to do with the ok observable value.
There is the HTML
<div>
<input type="checkbox" id="chk" data-bind="checked: ok"/><br>
<input type="button" id="btnCheck" value="Check" data-bind="click: Check"/>
<input type="button" id="btnUnCheck" value="Uncheck" data-bind="click:Uncheck"/>
</div>
<div>
Value: <span data-bind="text: ok"></span>
</div>
And the javascript :
function MyViewModel() {
var self = this;
self.ok = ko.observable();
self.Check = function(){
self.ok(true);
}
self.Uncheck = function(){
self.ok(false);
}
};
vm = new MyViewModel();
ko.applyBindings(vm);
You can see it in this fiddle.
I have a strange problem. This code works fine in chrome and firefox, but in IE 8 the live event will not fire the first time I uncheck a box. If I check it and then uncheck again it works every time after that.
My serverside code in the view
<%: Html.CheckBox("select-invoice-" + invoice.InvoiceNumber,
true,
new { title = "choose to not pay anything on this invoice by unchecking this box" }) %>
renders to this
<input checked="checked" id="select-invoice-TST-1001"
name="select-invoice-TST-1001"
title="choose to not pay anything on this invoice by unchecking this box"
type="checkbox" value="true" />
Here is my javascript live event wireup, simplified
$(function () {
$("[id^='select-invoice-']").live('change', function () {
var invoiceId = $(this).attr('id').substr('select-invoice-'.length);
ComputeTotalPayment();
if ($(this).is(':checked')) {
//save invoice data
} else {
//remove invoice data
}
});
});
There are no errors in the javascript on any browser. If I switch IE to compatibility mode the live event never works. Other live events for clicks on links work just fine.
The change event doesn't fire correctly in IE until the checkbox loses focus.
Bug: http://webbugtrack.blogspot.com/2007/11/bug-193-onchange-does-not-fire-properly.html
You'll need to map to the "click" event instead.
I have found that change causes some problems in IE. Try using the click event instead. This appears to fix the problem.
I had a similar problem and solved it by calling .change() once on page load.
$(function () {
$("[id^='select-invoice-']").live('change', function () {
var invoiceId = $(this).attr('id').substr('select-invoice-'.length);
ComputeTotalPayment();
if ($(this).is(':checked')) {
//save invoice data
} else {
//remove invoice data
}
}).change();
});