Recent Widgets


Register for DashboardWidgets

Recent Forums Posts

Partners


iCompositions

MacDesktops.net

RSS Showcase
RSS Comments
RSS Forums

This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies. Posted in: Widget Development

How to save two different preferences?

Author Message
paulx



Joined: 16 Aug 2005
Posts: 7

Posted: Tue Nov 13, 2007 - 4:19 pm    Post subject: How to save two different preferences? Reply with quote

Hi there,

I have something like that in my widget:
Code:

<form id="id" name="name">
   <select name="selectname" size=1 onChange="show(this);" id='selectid'>

         <option value="http://www.url.com/img.jpg">img 1</option>
         <option value="http://www.url.com/img2.jpg">img 2</option>
         <option value="anotherDivID">3</option>
 
   </select>
</form>

The "value" of these options are different: URLs to image files and one contains an id of an div ("anotherDivID").
Now my question is how to save the "value" of the selected option to the preference file?

I know some basic how to save to preference but I don't know how to "access" the value of the "option" using javascript.
Can someone point me in the right direction?
View user's profile Send private message Send e-mail Widgets
Greenie



Joined: 08 Jun 2005
Posts: 81

Posted: Wed Nov 14, 2007 - 4:32 pm    Post subject: Reply with quote

some possible ways ("myList" is a reference to a select-object):

This sets myVar to the value of the option with index i:

Code:
myVar = myList.options[i].value;


This sets myVar to the value of the selected option:

Code:
myVar = mylist[myList.selectedIndex].value;


Hope that helps. Good luck
View user's profile Send private message Send e-mail AIM Address ICQ NumberWidgets
Quill



Joined: 23 Feb 2007
Posts: 11

Posted: Thu Nov 15, 2007 - 6:09 am    Post subject: Passing Option Values from HTML to Javascript Reply with quote

Hello,

You could also use the "this" property to pass on the value of the selected option (in your pop-up menu) to your function "show".

For example:

Code:
function show(myValue)
{
   if (window.widget)   // Check that it is running as a Dashboard widget
   {
      widget.setPreferenceForKey(myValue.value+"", "myPreference");
   }
}


The preference "myPreference" would now be set to the value of the option the user chose.

The reason for adding the null string "" to myValue is simply to force Dashboard's required typing for this parameter (thanks to Gandreas for that tip).

Edit: The code I originally submitted had an error in it, which was picked up by paulx. I had myValue instead of myValue.value in the setPreferenceForKey command... I've now fixed it in the code above (sorry!).
View user's profile Send private message Send e-mail Visit poster's website Widgets
paulx



Joined: 16 Aug 2005
Posts: 7

Posted: Thu Nov 15, 2007 - 8:18 am    Post subject: Reply with quote

Wow! Thanks a lot!

That was very, very helpful and I figured it out - it works now! Very Happy
But i had to change show(this) to show(value) in the select menu.

However, thanks a lot again! Very Happy


I have another question:
I added a CSS-styleswitcher to a widget. Is there a way to include a simple and smooth animation when switching between 2 different CSS files?
View user's profile Send private message Send e-mail Widgets
Greenie



Joined: 08 Jun 2005
Posts: 81

Posted: Thu Nov 15, 2007 - 10:06 am    Post subject: Reply with quote

I guess you mean something like a morphing-effect from one skin/theme to another? Don't think this is possible.

But it should be possible to use the AppleAnimator-classes to create some kind of fading-effect (haven't tried that out).
The first thing that comes to my mind would be to fade out the widget (changing opacity in several steps until the widget is invisible), then change the css-class/style/theme and then fade the widget in again.
View user's profile Send private message Send e-mail AIM Address ICQ NumberWidgets
paulx



Joined: 16 Aug 2005
Posts: 7

Posted: Fri Nov 16, 2007 - 8:17 pm    Post subject: Re: Passing Option Values from HTML to Javascript Reply with quote

Quill wrote:

Edit: The code I originally submitted had an error in it, which was picked up by paulx. I had myValue instead of myValue.value in the setPreferenceForKey command... I've now fixed it in the code above (sorry!).


Hi Quill,

thanks for the fix - very appreciated! Cool
Somehow I got it to work but your fix shed light on an other issue.
Thanks a lot (again)!
View user's profile Send private message Send e-mail Widgets
paulx



Joined: 16 Aug 2005
Posts: 7

Posted: Fri Nov 16, 2007 - 8:22 pm    Post subject: Reply with quote

OK, now I have another basic question:
In javascript is there a way to shorten something like that?:

Quote:
document.getElementById("div").style.top = "44px";
document.getElementById("div").style.left = "22px";
document.getElementById("div").style.position = "absolute";
...
...


I wasn't able to figure it out. Geek
View user's profile Send private message Send e-mail Widgets
Quill



Joined: 23 Feb 2007
Posts: 11

Posted: Fri Nov 16, 2007 - 9:46 pm    Post subject: Simplifying Code Reply with quote

You could simplify it like this:

Code:
var myDiv = document.getElementById("myDiv").style;
myDiv.top = "44px";
myDiv.left = "22px";
myDiv.position = "absolute";


Quill
View user's profile Send private message Send e-mail Visit poster's website Widgets
paulx



Joined: 16 Aug 2005
Posts: 7

Posted: Tue Nov 20, 2007 - 10:34 am    Post subject: Reply with quote

Thanks again!

Is there a way to add / update / delete pre-defined menu-items via Javascript?
I want to make it possible to enable/disable some menu-items by clicking a checkbox.

Quote:
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option> <<<How to disable/enable this menu-item?


I just found this but don't know how to handle it (or whether it is useful or not Smile:
http://forum.mootools.net/topic.php?id=3110
Quote:
var option = new Element('option');
option.setProperty("value", value);
option.setHTML(text);
$('theSelect').adopt(option);
View user's profile Send private message Send e-mail Widgets
Greenie



Joined: 08 Jun 2005
Posts: 81

Posted: Tue Nov 20, 2007 - 12:03 pm    Post subject: Reply with quote

I don't think there's a way to disable certain options. Theoretically it should be possible to use the html-attribute "disabled" (either just by using the word alone or by writing ' disabled="disabled" '. But that doesn't seem to work in Dashboard.
But anyway, here are ways to add/delete/edit options.

declare a variable, pointing to the selectlist:

Code:
myList = document.getElementById("ASelectListID");


add an entry to the selectlist:

Code:
myList.options[myList.length] = new Option("visibleText","optionValue");


The above works (adds an option to the end of a selectlist) because options are indexed automatically starting from 0. So if you have a selectlist with 1 element, the length is 1 but the option with index 1 doesn't exist (yet) and is created by the above command


change value or text of an existing entry:
Code:
myList.options[1].value = "ValueOfOptionEntry";
myList.options[1].text = "VisibleTextOfOptionEntry";


deleting one option should work like this:

Code:
  myList.options[1] = null;


this practically removes the given option, re-indexing the other options and setting the selectlists length
View user's profile Send private message Send e-mail AIM Address ICQ NumberWidgets
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.

 
Powered by phpBB © 2001, 2002 phpBB Group