WinJS - Preventing two quick button presses - windows-8

We have a situation in an application in WinJS where our serialised data is read from a file on disk, and depending on the page, different sections of the file are accessed.
The problem we have is that when a user double taps a control, button, listview etc, the system will try to read the file twice and sporadically blows up.
Is there a recommended route in WinJS to prevent or handle double presses on controls? other than something like manually disabling and re-enabling all buttons when pressed?
We've looked at some options, including overriding addEventListener, but none are perfect, any suggestions in this area would be greatly appreciated.
Additional: Whilst in this example the problem is reading a file, we have other applications where performing quick double presses on lists will try to navigate to a page twice (and add it to the nav.history twice), so it seems like there are a number of places and symptoms where this kind of thing could occur.

The best way is to disable the buttons while processing and reenable them when done. This will provide appropriate feedback to the user if things take longer than expected.
You could disable them via databinding rather than setting them all manually.
HTML
<button id="button1" data-win-bind="disabled: disabled">Click</button>
<button id="button2" data-win-bind="disabled: disabled">Click</button>
<button id="button3" data-win-bind="disabled: disabled">Click</button>
<button id="button4" data-win-bind="disabled: disabled">Click</button>
JavaScript
var bindingSource;
app.onactivated = function (args) {
if (args.detail.kind === activation.ActivationKind.launch) {
if (args.detail.previousExecutionState !== activation.ApplicationExecutionState.terminated) {
// TODO: This application has been newly launched. Initialize
// your application here.
} else {
// TODO: This application has been reactivated from suspension.
// Restore application state here.
}
args.setPromise(WinJS.UI.processAll());
var disabledContext = { disabled: false }
var btn = document.getElementById("button1");
btn.addEventListener("click", buttonClickHandler, false);
WinJS.Binding.processAll(btn, disabledContext);
btn = document.getElementById("button2");
btn.addEventListener("click", buttonClickHandler, false);
WinJS.Binding.processAll(btn, disabledContext);
btn = document.getElementById("button3");
btn.addEventListener("click", buttonClickHandler, false);
WinJS.Binding.processAll(btn, disabledContext);
btn = document.getElementById("button4");
btn.addEventListener("click", buttonClickHandler, false);
WinJS.Binding.processAll(btn, disabledContext);
bindingSource = WinJS.Binding.as(disabledContext);
}
};
function buttonClickHandler(eventInfo) {
bindingSource.disabled = true
WinJS.Promise.timeout(5000).then(function (c) {
bindingSource.disabled = false
});
}
--Rob

Related

Cannot enable a Ribbon button programmatically

I developed a VSTO 4 add-in for Excel. It works perfect, however, I have a button placed in the custom tab of its Ribbon control that is initially disabled.
After clicked other ribbon button in my custom tab, I need to enable the initially disabled button.
I tried with:
btnCancelar.Visible = true;
In the Click event of a button, but button is not shown. The strange thing is that when debugging, it still does not appear, but if a MessageBox is shown, the button get visible at last.
I don't understand this behaviour. How can I enable or disable a ribbon button dynamically by code?
I'm not sure what your language is used in your project, but I guess you can tranform it to your own language used. I'll show the example here in C#:
First you need to implement a so called Callback function in the RibbonXML definition:
<button id="buttonSomething" label="Content" size="large" getVisible="EnableControl"/>
then the next step is to implement the Callback function:
public bool EnableControl(IRibbonControl control)
{
return true; // visible ... false = invisible
}
VSTO will trigger the getVisible Callback and depending on the return value enable or disable the visible state (don't forget to remove any Visible property from the RibbonXML, otherwise the Callback is not triggered)
In case of the Ribbon Designer you need to make sure your Click signature is correct, the easies way to do that is by double clicking the button on the ribbon designer. This will create the Click method for you, for instance:
I created a Ribbon with the Ribbon designer and added two buttons. Double clicked the first button to get an empty method like below, and added the code.
private void button1_Click(object sender, RibbonControlEventArgs e)
{
// Toggle button visibility and make sure the button is enabled
// Visible (obviously) makes it visible, while Enabled is grayed if
// false. You don't need this it is Enabled by default, so just for
// demo purposes
button2.Visible = !button2.Visible;
button2.Enabled = button2.Visible;
// Force Ribbon Invalidate ...
this.RibbonUI.Invalidate();
// Long running proces
}
This worked perfectly for me, so if it doesn't work for you please provide more details of your coding.
I have created a workaround to this.
It was simple. Just started the long running process in different thread. That way, cancel button is shown when it should and then hidden after the process ends.
I used this code to launch the process in the Ribbon.cs code:
btnCancelar.Visible = true;
Action action = () => {
Formatter.GenerateNewSheet(Formatter.TargetType.ImpresionEtiquetas, frm.CustomerID, workbook, btnCancelar);
};
System.Threading.Tasks.Task.Factory.StartNew(action);
And inside the process method I have this code:
public static bool GenerateNewSheet(TargetType type, string customerID, Excel.Workbook workbook, Microsoft.Office.Tools.Ribbon.RibbonButton btnCancelar)
{
try
{
_cancelled = false;
InfoLog.ClearLog();
switch (type)
{
case TargetType.ImpresionEtiquetas:
return GenerateTagPrinting(customerID, workbook);
}
return false;
}
finally
{
btnCancelar.Visible = false;
}
}
The interesting thing here I have discovered is that Excel is thread safe, so it was not necessary to add a synchronization mechanism neither when adding rows in the new sheet nor when setting Visible property to false again.
Regards
Jaime

Dynamically setting passwordMask in Titanium

Since Titanium doesn't allow you to manually change the hintText colour of a textfield, I have to set hintText manually. Because of this, I have to dynamically change the passwordMask setting on one of fields I'm using.
However, I'm getting weird behaviour and I can't tell if I'm doing something wrong, or if it's a bug in Titanium.
So, here's my markup:
<TextField id="password" onFocus="passwordFocusEvent" onReturn="passwordReturnEvent" onBlur="passwordBlurEvent" value="password"></TextField>
And some of my controller code:
function passwordFocusEvent(e) {
slideViewUp();
if (e.value === 'password') {
e.source.setPasswordMask(true);
e.source.value = '';
}
}
function passwordBlurEvent(e) {
if (!e.value) {
e.source.setPasswordMask(false);
e.source.value = 'password';
}
}
function passwordReturnEvent(e) {
slideViewDown();
passwordBlurEvent(e);
}
What happens is bizarre. When I focus on the password field, it remains plain text. I enter some text, then click off to another field, stays as plain text.
I click back to the password field, it's STILL plain text.
Now here's the weirdness. Up to this point, I would just assume it's not working. However, when I click off this second time, the passwordMask is set.
Major WTF.
I even tried targeting the field directly using $.password.passwordMask = true; but same thing.
Unfortunately, you cant do this. According to the docs on Ti.UI.TextField in the fine print;
Note: on iOS, passwordMask must be specified when this text field is created.
Its not all bad news though, there are a couple ways you can approach this, one option is to make the password mask yourself, by listening to the change event:
var theStoredPassword = '';
$.password.addEventListener('change', function(e) {
var newpass = e.source.value;
if(newpass.length < theStoredPassword.length) {
// Character deleted from end
theStoredPassword = theStoredPassword.substring(0, theStoredPassword.length-1);
} else {
// Character added to end
theStoredPassword += newpass.substring(newpass.length-1);
}
// Mask the text with unicode ● BLACK CIRCLE, 25CF
$.password.value = new Array(newpass.length + 1).join('●');
});
Another option, would be to have two text fields and swap them out whenever the user focuses the password field, the top one would have the custom hinttext, the bottom one would be passwordMasked. In fact thats probably way easier than what I just coded up. :-)

Clicking on marker cluster wants to open spider with exact same location markers by default

I was trying to implement MarkerCluster (MS) with
Overlapping Marker Cluster (OMS). Everything seems to work just fine.
However, I am looking to modify the way OMS is works so that if I click on a cluster that has 2 points under it
When I click on any Cluster with 2 points in it with exact same geo location, its opens a marker and when I am clicking on that marker it's opening spider with 2 markers.
What I want when I am clicking on the
Cluster, straight a way it will open up spider with 2 markers, already
spend lot of times but still nothing worked.
I already tried many solutions, like
1.
I can track the marker when I am adding to OMS(oms.addMarker), and can
click depending on zoom_changed event of google map, but its not firing
spiderfy rather its firing click event of what we added to markers.....
2.
I have see a event spiderfy, so I tried to trigger that event with a
marker object (oms.trigger('spiderfy', marker);) but nothing working...
Here I am adding code snippet too:
mc = new MarkerClusterer(map, markers.locations, mcOptions);
google.maps.event.addListener(mc, 'clusterclick', function(cluster) {
enter code hereclusterClicked = true;
// HERE WE WANTS TO FIRE SPIDER FUNCTIONALITY ...
});
I could solve this problem, first of all it is obvious that you have implemented and Overlapping Xluster Marker Marker Cluster in your google maps.
My solution is something very simple.
We capture the click event of markerCluster.
obtain the markers of markerCluster.
check whether all the markerCluster markers are in the same position.
if they are in the same position we make a click event trigger the latter obtained the markerCluster marker.
In short this is the code:
var markerClusterer = new MarkerClusterer(map, allMarkers, {styles: styles[0], clusterClass: 'poiCluster', maxZoom:18});
google.maps.event.addListener(markerClusterer, 'click', function(cluster) {
var markers = cluster.getMarkers();
if(estanTodosEnLaMismaPosicion(markers)){
//to wait for map update
setTimeout(function(){
google.maps.event.trigger(markers[markers.length-1], 'click');
},1000)
}
return true;
});
function estanTodosEnLaMismaPosicion(markers){
var cont=0;
var latitudMaster=markers[0].getPosition().lat();
var longitudMaster=markers[0].getPosition().lng();
for(var i=0;i<markers.length;i++){
if(markers[i].getPosition().lat() === latitudMaster & markers[i].getPosition().lng() === longitudMaster ){
cont++;
}else{
return false;
}
}
if(cont==markers.length){
return true;
}else if(cont<markers.length){
return false;
}
}

How to use dojo moveTo programmatically through javascript?

I can markup a Heading or ListItem to have a moveTo attribute and that transition works perfectly.
Is there a way perform a transition to a named view programmatically say, on a button click?
Somewhere on the net I found below code, but its not working. I need something similar to this -
function moveTo(){
var w = dijit.byId('currentView');
w.performTransition('#newView',1,"fade",null);
}
This code sample registers an onclick event handler on a button with the id "ButtonID". After pressing the button, a lookup in the dijit registry will be performed to find the displayed view.
You can call the function performTransition(...) on any dojox.mobile.View.
require(["dijit/registry"], function(registry) {
dojo.ready(function() {
// Button Listener
registry.byId("ButtonID").on("click", function(){
var oldView = dijit.registry.byId("ID_View1");
oldView.performTransition("ID_View2", 1, "slide", null);
});
});
But:
Changing "moveTo" parameters programmatically is much more difficult than performing transitions between views. You have to do some nasty things to override the moveTo Attribute of a widget like for example a Backbutton in a dojox.mobile.Heading
var heading1 = dijit.registry.byId("ID_Heading");
heading1.destroyDescendants();
heading1.moveTo = viewId;
heading1.backButton = false;
heading1._setBackAttr("Zurück");

closing windows ui message popup

I m displaying an popup message for my windows 8 application using javascript like below
var msg = new Windows.UI.Popups.MessageDialog("No internet connection has been found.");
msg.commands.append(new Windows.UI.Popups.UICommand("Try again", commandInvokedHandler));
msg.commands.append(new Windows.UI.Popups.UICommand("Close", commandInvokedHandler));
msg.defaultCommandIndex = 0;
msg.cancelCommandIndex = 1;
msg.showAsync();
Now I want to close the popup message programatically after some timeinterval as the user has not given any input.
I do not think there is a hide/dismiss/cancel command for that kind of message popup. The cancel command is invoked when pressing the escape key on you're keyboard. Thees kinds of messages is not intended for information purpose. You should instead use a "FlyOut".
HTML:
<!-- Define a flyout in HTML as you wish -->
<div id="informationFlyout" data-win-control="WinJS.UI.Flyout">
<p>
Some informative text
</p>
</div>
<!-- an anchor for the flyout, where it should be displayed -->
<div id="flyoutAnchor"></div>
JS:
// Get an anchor for the flyout
var flyoutAnchor = document.getElementById("flyoutAnchor");
// Show flyout at anchor
document.getElementById("informationFlyout").winControl.show(flyoutAnchor);
To dismiss the flyout after a set amount of time you could just do a setTimeout and hide in you're code:
// execute this code after 2000ms
setTimeout(function () {
// Fetch the flyout
var flyout = document.getElementById("informationFlyout"); // Get the flyout from HTML
// Check if the element still exists in DOM
if (flyout)
flyout.winControl.hide(); // Dismiss the flyout
}, 2000);
Read more about the FlyOut-popups here
There is infact a cancel that you can call on the object returned by the showAsync method, first call the messageDialog in this way:
var msg = new Windows.UI.Popups.MessageDialog("No internet connection has been found.");
var asyncOperation = msg.showAsync();
then whenever you want you can call:
asyncOperation.cancel();
and the messageDialog will be dismissed.