Toggle widget visibility with single button - google-apps-script

The following code should write the inverse of true/false as found in the textbox back into the textbox when the button is clicked - but it doesn't work correctly. It will work correctly one way, but not the other (the one that works is whichever ClickHandler was defined last). I've tried using validateNotMatches too but no joy.
If I change the code so that the label's text is updated instead of the textbox's then that works fine.
I am aware of suggested workarounds such as using two buttons, but I just want to know if I'm doing something wrong or if this looks like a bug in GAS. Thanks.
function doGet(e)
{
var app = UiApp.createApplication();
var tb = app.createTextBox().setText('true');
var button = app.createButton('button');
var label = app.createLabel().setText('-');
button.addClickHandler(app.createClientHandler()
.validateMatches(tb, 'true')
//.forTargets(label).setText('false')
.forTargets(tb).setText('false')
);
button.addClickHandler(app.createClientHandler()
.validateMatches(tb, 'false')
//.forTargets(label).setText('true')
.forTargets(tb).setText('true')
);
return app.add(app.createHorizontalPanel().add(tb).add(button).add(label));
}

It's not a bug... both events fire. The validation happens on the current state of the widget, not on the state when the event was fired, so after you flip it to "false" the second handler validates and flips it back to "true".

Related

AS3 set focus on specific component

I have a form with several component: datagrid, textArea, text input...
For each component FocusIn Event is available.
var objTarget:String;
protected function memo_focusInHandler(event:FocusEvent):void
{
objTarget=event.currentTarget.id;
}
With memo_focusInHandler, I know which has focus.
My goal is to backup last focus objet, and re open Windows with focus on this object.
I try to do this:
objTarget.setfocus();
But it doesn't work. Could you help to found the best way to reach my goal.
String is not a display object, thus it can't be in focus. The representation of string on the stage is a TextField.
In as3 you can set focus to the desired target by using stage method:
stage.focus = myTarget;
Please see the corresponding documentation section: https://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/Stage.html#focus
There is no need (that you've shown) to work with the string id reference. It would much simpler (and slightly more efficient) to work directly with the object reference.
var objTarget:Object; // Object instead of type :String
protected function memo_focusInHandler(event:FocusEvent):void {
objTarget = event.currentTarget; //instead of the currentTarget's id property, assign the current target itself
}
Then, when you want to reset focus, you can do:
if(objTarget is TextInput || objTarget is TextArea){ //make sure it's a text input or text area first - optional but recommended if you don't like errors
objTarget.selectRange(objTarget.text.length, objTarget.text.length); //set cursor to the end
objTarget.setFocus(); //focus the text input/area
}
I found a solution:
this[objTarget].selectRange(this[objTarget].text.length, this[objTarget].text.length);
this[objTarget].setFocus();

Blinking Caret Not Showing up in Input Text Field

I want one of my input text boxes to be the stage focus as well as have the blinking caret without the user needing to click inside the text field. I've been searching around frantically for an answer to this question, and everyone's answer boils down to this code: (the instance name of the text field being "input")
stage.focus = input;
input.setSelection(0, input.text.length);
But for some reason this code isn't working for me. Anyone have any idea why?
Update
For some reason this works:
stage.addEventListener(MouseEvent.CLICK,update);
function update(e:MouseEvent){
stage.focus = input;
}
And this does as well but the caret doesn't blink:
var counter:int=0;
stage.addEventListener(Event.ENTER_FRAME,update);
function update(e:Event){
counter++;
if(counter>30){
stage.focus = input;
}
}
This still doesn't satisfy my question though, why do you need a mouse click of some type in order to make my desired action work properly?
1.. How about if you set focus to happen inside a mouseClick function?
2.. Try this
stage.focus = input;
input.text = " "; //with a space (for blank but not empty);
input.setSelection(0, input.text.length);
3.. Bail out scenario then this utility might help or at least you'll learn something from its code Link here

User confirmation from a formatted screen

I developed an app that runs from a Spreadsheet. It has a formatted screen, ie. doc.show(app
). There is a mass change function. I'd like to prompt the user for 'Are you sure ... OK to Continue'.
The ideal solution would be a pop-up form with an "OK to continue" response.
I can't find a way to prompt the user and accept a response that doesn't simultaneously take down the formatted screen.
I know there must be a easy way to do this. I searched this forum and others, but cannot find a solution that applies to Google Spreadsheets.
When using the GUI Builder I've found a very simple solution that is to create a panel or a label that is actually masking the whole UI (or part of it) and that is normally invisible.
When I make it visible I can click on it, it turns invisible again and I'm back on the standard UI. It uses the ability in GUI builder to move elements backwards and forwards so masking is very easy (a sort of multi layer design). I guess the same behavior is achievable with script defined UI but I'm not sure how...
regards,
Serge
EDIT : For information : I just set up an interface using this technique and I noticed that panels that have been made invisible must be restored along with all their elements otherwise they reappear empty. Using Clienthandlers here is an example with two panels and two buttons that do the job :
var panhandler0 = app.createClientHandler()
.forTargets(panel1).setVisible(false);// hide panel1 when button 'ENTER'on panel1 is pressed
enter.addClickHandler(panhandler0);
var panhandler1 = app.createClientHandler()
.forTargets(panel2,msg,grid2).setVisible(true);// show panel2 when button 'ENTER' on panel1 is pressed
enter.addClickHandler(panhandler1);
var panhandler2 = app.createClientHandler()
.forTargets(panel2,msg,grid2).setVisible(true);// re-show panel2 when button 'retry'on panel2 is pressed
retry.addClickHandler(panhandler2);
var panhandler3 = app.createClientHandler()
.forTargets(panel2).setVisible(false);// hide panel2 when button 'retry'on panel2 is pressed
retry.addClickHandler(panhandler3);
var panhandler4 = app.createClientHandler()
.forTargets(panel1,txt,grid,hpanel).setVisible(true);// re-show panel1 when button 'retry'on panel2 is pressed
Works nicely !
You can use Browser.MsgBox() and add buttons into it for users confirmation.
Refrence URL
https://developers.google.com/apps-script/class_browser
This is indeed not an easy thing to do. You'll have to use your own GUI (formatted screen) to show the message. This way you can restore the state of your GUI again. Here is an example:
function uiTest() {
var app = UiApp.createApplication().setTitle("UI");
app.add(app.createGrid(1,1).setId('main'));
createGUI(app,{});
SpreadsheetApp.getActive().show(app);
}
function createGUI(app,p) {
var panel = app.createVerticalPanel();
var handler = app.createServerHandler('handler').addCallbackElement(panel);
panel.add(
app.createTextBox().setName('text').setId('text').setText(p.text ? p.text : "")).add(
app.createButton('Do it').addClickHandler(handler));
app.getElementById('main').setWidget(0,0, panel);
}
function handler(e) {
var app = UiApp.getActiveApplication();
var hidden = app.createHidden('oldValues', JSON.stringify(e.parameter));
app.getElementById('main').setWidget(0,0, app.createVerticalPanel().add(
hidden).add(
app.createLabel("Question message")).add(
app.createButton('Ok').addClickHandler(app.createServerHandler('after').addCallbackElement(hidden))));
return app;
}
function after(e) {
var app = UiApp.getActiveApplication();
createGUI(app,JSON.parse(e.parameter.oldValues));
return app;
}
The difficulty will really depend on how much state you need to restore. If none, then it's a lot, lot easier. In this example I restore the value of the textbox. You can type in anything there to see that it will be there after the popup.

how to get a button in an Openlayers popup?

I am trying to put a button inside of an Openlayers popup. While the button appears to display correctly with the following code, the function 'handlerFunc' does not execute when the button is clicked. The segment of code I have posted is all within another function (so handlerFunc is actually a nested function). I'm using JQuery for the button itself. Any ideas on what might be going wrong? Thanks!
var feature = new OpenLayers.Feature(presences, ll);
feature.popupClass = popupClass;
feature.data.popupContentHTML = "<button id='popupButton'>Click me</button>";
feature.data.overflow = (overflow) ? "auto" : "hidden";
feature.data.icon = markerIcon;
$('#popupButton').button();
$('#popupButton').click(handlerFunc);
function handlerFunc() {
// do something
}
Most likely reason is that your button doesn't exist when you bind to a click event. $('#popupButton') returns null. Instead of using $('#popupButton').click(handlerFunc); try $('#popupButton').live('click', handlerFunc);. That means that we bind to an event not only when DOM is built, but when object appears.

Simple Flash form, redirecting to URL using textfield as argument

This seemed simple enough, I do know Java, PHP and JavaScript, so it's simply a matter of not enough knowledge of the Flash/ActionScript 3.0 platform.
After doing some research, I added a layer to the movie for the code (Flash CS5). I then right-click, select "Actions" and write this code.
addEventListener(Event.ENTER_FRAME, setup);
submitButton.addEventListener(MouseEvent.CLICK, onSubmit);
queryField.addEventListener(MouseEvent.CLICK, onQueryClick);
var defaultText = "Search...";
function setup(){
queryField.text = defaultText;
}
function onSubmit(){
if(queryField.text != defaultText && queryField.text != ""){
navigateToURL(new URLRequest("http://www.somedomain.com/?query=" + queryField.text), "_blank");
}
}
function onQueryClick(){
if(queryField.text == defaultText){
queryField.text = "";
}
}
As you can see, I want to add a default text on load to the form field referenced by "queryField", which should clear on focusing the field. When submitting the form the browser should redirect to an URL appending the value of queryField.
As I said, I don't have enough knowledge of the platform to know what's wrong. Although it seems like maybe the references in the functions are not available due to the different scope. But a different example I saw ignored scope of form field references as well.
Right now, the published movies does precisely nothing.
In order to set a default value to the TextField, there is no need to use an ENTER_FRAME event. Just do that :
var defaultText = "Search...";
queryField.text = defaultText;
In your code, the TextField is continously updated with the default value (see the ENTER_FRAME event documentation). That's why nothing happend, the TextField is always equal to the default text.