<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: options code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sat, 17 May 2008 18:07:04 GMT</pubDate>
    <description>DZone Snippets: options code</description>
    <item>
      <title>HTML/JavaScript - Select list - Add/Remove Options (DOM)</title>
      <link>http://snippets.dzone.com/posts/show/4442</link>
      <description>from http://www.mredkj.com/tutorials/tutorial005.html&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Overview&lt;br /&gt;&lt;br /&gt;    * Insert Before Selected - A new option is created and added above the selected option (as determined by selectedIndex). If none are selected, then no option is added.&lt;br /&gt;    * Remove Selected - Deletes the selected option (or options) from the list. If no options are selected, no options are deleted.&lt;br /&gt;    * Append Last - No matter what is selected, a new option is added at the end.&lt;br /&gt;    * Remove Last - No matter what is selected, the last option is deleted from the list.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Explanation&lt;br /&gt;&lt;br /&gt;According to DOM Level 1, the following is the syntax for the add and remove methods in HTMLSelectElement:&lt;br /&gt;&lt;br /&gt;void add(in HTMLElement element, in HTMLElement before) raises(DOMException);&lt;br /&gt;void remove(in long index);&lt;br /&gt;&lt;br /&gt;The add method takes two arguments: the element to add, and the element to insert before. The spec also says you can add to the end of the list by passing null as the second argument.&lt;br /&gt;&lt;br /&gt;The remove method just takes a number: the index of the option to be removed. &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The JavaScript&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;script language="JavaScript" type="text/javascript"&gt;&lt;br /&gt;&lt;!--&lt;br /&gt;var count1 = 0;&lt;br /&gt;var count2 = 0;&lt;br /&gt;&lt;br /&gt;function insertOptionBefore(num)&lt;br /&gt;{&lt;br /&gt;  var elSel = document.getElementById('selectX');&lt;br /&gt;  if (elSel.selectedIndex &gt;= 0) {&lt;br /&gt;    var elOptNew = document.createElement('option');&lt;br /&gt;    elOptNew.text = 'Insert' + num;&lt;br /&gt;    elOptNew.value = 'insert' + num;&lt;br /&gt;    var elOptOld = elSel.options[elSel.selectedIndex];  &lt;br /&gt;    try {&lt;br /&gt;      elSel.add(elOptNew, elOptOld); // standards compliant; doesn't work in IE&lt;br /&gt;    }&lt;br /&gt;    catch(ex) {&lt;br /&gt;      elSel.add(elOptNew, elSel.selectedIndex); // IE only&lt;br /&gt;    }&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;function removeOptionSelected()&lt;br /&gt;{&lt;br /&gt;  var elSel = document.getElementById('selectX');&lt;br /&gt;  var i;&lt;br /&gt;  for (i = elSel.length - 1; i&gt;=0; i--) {&lt;br /&gt;    if (elSel.options[i].selected) {&lt;br /&gt;      elSel.remove(i);&lt;br /&gt;    }&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;function appendOptionLast(num)&lt;br /&gt;{&lt;br /&gt;  var elOptNew = document.createElement('option');&lt;br /&gt;  elOptNew.text = 'Append' + num;&lt;br /&gt;  elOptNew.value = 'append' + num;&lt;br /&gt;  var elSel = document.getElementById('selectX');&lt;br /&gt;&lt;br /&gt;  try {&lt;br /&gt;    elSel.add(elOptNew, null); // standards compliant; doesn't work in IE&lt;br /&gt;  }&lt;br /&gt;  catch(ex) {&lt;br /&gt;    elSel.add(elOptNew); // IE only&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;function removeOptionLast()&lt;br /&gt;{&lt;br /&gt;  var elSel = document.getElementById('selectX');&lt;br /&gt;  if (elSel.length &gt; 0)&lt;br /&gt;  {&lt;br /&gt;    elSel.remove(elSel.length - 1);&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;//--&gt;&lt;br /&gt;&lt;/script&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The HTML&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;form&gt;&lt;br /&gt;&lt;input type="button" value="o" onclick="insertOptionBefore(count1++);" /&gt;&lt;br /&gt;Insert Before Selected&lt;br /&gt;&lt;br /&gt;&lt;input type="button" value="o" onclick="removeOptionSelected();" /&gt;&lt;br /&gt;Remove Selected&lt;br /&gt;&lt;br /&gt;&lt;select id="selectX" size="10" multiple="multiple"&gt;&lt;br /&gt;&lt;option value="original1" selected="selected"&gt;Orig1&lt;/option&gt;&lt;br /&gt;&lt;option value="original2"&gt;Orig2&lt;/option&gt;&lt;br /&gt;&lt;/select&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;input type="button" value="o" onclick="appendOptionLast(count2++);" /&gt;&lt;br /&gt;Append Last&lt;br /&gt;&lt;br /&gt;&lt;input type="button" value="o" onclick="removeOptionLast();" /&gt;&lt;br /&gt;Remove Last&lt;br /&gt;&lt;/form&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;</description>
      <pubDate>Wed, 22 Aug 2007 12:39:41 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4442</guid>
      <author>bradalyst (brad)</author>
    </item>
    <item>
      <title>Using Ruby hashes as keyword arguments, with easy defaults</title>
      <link>http://snippets.dzone.com/posts/show/2329</link>
      <description>Similar to many Rails helpers/methods, a lot of the methods I write often use an optional hash of options, or sometimes just a hash only, to simulate keyword arguments (often using symbols).&lt;br /&gt;&lt;br /&gt;The only downside to doing this is you lose out on easily setting default values using Ruby's default method argument values. You might use code something similar to the following to make up for this:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;def some_method(opts={})&lt;br /&gt;  my_foo =  opts[:foo] || 'mydefaultfoo'&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;However, as you have more and more keyword options, setting defaults in this way gets rather tedious. Fortunately, Ruby's Hash#merge comes to our rescue (almost) - it allows you to merge the contents of one hash with another. The only problem - any duplicate keys in the hash you are merging will overwrite your original hash values - when it comes to setting default values, we want this to work the other way around; we only want values in the defaults hash to be merged if they do not exist in the original hash. Again, Ruby comes to our rescue - Hash#merge takes a block as an argument and will pass any duplicate values that crop up into the block - we can use this block to decide which value to keep.&lt;br /&gt;&lt;br /&gt;Using the simple monkey patch to the Hash class below, you will no longer have to set each default individually:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;class Hash&lt;br /&gt;  def with_defaults(defaults)&lt;br /&gt;    self.merge(defaults) { |key, old, new| old.nil? ? new : old } &lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  def with_defaults!(defaults)&lt;br /&gt;    self.merge!(defaults) { |key, old, new| old.nil? ? new : old }&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Of course, sticking with Ruby naming conventions, with_defaults() will return a new hash whilst with_defaults!() will change the original hash directly. &lt;br /&gt;&lt;br /&gt;See http://www.lukeredpath.co.uk/index.php/2006/07/27/using-ruby-hashes-as-keyword-arguments-with-easy-defaults/ for further discussion and alternatives.</description>
      <pubDate>Thu, 27 Jul 2006 13:17:06 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2329</guid>
      <author>lukeredpath (Luke Redpath)</author>
    </item>
    <item>
      <title>Tag Cloud in Ruby</title>
      <link>http://snippets.dzone.com/posts/show/2251</link>
      <description>The options.delete with the default looks mildly interesting. Just wanted to remember that.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;def font_size_for_tag_cloud( total, lowest, highest, options={} )&lt;br /&gt; return nil if total.nil? or highest.nil? or lowest.nil?&lt;br /&gt; #&lt;br /&gt; # options&lt;br /&gt; maxf = options.delete( :max_font_size ) || 14&lt;br /&gt; minf = options.delete( :min_font_size ) || 11&lt;br /&gt; maxc = options.delete( :max_color ) || [ 0, 0, 0 ]&lt;br /&gt; minc = options.delete( :min_color ) || [ 156, 156, 156 ]&lt;br /&gt; hide_sizes = options.delete( :hide_sizes )&lt;br /&gt; hide_colours = options.delete( :hide_colours )&lt;br /&gt; #&lt;br /&gt; # function to work out rgb values&lt;br /&gt; def rgb_color( a, b, i, x)&lt;br /&gt;  return nil if i &lt;= 1 or x &lt;= 1&lt;br /&gt;  if a &gt; b&lt;br /&gt;   a-(Math.log(i)*(a-b)/Math.log(x)).floor&lt;br /&gt;  else&lt;br /&gt;   (Math.log(i)*(b-a)/Math.log(x)+a).floor&lt;br /&gt;  end&lt;br /&gt; end&lt;br /&gt; #&lt;br /&gt; # work out colours&lt;br /&gt; c = []&lt;br /&gt; (0..2).each { |i| c &lt;&lt; rgb_color( minc[i], maxc[i], total, highest ) || nil }&lt;br /&gt; colors = c.compact.empty? ? minc.join(',') : c.join(',')&lt;br /&gt; #&lt;br /&gt; # work out the font size&lt;br /&gt; spread = highest.to_f - lowest.to_f&lt;br /&gt; spread = 1.to_f if spread &lt;= 0&lt;br /&gt; fontspread = maxf.to_f - minf.to_f&lt;br /&gt; fontstep = spread / fontspread&lt;br /&gt; size = ( minf + ( total.to_f / fontstep ) ).to_i&lt;br /&gt; size = maxf if size &gt; maxf&lt;br /&gt; #&lt;br /&gt; # display the results&lt;br /&gt; size_txt = "font-size:#{ size.to_s }px;" unless hide_sizes&lt;br /&gt; color_txt = "color:rgb(#{ colors });" unless hide_colours&lt;br /&gt; return [ size_txt, color_txt ].join&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 06 Jul 2006 17:01:58 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2251</guid>
      <author>jnunemaker ()</author>
    </item>
  </channel>
</rss>
