Spinning wheel on webkit for iPhone/iPod touch

First things first. Have a look at the demo page or, if you don’t have your device at hand, watch the screencast I baked for you.
How to use the script
The widget is composed of two parts: the stylesheet and the javascript. No HTML is needed as all the elements are created by the script on the fly. Include both the JS and the CSS into your page and you are ready to spin. You’ll be also surprised to see that the spinning wheel itself is built with just two images, while other three images are needed for the header and buttons. The PNGs altogether are 4.3kb.
The code does not need initialization on window load. You cannot have more than one picker at a time, so the SpinningWheel object is unique and it is created as soon as you include the JS file.
The first thing you need to do is to define the slots with:
SpinningWheel.addSlot(obj values, str styles, str defaultValue)
values is in the form of: { key: value, key2: value, ... }. Keys are the identifiers that won’t be shown in the picker (think of them as the value parameter in the <option value="foo">bar</option> tag). Values are the labels printed on the slots.
styles is a list of space separated predefined styles to be applied to the slot. The available values are:
right, align text inside the slot to the right;readonly, the slot can’t be spun;shrink, shrink the slot width to the minimum possible.
The first element of the slot will be selected if no defaultValue is defined.
When all the slots have been created, set the default actions for the cancel and donebuttons.
SpinningWheel.setCancelAction( function(){ } );
SpinningWheel.setDoneAction( function() { } );
Finally show the picker:
SpinningWheel.open();
Voila, the Picker View is ready for countless hours of spinning pleasure.
To get the actual selected values call:
var result = SpinningWheel.getSelectedValues();
result.keys will be filled with an array of the selected keys while result.values will hold the list of the selected values (or labels).
Let’s wrap everything together.
function swExample() {var numbers = { 0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8, 9: 9 };SpinningWheel.addSlot(numbers, 'right');SpinningWheel.addSlot(numbers, 'right');SpinningWheel.addSlot({ separator: '.' }, 'readonly shrink');SpinningWheel.addSlot(numbers, 'right');SpinningWheel.addSlot({ Kg: 'Kg', Lb: 'Lb', St: 'St' }, 'shrink');SpinningWheel.setCancelAction(cancel);SpinningWheel.setDoneAction(done);SpinningWheel.open();}function done() {var results = SpinningWheel.getSelectedValues();alert('values:' + results.values.join(', ') + ' - keys: ' + results.keys.join(', '));}function cancel() {alert('cancelled!');}function swExample() { var numbers = { 0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8, 9: 9 }; SpinningWheel.addSlot(numbers, 'right'); SpinningWheel.addSlot(numbers, 'right'); SpinningWheel.addSlot({ separator: '.' }, 'readonly shrink'); SpinningWheel.addSlot(numbers, 'right'); SpinningWheel.addSlot({ Kg: 'Kg', Lb: 'Lb', St: 'St' }, 'shrink'); SpinningWheel.setCancelAction(cancel); SpinningWheel.setDoneAction(done); SpinningWheel.open();} function done() { var results = SpinningWheel.getSelectedValues(); alert('values:' + results.values.join(', ') + ' - keys: ' + results.keys.join(', '));} function cancel() { alert('cancelled!');}
Look at the demo for more examples.
Create custom styles
I preconfigured for you three styles for the slots, but you can add as many as you need. Say you want a slot with center aligned text. Add the following to the stylesheet:
#sw-slots .sw-center { text-align:center; }
To apply the style create the slot like this:
SpinningWheel.addSlot(values, 'center');
A piece of cake.
By default the slots try to fit their content. Slots with long text will be wider than ones with short text. (Same as the <table /> cell elements). With custom style you can override this behavior. If you have two slots and you want them to be exactly the same width you may add the following style:
#sw-slots .sw-50percent { width:50%; }
and create the slots with:
SpinningWheel.addSlot(values, '50percent'); SpinningWheel.addSlot(values);
You don’t need to apply the style to both slots as the second will fit the remaining space (or the other 50% of the screen width).
Limitations
None that I can tell if not those imposed by the device small CPU. All animations are hardware accelerated, the “birth date” example in the demo creates more than one hundred elements and all animations are pretty fluid.
The script is also compatible with both landscape and portrait mode and you can freely switch from one to the other while the spinning wheel is opened. (That is more than what the native Picker View has to offer).
Note that once closed the spinning wheel is completely inaccessible and all variables will be null or undefined. So basically you can’t programmatically query theSpinningWheel object while it is not visible.

(1 votes, average: 4.00 out of 5)
I’ve been working with jqtouch as well and the conflict is easily fixed with a few tweaks to the css. Note that one of the first selectors in the jtouch.css hides all elements under the body tag, so what we have to do is reinforce a couple selectors in spinningwheel.css to make sure certain element styles wont get stomped on.The primary one is here:
#sw-wrapper {position:absolute; z-index:1000;left:0;width:100%;display:inline !important;min-height: 0px !important;font-family:helvetica, sans-serif;background:rgba(0,0,0,0.7);text-align:left;}
You must add display:inline !important; and min-height: 0px !important; otherwise the container will inherit these values from jtouch.css and you don’t want that.The other change is here:
#sw-slots li {border-top: 0px solid black !important;color:#000000 !important;padding:0 8px;height:44px;overflow:hidden;font:bold 24px/44px Helvetica,sans-serif;}
Again, you must add border-top: 0px solid black !important; and color:#000000 !important; or they will get inherited from theme.css (at least they will in the apple theme) and that makes the list items get out of sync and look all messed up.With these two tweaks you will be able to see the control slide up into place and operate as advertised. You will still have to wire your cancel and done actions of course, but this will get you on your way.
Like or Dislike:
1
0