Code http://www.drewwilson.com/stream/code/ Code 20 Titan: An Introduction http://www.drewwilson.com/stream/code/titan-an-introduction Drew Wilson <p> <a href="http://www.titanproject.org">Titan</a> is amazing. It is extremely easy to use and <strong>will</strong> speed up your development time. If you already know jQuery &amp; Javascript there is zero learning curve.<br> The only hard part about Titan, has been getting the word out :) </p> <p> If you have any questions or comments please use the Titan <a href="https://groups.google.com/group/titan-js">Google Group</a>.<br> Titan will be released soon, I just need to do some initial documentation. </p> <h3>What is Titan?</h3> <p> Titan is an Open Source Javascript Web Framework. It is built on jQuery, making it the easiest, smallest, and most liberating Javascript Web Framework to work with.<br> Titan interfaces with JSON for you, enabling you to easily put data from JSON into your HTML elements via <b>Javascript</b> or <b>CSS Class Names</b>.<br> Titan will even query the server for you using REST methods to retrieve the JSON you will be working with. </p> <h3>Why Create Titan?</h3> <p> Javascript (among others) is the future of the web. This future has in fact already begun to take form and Titan is a pioneer, paving the way.<br> There are other Javascript Web Frameworks, but there aren't any that give you the freedom to use your own interface. Titan doesn't limit you to an particular interface since it uses the HTML that you construct, rather than it's own.<br> Using Titan you can build any form of web application or website. Just like jQuery, Titan compliments your workflow and doesn't restrict you in any way. Additionally your interface is dictated by you, and not the framework. Titan works <strong>with</strong> you, not the other way around. </p> <p> Titan was created by: <a href="http://www.builditwith.me/user/cbosuna">Mike Osuna</a>, <a href="http://www.builditwith.me/user/willwilson">Will Wilson</a>, <a href="http://www.builditwith.me/user/drewwilson">Drew Wilson</a> </p> <p> Titan was built on these core values: </p> <ul> <li><b>Template Independence</b> - Titan must not generate HTML, instead it uses the existing user defined HTML elements.</li> <li><b>Standards Based / Validation</b> - Titan must not break HTML or CSS validation. The Titan CSS interface is completely valid.</li> <li><b>Less Code is Better Code</b> - Keep things small. Titan is only 25kb.</li> </ul> <h3>Titan Overview Video</h3> <p> Most all of your questions should be answered in this video. If you have any other questions, please leave them on the Titan <a href="https://groups.google.com/group/titan-js">Google Group</a>. Thanks to <a href="http://www.perfectspace.com">Nate Ritter</a> for the interview!<br> <object width="686" height="386"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id=8763767&amp;server=vimeo.com&amp;show_title=0&amp;show_byline=0&amp;show_portrait=0&amp;color=0871a8&amp;fullscreen=1" /><embed src="http://vimeo.com/moogaloop.swf?clip_id=8763767&amp;server=vimeo.com&amp;show_title=0&amp;show_byline=0&amp;show_portrait=0&amp;color=0871a8&amp;fullscreen=1" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="686" height="386"></embed></object> </p> <h3>Titan Development Example Video</h3> <p> This video is a slightly out-dated and was originally released before the launch of my CMS <a href="http://www.firerift.com">Firerift</a>. Everything in the video is still relevant and current, however it is specifically relating to using Titan with Firerift. So you will notice many references to Firerift and Firerift specific features.<br> <object width="686" height="429"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id=5815767&amp;server=vimeo.com&amp;show_title=0&amp;show_byline=0&amp;show_portrait=0&amp;color=0871a8&amp;fullscreen=1" /><embed src="http://vimeo.com/moogaloop.swf?clip_id=5815767&amp;server=vimeo.com&amp;show_title=0&amp;show_byline=0&amp;show_portrait=0&amp;color=0871a8&amp;fullscreen=1" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="686" height="429"></embed></object> </p> <h3>Where is Titan Headed?</h3> <p> Titan is Open Source, so hopefully it "heads" into the hands of all of you guys! The initial core of Titan is built and has been tested and used in production on many different websites and web apps: <a href="http://www.firerift.com">Firerift</a>, <a href="http://www.builditwith.me">Built It With Me</a>, etc.<br> However, there are still many improvements that can be made and additional features that would be nice to implement. We most definitely welcome any code contributes by you all, with the goal of making Titan the "Javascript Framework of Choice" on the web. </p> <br /> http://www.drewwilson.com/stream/code/titan-an-introduction Sun, 17 Jan 2010 01:05:47 PST TipTip jQuery Plugin http://www.drewwilson.com/stream/code/tiptip-jquery-plugin Drew Wilson <figure id="file-download" rel="32" style="background: transparent url(http://www.drewwilson.com/upload/media/tiptip-bg.jpg) no-repeat left top;"> <dd><a href="http://www.drewwilson.com/upload/data/4/tipTipv13.zip">Download Source Code</a></dd> <dt> <div><h4>TipTip</h4></div> <span id="file-version"></span> <span id="file-count"></span> </dt> </figure> <div class="donation-box"> <div class="right"> Need Help? <a href="http://drew.tenderapp.com/">Community Forums</a> </div> <div class="left"> Using this? <form action="https://www.paypal.com/cgi-bin/webscr" method="post" name="donateform"><input type="hidden" name="cmd" value="_s-xclick"><input type="hidden" name="hosted_button_id" value="HYTWPBBNJFLKC"> <a href="javascript:void(0);" onclick="document.donateform.submit()">Donate Now</a> </form> </div> </div> <p> UPDATE: Version 1.3 Released (Mar. 23, 2010). Get the latest! Details below.<br><br> </p> <p> This plugin is yet another product from my development work on <a href="http://www.builditwith.me">Build It With Me</a>. I wanted a sweet &amp; simple custom tooltip, but nothing I found seemed to be smart enough for what I needed. I needed a custom tooltip that behaved just like the browser tooltip, and that is exactly what <strong>TipTip</strong> does! <br> <strong>TipTip</strong> detects the edges of the browser window and will make sure the tooltip stays within the current window size. As a result the tooltip will adjust itself to be displayed above, below, to the left or to the right of the element with <strong>TipTip</strong> applied to it, depending on what is necessary to stay within the browser window. </p> <p> <strong>TipTip</strong> is a very lightweight and intelligent custom tooltip jQuery plugin. <br> It uses <b>ZERO</b> images and is completely customizable via CSS. It's also only 3.5kb minified! </p> <h3>Examples</h3> <p> Rollover any of the <a href="" class="tiptip" title="In computing, a hyperlink (or link) is a reference to a document that the reader can directly follow, or that is followed automatically.">links</a> in these sentences. <a href="" class="tiptip" title="A great custom tooltip plugin!">TipTip</a> can be applied to any element, not just links. Checkout the <a href="" class="tiptip" title="Seriously... check it out.">video</a> below for a more in-depth example. </p> <h3>Feature Overview Video</h3> <p> <object width="686" height="386"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id=8508658&amp;server=vimeo.com&amp;show_title=0&amp;show_byline=0&amp;show_portrait=0&amp;color=0871a8&amp;fullscreen=1" /><embed src="http://vimeo.com/moogaloop.swf?clip_id=8508658&amp;server=vimeo.com&amp;show_title=0&amp;show_byline=0&amp;show_portrait=0&amp;color=0871a8&amp;fullscreen=1" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="686" height="386"></embed></object> </p> <h3>How It Works</h3> <p> <strong>TipTip</strong> uses the <code>title</code> attribute just like the native browser tooltip does. However, the <code>title</code> will be copied and then removed from the element when using <strong>TipTip</strong> so that the browser tooltip will not show up. </p> <p> <strong>TipTip</strong> only generates one set of popup elements total, rather then generating one set of popup elements for each element with <strong>TipTip</strong> applied to it. This helps speed things up and reduces processing time. The elements generated are all <code>div</code> elements and are appended to the end of the <code>body</code> element. The structure looks like this: </p> <pre class="brush: html">&lt;div id="tiptip_holder"&gt; &lt;div id="tiptip_content"&gt; &lt;div id="tiptip_arrow"&gt; &lt;div id="tiptip_arrow_inner"&gt;&lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;</pre> <p> There are specific CSS class names added to the "tiptip_holder" <code>div</code> element when it appears depending on the orientation it appears in. Here is a list of the class names along with it's orientation: </p> <ul> <li><strong>tip_top</strong> - When the tooltip appears <b>above</b> the element.</li> <li><strong>tip_bottom</strong> - When the tooltip appears <b>below</b> the element.</li> <li><strong>tip_left</strong> - When the tooltip appears to the <b>left</b> the element.</li> <li><strong>tip_right</strong> - When the tooltip appears to the <b>right</b> the element.</li> <li><strong>tip_left_top</strong> - When the tooltip appears to the <b>left and above</b> the element.</li> <li><strong> tip_left_bottom</strong> - When the tooltip appears to the <b>left and below</b> the element.</li> <li><strong>tip_right_top</strong> - When the tooltip appears to the <b>right and above</b> the element.</li> <li><strong> tip_right_bottom</strong> - When the tooltip appears to the <b>right and below</b> the element.</li> </ul> <p> <strong>TipTip</strong> has been tested (and works) in: IE7 & IE8, Firefox, Safari, Opera, and Chrome. </p> <h3>How To Use It</h3> <p> Obviously you need to make sure you have the latest jQuery library already loaded in your page. After that it's really simple, just add the following code to your page: </p> <pre class="brush: js">$(function(){ $(".someClass").tipTip(); });</pre> <p> Below is an example of using <strong>TipTip</strong> with some options: </p> <pre class="brush: js">$(function(){ $(".someClass").tipTip({maxWidth: "auto", edgeOffset: 10}); });</pre> <p> Below is an example of what your <code>HTML</code> would look like: </p> <pre class="brush: html">&lt;p&gt; Cras sed ante. Phasellus in massa. &lt;a href="" class="someClass" title="This will show up in the TipTip popup."&gt;Curabitur dolor eros&lt;/a&gt;, gravida et, hendrerit ac, cursus non, massa. &lt;span id="foo"&gt; &lt;img src="image.jpg" class="someClass" title="A picture of the World" /&gt; &lt;/span&gt; &lt;/p&gt;</pre> <h3>Options</h3> <ul> <li><strong>activation</strong>: <b>string ("hover" by default)</b> - jQuery method TipTip is activated with. Can be set to: "hover", "focus" or "click".</li> <li><strong>keepAlive</strong>: <b>true of false (false by default)</b> - When set to true the TipTip will only fadeout when you hover over the actual TipTip and then hover off of it.</li> <li><strong>maxWidth</strong>: <b>string ("200px" by default)</b> - CSS <code>max-width</code> property for the TipTip element. This is a string so you can apply a percentage rule or 'auto'.</li> <li><strong>edgeOffset</strong>: <b>number (3 by default)</b> - Distances the TipTip popup from the element with TipTip applied to it by the number of pixels specified.</li> <li><strong>defaultPosition</strong>: <b>string ("bottom" by default)</b> - Default orientation TipTip should show up as. You can set it to: "top", "bottom", "left" or "right".</li> <li><strong>delay</strong>: <b>number (400 by default)</b> - Number of milliseconds to delay before showing the TipTip popup after you <code>mouseover</code> an element with TipTip applied to it.</li> <li><strong>fadeIn</strong>: <b>number (200 by default)</b> - Speed at which the TipTip popup will fade into view.</li> <li><strong>fadeOut</strong>: <b>number (200 by default)</b> - Speed at which the TipTip popup will fade out of view.</li> <li><strong>attribute</strong>: <b>string ("title" by default)</b> - HTML attribute that TipTip should pull it's content from.</li> <li><strong>content</strong>: <b>string (false by default)</b> - HTML or String to use as the content for TipTip. Will overwrite content from any HTML attribute.</li> <li><strong>enter</strong>: <b>callback function</b> - Custom function that is run each time you <code>mouseover</code> an element with TipTip applied to it.</li> <li><strong>exit</strong>: <b>callback function</b> - Custom function that is run each time you <code>mouseout</code> of an element with TipTip applied to it.</li> </ul> <h3>ChangeLog</h3> <strong>Version 1.3 (Mar. 23, 2010)</strong> <ul> <li>Added <code>defaultPoistion</code> option that enables you to set the default orientation TipTip should show up as.</li> <li>Added <code>attribute</code> option that enables you to set the HTML attribute that TipTip should pull it's content from.</li> <li>Added <code>content</code> option. This will be used as the content for the TipTip and will overwrite any content pulled form any HTML attribute.</li> <li>Added <code>activation</code> option enables you to specify the jQuery method TipTip is activated with: hover, focus or click. Now you can use TipTip on forms and for validation!</li> <li>Added <code>keepAlive</code> option that when set to true the TipTip will only fadeout when you hover over the actual TipTip and then hover off of it. Allowing for hyperlinks inside your TipTip content to be accessible.</li> </ul> <strong>Version 1.2 (Jan. 13, 2010)</strong> <ul> <li>Added HTML support with Tip Tip. You can now add HTML into the Title attribute (though this is not recommended if you want strictly valid code).</li> <li>Tightened up spacing margins in JS.</li> <li>Updated margins in CSS file.</li> </ul> <strong>Version 1.1 (Jan. 03, 2010)</strong> <ul> <li>Swapped dynamically added orientation CSS class names ('_left' &amp; '_right') to make better sense.</li> <li>Added in some tighter spacing for the tooltip in JS.</li> </ul> <strong>Version 1.0 (Jan. 02, 2010)</strong> <ul> <li>Initial release.</li> </ul> <h3>License</h3> <p> This <strong>TipTip</strong> jQuery plug-in is dual licensed under the <a href="http://www.opensource.org/licenses/mit-license.php">MIT</a> and <a href="http://www.gnu.org/licenses/gpl.html">GPL</a> licenses. </p> <link rel="stylesheet" type="text/css" href="http://code.drewwilson.com/css/tipTip.css" /> <script src="http://code.drewwilson.com/js/jquery.tipTip.minified.js"></script><br /> http://www.drewwilson.com/stream/code/tiptip-jquery-plugin Sun, 03 Jan 2010 04:13:28 PST AutoSuggest jQuery Plugin http://www.drewwilson.com/stream/code/autosuggest-jquery-plugin Drew Wilson <figure id="file-download" rel="31" style="background: transparent url(http://www.drewwilson.com/upload/media/autosuggest-bg.jpg) no-repeat left top;"> <dd><a href="http://www.drewwilson.com/upload/data/4/autoSuggestv14.zip">Download Source Code</a></dd> <dt> <div><h4>AutoSuggest</h4></div> <span id="file-version"></span> <span id="file-count"></span> </dt> </figure> <div class="donation-box"> <div class="right"> Need Help? <a href="http://drew.tenderapp.com/">Community Forums</a> </div> <div class="left"> Using this? <form action="https://www.paypal.com/cgi-bin/webscr" method="post" name="donateform"><input type="hidden" name="cmd" value="_s-xclick"><input type="hidden" name="hosted_button_id" value="S76F7R539467C"> <a href="javascript:void(0);" onclick="document.donateform.submit()">Donate Now</a> </form> </div> </div> <p> UPDATE: Version 1.4 Released (Mar. 23, 2010). Get the latest! Details below.<br><br> </p> <p> So I needed to add some auto-completing functionality to my project <a href="http://www.builditwith.me">Build It With Me</a>. I figured it would be easy to find a ready-made jQuery plugin to do the work for me. I was wrong. <br> Naturally I took it upon myself to solve this problem for all of you :) I created this plugin to be as general purpose as possible. As a result it ended up being <b>vastly</b> more customizable than any other jQuery auto-complete plugin. Not only that, I am using actual jQuery. For some reason all the other "jQuery" plugins don't really take advantage of the radness that is jQuery. Consequently, my plugin ended up being <b>dramatically</b> smaller in size (way less code). Just <del>7kb</del> 8.8kb minified and only 6kb packed. Have fun with it! <br> <strong>AutoSuggest</strong> is a very lightweight jQuery plugin that makes auto-completing extremely easy. </p> <h3>Example</h3> <p> Type the letter 'm' into the box below: </p> <p> <input type="text" id="example_input"> </p> <h3>Feature Overview Videos</h3> <p> Feature Update for Version 1.3<br> <object width="686" height="386"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id=9312964&server=vimeo.com&show_title=0&show_byline=0&show_portrait=0&color=0871a8&fullscreen=1" /><embed src="http://vimeo.com/moogaloop.swf?clip_id=9312964&server=vimeo.com&show_title=0&show_byline=0&show_portrait=0&color=0871a8&fullscreen=1" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="686" height="386"></embed></object><br><br> </p> <p> Initial Feature Overview for Version 1.0<br> <object width="686" height="386"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id=8464627&amp;server=vimeo.com&amp;show_title=0&amp;show_byline=0&amp;show_portrait=0&amp;color=0871a8&amp;fullscreen=1" /><embed src="http://vimeo.com/moogaloop.swf?clip_id=8464627&amp;server=vimeo.com&amp;show_title=0&amp;show_byline=0&amp;show_portrait=0&amp;color=0871a8&amp;fullscreen=1" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="686" height="386"></embed></object> </p> <h3>How It Works</h3> <p> <strong>AutoSuggest</strong> will turn any regular text <code>input</code> box into a rad auto-complete box. It will dynamically create all the HTML elements that it needs to function. You don't need to add any extra HTML to work with <strong>AutoSuggest</strong>. Also, <strong>AutoSuggest</strong> uses <b>ZERO images</b>! All styling is done 100&#37; in the included CSS file. This means it is super easy to customize the look of everything! You only need to edit the included CSS file. You can even use images if you want, just add the appropriate lines of code into the CSS file. </p> <p> As you type into the <strong>AutoSuggest</strong> <code>input</code> box, it will filter through it's Data and "suggest" matched Data items to you. You can pass in an Object of Data to <strong>AutoSuggest</strong> or you can have it call a URL as you type to get it's Data from. <strong>AutoSuggest</strong> will display the matched Data items in a selectable list, which is 100&#37; customizable. You have the option of structuring the HTML elements of that list however you want via the <code>formatList</code> callback function. You case an example of this in the second example above. </p> <p> When you type into the <code>input</code> box and the "suggestion" dropdown list appears, a few things happen: </p> <ul> <li>A <code>class</code> of "loading" is applied to the main <strong>AutoSuggest</strong> <code>ul</code> while the data is loaded. That class is then removed when all processing has finished and before the suggestion results list is made visible.</li> <li>As you hover over each suggested option in the list a <code>class</code> of "selected" is added to that item, and then removed when you <code>mouseout</code>.</li> <li>When you make a selection the item is added to the <code>input</code> box. Also there is a hidden <code>input</code> field generated for each <strong>AutoSuggest</strong> box that stores the values (comma separated) of each item you have selected. This input box will have a unique ID as well as a class name of "as-values".</li> </ul> <p> The plugin expects the Data passed into it (or gathered from the URL) to be formatted in <a href="http://json.org">JSON</a>. JSON is an extremely easy format to work with, and if you don't already... you should :) To learn more about JSON, check out my post/video, <a href="http://code.drewwilson.com/entry/an-introduction-to-json">An Introduction to JSON</a>. </p> <p> When an AJAX request is made the search string is sent over in a param named "q" by default. However you can change that name with the <b>queryParam</b> option. Here is a default example AJAX request: <b>http://www.mysite.com/your/script/?q=mick</b><br> "mick" would be the search query that was typed into the <code>input</code> box. Be sure to setup your server-side code to grab that param and send back some results. </p> <p> As of <strong>AutoSuggest</strong> version 1.4 you can now create selections by using the <b>tab</b> or <b>comma</b> keys. To do this simply type something into the box and hit the <b>tab</b> or <b>comma</b> keys. The selection is added to <strong>AutoSuggest</strong> in the exact same manner as if it were chosen from the Results dropdown. </p> <p> <strong>AutoSuggest</strong> has been tested (and works) in: IE7 & IE8, Firefox, Safari, Opera, and Chrome. </p> <h3>How To Use It</h3> <p> Obviously you need to make sure you have the latest jQuery library (at least 1.3) already loaded in your page. After that it's really simple, just add the following code to your page (make sure to wrap your code in jQuery's ready function): </p> <pre class="brush: js">$(function(){ $("input[type=text]").autoSuggest(data); });</pre> <p> The above line of code will apply <strong>AutoSuggest</strong> to all text type <code>input</code> elements on the page. Each one will be using the same set of Data. If you want to have multiple <strong>AutoSuggest</strong> fields on your page that use different sets of Data, make sure you select them separately. Like this: </p> <pre class="brush: js">$(function(){ $("div.someClass input").autoSuggest(data); $("#someID input").autoSuggest(other_data); });</pre> <p> Doing the above will allow you to pass in different options and different Data sets. </p> <p> Below is an example of using <strong>AutoSuggest</strong> with a Data Object and other various options: </p> <pre class="brush: js">var data = {items: [ {value: "21", name: "Mick Jagger"}, {value: "43", name: "Johnny Storm"}, {value: "46", name: "Richard Hatch"}, {value: "54", name: "Kelly Slater"}, {value: "55", name: "Rudy Hamilton"}, {value: "79", name: "Michael Jordan"} ]}; $("input[type=text]").autoSuggest(data.items, {selectedItemProp: "name", searchObjProps: "name"});</pre> <p> Below is an example using a URL to gather the Data Object and other various options: </p> <pre class="brush: js">$("input[type=text]").autoSuggest("http://mysite.com/path/to/script", {minChars: 2, matchCase: true});</pre> <p> <del>Please not that you <b>MUST</b> have an object property of "value" for each data item.</del> (This is now configureable with the <code>selectedValuesProp</code> option). The "value" property will be stored (comma separated) in the hidden <code>input</code> field when chosen from the "suggestion" dropdown list. You can see an example of the "value" property being set for each data item in the example above. Typically the "value" property would contain the ID of the item, so you can send a list of "chosen" IDs to your server. </p> <p> Below is an example of how to process the data sent via AJAX to your server in PHP: </p> <pre class="brush: php">&lt;? $input = $_GET["q"]; $data = array(); // query your DataBase here looking for a match to $input $query = mysql_query("SELECT * FROM my_table WHERE my_field LIKE '%$input%'"); while ($row = mysql_fetch_assoc($query)) { $json = array(); $json['value'] = $row['id']; $json['name'] = $row['username']; $json['image'] = $row['user_photo']; $data[] = $json; } header("Content-type: application/json"); echo json_encode($data); ?&gt;</pre> <h3>Options</h3> <ul> <li><strong>asHtmlID</strong>: <b>string (false by default)</b> - Enables you to specify your own custom ID that will be appended to the top level AutoSuggest UL element's ID name. Otherwise it will default to using a random ID. Example: id="CUSTOM_ID". This is also applies to the hidden input filed that holds all of the selected values. Example: id="as-values-CUSTOM_ID"</li> <li><strong>startText</strong>: <b>string ("Enter Name Here" by default)</b> - Text to display when the AutoSuggest input field is empty.</li> <li><strong>emptyText</strong>: <b>string ("No Results" by default)</b> - Text to display when their are no search results.</li> <li><strong>preFill</strong>: <b>object or string (empty object by default)</b> - Enables you to pre-fill the AutoSuggest box with selections when the page is first loaded. You can pass in a comma separated list of values (a string), or an object. When using a string, each value is used as both the display text on the selected item and for it's value. When using an object, the options <code>selectedItemProp</code> will define the object property to use for the display text and <code>selectedValuesProp</code> will define the object property to use for the value for the selected item. Note: you must setup your preFill object in that format. A preFill object can look just like the example objects laid out above.</li> <li><strong>limitText</strong>: <b>string ("No More Selections Are Allowed" by default)</b> - Text to display when the number of selections has reached it's limit.</li> <li><strong>selectedItemProp</strong>: <b>string ("value" by default)</b> - Name of object property to use as the display text for each chosen item.</li> <li><strong>selectedValuesProp</strong>: <b>string ("value" by default)</b> - Name of object property to use as the value for each chosen item. This value will be stored into the hidden input field.</li> <li><strong>searchObjProps</strong>: <b>string ("value" by default)</b> - Comma separated list of object property names. The values in these objects properties will be used as the text to perform the search on.</li> <li><strong>queryParam</strong>: <b>string ("q" by default)</b> - The name of the param that will hold the search string value in the AJAX request.</li> <li><strong>retrieveLimit</strong>: <b>number (false by default)</b> - If set to a number, it will add a '&limit=' param to the AJAX request. It also limits the number of search results allowed to be displayed in the results dropdown box.</li> <li><strong>extraParams</strong>: <b>string ("" by default)</b> - This will be added onto the end of the AJAX request URL. Make sure you add an '&amp;' before each param.</li> <li><strong>matchCase</strong>: <b>true or false (false by default)</b> - Make the search case sensitive when set to true.</li> <li><strong>minChars</strong>: <b>number (1 by default)</b> - Minimum number of characters that must be entered into the AutoSuggest input field before the search begins.</li> <li><strong>keyDelay</strong>: <b>number (400 by default)</b> - Number of milliseconds to delay after a keydown on the AutoSuggest input field and before search is started.</li> <li><strong>resultsHighlight</strong>: <b>true or false (true by default)</b> - Option to choose whether or not to highlight the matched text in each result item.</li> <li><strong>neverSubmit</strong>: <b>true or false (false by default)</b> - If set to <b>true</b> this option will never allow the 'return' key to submit the form that AutoSuggest is a part of.</li> <li><strong>selectionLimit</strong>: <b>number (false by default)</b> - Limits the number of selections that are allowed to be made to the number specified.</li> <li><strong>showResultList</strong>: <b>true or false (true by default)</b> - If set to <b>false</b>, the Results Dropdown List will never be shown at any time.</li> <li><strong>start</strong>: <b>callback function</b> - Custom function that is run only once on each AutoSuggest field when the code is first applied.</li> <li><strong>selectionClick</strong>: <b>callback function</b> - Custom function that is run when a previously chosen item is clicked. The item that is clicked is passed into this callback function as 'elem'.<br>Example: <code>selectionClick: function(elem){ elem.fadeTo("slow", 0.33); }</code></li> <li><strong>selectionAdded</strong>: <b>callback function</b> - Custom function that is run when a selection is made by choosing one from the Results dropdown, or by using the tab/comma keys to add one. The selection item is passed into this callback function as 'elem'.<br>Example: <code>selectionAdded: function(elem){ elem.fadeTo("slow", 0.33); }</code></li> <li><strong>selectionRemoved</strong>: <b>callback function</b> - Custom function that is run when a selection removed from the AutoSuggest by using the delete key or by clicking the "x" inside the selection. The selection item is passed into this callback function as 'elem'.<br>Example: <code>selectionRemoved: function(elem){ elem.fadeTo("fast", 0, function(){ elem.remove(); }); }</code></li> <li><strong>formatList</strong>: <b>callback function</b> - Custom function that is run after all the data has been retrieved and before the results are put into the suggestion results list. This is here so you can modify what &amp; how things show up in the suggestion results list.</li> <li><strong>beforeRetrieve</strong>: <b>callback function</b> - Custom function that is run right before the AJAX request is made, or before the local objected is searched. This is used to modify the search string before it is processed. So if a user entered "jim" into the AutoSuggest box, you can call this function to prepend their query with "guy_". Making the final query = "guy_jim". The search query is passed into this function. Example: <code>beforeRetrieve: function(string){ return string; }</code></li> <li><strong>retrieveComplete</strong>: <b>callback function</b> - Custom function that is run after the ajax request has completed. The data object MUST be returned if this is used. Example: <code>retrieveComplete: function(data){ return data; }</code></li> <li><strong>resultClick</strong>: <b>callback function</b> - Custom function that is run when a search result item is clicked. The data from the item that is clicked is passed into this callback function as 'data'.<br>Example: <code>resultClick: function(data){ console.log(data); }</code></li> <li><strong>resultsComplete</strong>: <b>callback function</b> - Custom function that is run when the suggestion results dropdown list is made visible. Will run after every search query.</li> </ul> <p> The <b>formatList</b> option will hand you 2 objects: </p> <ul> <li><b>data</b>: This is the data you originally passed into <strong>AutoSuggest</strong> (or retrieved via an AJAX request)</li> <li><b>elem</b>: This is the HTML element you will be formatting (the 'result' <code>li</code> item).</li> </ul> </p> In order to add extra things to the 'result' item (like an image) you will need to make sure you pass that data into <strong>AutoSuggest</strong>.<br> Below is an example of formatList in action: </p> <pre class="brush: js">formatList: function(data, elem){ var my_image = data.image; var new_elem = elem.html("add/change stuff here, put image here, etc."); return new_elem; }</pre> <p> You MUST return the HTML object. <b>formatList</b> will run on each 'result' item. </p> <h3>ChangeLog</h3> <strong>Version 1.4 (Mar. 23, 2010)</strong><br> Changes: <ul> <li>When using <code>asHtmlID</code>, the "as-input-" is no longer prepended to the beginning of the ID name. It is now just whatever you set for <code>asHtmlID</code>. This adds easy Rails compatibility.</li> </ul> New Features: <ul> <li>Added the ability to generate new selections by hitting <b>tab</b> or <b>comma</b> after entering some text into the main input field. This was a hugely requested feature, and will make AutoSuggest even more useful.</li> <li>Added <code>showResultList</code> which gives you the option to never show a results list at any time.</li> <li>Added in <code>beforeRetrieve</code> callback function (see 'Options' section above for description).</li> <li>Added in <code>selectionAdded</code> callback function (see 'Options' section above for description).</li> <li>Added in <code>selectionRemoved</code> callback function (see 'Options' section above for description).</li> </ul> <strong>Version 1.3 (Feb. 07, 2010)</strong> - (watch update video above for more details)<br> Changes: <ul> <li>Added a new feature overview video!</li> <li>The hidden input field that holds all the values (class name of 'as-values') now has it's own ID, defined by the <b>asHtmlID</b> option. Example: id="as-values-CUSTOM_ID". If the <b>asHtmlID</b> is not set, it will revert to a random number ID identifier ("as-values-RANDOM").</li> <li>Changed the option <code>selectedItem</code> to <code>selectedItemProp</code> to make better sense.</li> <li>Changed the option <code>searchObj</code> to <code>searchObjProps</code> to make better sense.</li> <li>Changed "Lucdia Grande" to "Lucida Grande" in CSS (bug fix).</li> <li>Fixed bug that would prevent you from adding certain selections at seemingly random times.</li> </ul> New Features: <ul> <li>Added <code>preFill</code> option that allows you to pre-fill the AutoSuggest box with selections when the page is loaded.</li> <li>Added <code>asHtmlID</code> option that allows you to specify your own custom ID that will be appended to the top level AutoSuggest UL element's ID name. Otherwise it will default to using a random ID.</li> <li>Added <code>emptyText</code> option to customize "No Results" text.</li> <li>Added <code>resultsHighlight</code> option to choose whether or not to highlight the matched text in the result items.</li> <li>Added <code>neverSubmit</code> option. This will never allow the 'return' key to submit the form that AutoSuggest is a part of.</li> <li>Added <code>resultClick</code> callback function. This will run a custom function when a result item has been clicked. It also hands you the data associated with that result item.</li> <li>Added <code>selectionLimit</code> option so you can limit the number of selections that are allowed to be made.</li> <li>Added <code>limitText</code> option to customize "No More Selections Are Allowed" text.</li> <li><code>retrieveLimit</code> now works with local objects and will limit the results to the number specified.</li> <li>Added <code>selectedValuesProp</code> option to specify the property you want to use to store the values into the hidden input field.</li> <li>Added in dynamic width for the results UL (description in video above).</li> <li>Added packed version of the JS (only 6kb!)</li> </ul> <strong>Version 1.2 (Jan. 05, 2010)</strong> <ul> <li>Fixed bug that didn't properly use the <code>selectedItem</code> option as the text for the results list (thanks @Niki for finding it).</li> <li><code>searchObj</code> now has it's white-space removed so it will no longer error if you do: searchObj: "value, prop, cool".</li> <li>Also, added in <code>selectionClick</code> callback function (see 'Options' section above for description).</li> </ul> <strong>Version 1.1 (Jan. 02, 2010)</strong> <ul> <li>Fixed CSS and visuals for Firefox 3.0.</li> <li>Added in the following options: queryParam, extraParams</li> </ul> <strong>Version 1.0 (Dec. 28, 2009)</strong> <ul> <li>Initial release.</li> </ul> <h3>License</h3> <p> This <strong>AutoSuggest</strong> jQuery plug-in is dual licensed under the <a href="http://www.opensource.org/licenses/mit-license.php">MIT</a> and <a href="http://www.gnu.org/licenses/gpl.html">GPL</a> licenses. </p> <link rel="stylesheet" type="text/css" href="http://code.drewwilson.com/css/autoSuggest.css" /> <script src="http://code.drewwilson.com/js/jquery.autoSuggest.minified.js"></script><br /> http://www.drewwilson.com/stream/code/autosuggest-jquery-plugin Thu, 31 Dec 2009 02:51:28 PST An Introduction to JSON http://www.drewwilson.com/stream/code/an-introduction-to-json Drew Wilson <p> It's no secret, I am a fan of JSON. I built <a href="http://www.firerift.com">Firerift</a> and <a href="http://www.titanproject.org">Titan</a>, both use JSON at the core. In my opinion Javacript is way of the future, and everyday that is becoming more and more obvious. JSON is the absolute best way to store data when using Javascript no question about it. However, I didn't always think that way. Solely because I didn't understand JSON and what it really does. <br> That is the very reason why I am writing this article. If you do not have a firm grasp on what JSON is and what it does, this one's for you. </p> <p> I spent some time putting together a little video that outlines JSON in it's most basic form. This video is not intended for those of you who already "get it". It's for those of you who have not yet discovered what JSON is. <br> My intention is just to give you a starting point with JSON and hopefully equip you enough so you understand the concepts behind JSON. From here you should be able to tackle the more "advanced" JSON tutorials and information resources out there on the web. Have fun with it! </p> <h4>The Video</h4> <p> <object width="686" height="386"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id=8463462&amp;server=vimeo.com&amp;show_title=0&amp;show_byline=0&amp;show_portrait=0&amp;color=0871a8&amp;fullscreen=1" /><embed src="http://vimeo.com/moogaloop.swf?clip_id=8463462&amp;server=vimeo.com&amp;show_title=0&amp;show_byline=0&amp;show_portrait=0&amp;color=0871a8&amp;fullscreen=1" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="686" height="386"></embed></object> </p> <h4>What is JSON</h4> <ul> <li>JSON = JavaScript Object Notation</li> <li>Object that contains Data</li> <li>Better than XML for use with Javascript (cause it is not a markup language and it is lightweight)</li> <li>Easier and quicker to setup than XML on the server-side</li> </ul> <h4>JSON vs. Arrays</h4> <ul> <li>JSON wins</li> <li>Not a good comparison since JSON can have arrays inside of it</li> <li>JSON can store nested objects and arrays, it is much more powerful than an array</li> </ul> <h4>What does JSON look like?</h4> <p> Declaring an empty JSON object in Javascript: </p> <pre class="brush: js">var data = {};</pre> <p> A basic JSON object: </p> <pre class="brush: js">var data = {kind: "Human", water: "Clear Blue", help: false};</pre> <p> A complex JSON object: </p> <pre class="brush: js">var bar = {prop: [{key: "Value", x: "yes"},{alien: false, skin: "Green Tone"}], sand: true}; var data = {kind: "Human", water: "Clear Blue", help: false, extra: bar};</pre> <h4>How do I set it up a JSON in PHP?</h4> <p> A simple example that outputs a static JSON object: </p> <pre class="brush: php">&lt;? header("Content-type: application/json"); echo '{"items: [ key: "value", another_key: { k: "val", x: "yes" } ]}'; ?&gt;</pre> <p> Convert PHP arrays into JSON: </p> <pre class="brush: php">&lt;? header("Content-type: application/json"); $my_object = '{"items: [ key: "value", another_key: { k: "val", x: "yes" } ]}'; $count = 2; echo json_encode(array("items" => $my_object, "count" => $count)); ?&gt;</pre> <p> Get data from a database and turn it into JSON with PHP: </p> <pre class="brush: php">&lt;? $input = $_GET["q"]; $data = array(); // query your DataBase here looking for a match to $input $query = mysql_query("SELECT * FROM my_table WHERE my_field LIKE '%$input%'"); while ($row = mysql_fetch_assoc($query)) { $json = array(); $json['value'] = $row['id']; $json['name'] = $row['username']; $json['image'] = $row['user_photo']; $data[] = $json; } header("Content-type: application/json"); echo json_encode($data); ?&gt;</pre> <h4>Using JSON with jQuery</h4> <p> Query the server and alert some JSON data: </p> <pre class="brush: js">$.getJSON("http://mysite.com/path/to/script", function(data){ var items = data.items[0]; alert(items.key); });</pre><br /> http://www.drewwilson.com/stream/code/an-introduction-to-json Thu, 31 Dec 2009 01:20:48 PST imgCenter jQuery Plugin http://www.drewwilson.com/stream/code/imgcenter-jquery-plugin Drew Wilson <figure id="file-download" rel="27" style="background: transparent url(http://www.drewwilson.com/upload/media/imgcenter-bg.jpg) no-repeat left top;"> <dd><a href="http://www.drewwilson.com/upload/data/4/imgCenterv10.zip">Download Source Code</a></dd> <dt> <div><h4>imgCenter</h4></div> <span id="file-version"></span> <span id="file-count"></span> </dt> </figure> <div class="donation-box"> <div class="right"> Need Help? <a href="http://drew.tenderapp.com/">Community Forums</a> </div> <div class="left"> Using this? <form action="https://www.paypal.com/cgi-bin/webscr" method="post" name="donateform"><input type="hidden" name="cmd" value="_s-xclick"><input type="hidden" name="hosted_button_id" value="3XFNZD7TGRBUY"> <a href="javascript:void(0);" onclick="document.donateform.submit()">Donate Now</a> </form> </div> </div> <p> Ever come across a situation where you need your images on a page to be perfectly centered in their HTML container so they all show up all tidy and uniform on the page?? Ya, me too. So I built an Image Centering plugin for jQuery. It has proven to be more useful than I would have ever thought. I love this plugin. What's better is I am releasing it free to you guys! <br> <strong>ImgCenter</strong> is a jQuery plugin that makes centering images a breeze. Download it now! </p> <h3>Examples</h3> <p class="overflow"> The images below are all contained inside of their own <code>SPAN</code> element.<br> <span class="centered"><img src="http://www.drewwilson.com/upload/media/frp_cIMG7080.jpg" alt="cIMG7080.jpg" class="center"></span> <span class="centered"><img src="http://www.drewwilson.com/upload/media/cIMG7708.jpg" alt="cIMG7708.jpg" class="center"></span> <span class="centered"><img src="http://www.drewwilson.com/upload/media/cIMG7272.jpg" alt="cIMG7272.jpg" class="center"></span> </p> <p> The images above are not square. They are being dynamically centered into the parent element, which is set to <code>200px</code> by <code>200px</code>, giving them the appearance of being a cropped version of the original image. And yes, I shot those photos :) </p> <h3>How It Works</h3> <p> <strong>ImgCenter</strong> will first hide each image it is applied to from the screen before doing anything else. It will then show the image again once it has finished doing it's sizing and centering. This eliminates any annoying flicker you might see while the image is resized and centered by javascript. It then centers the images based on it's parent element. So if you don't wrap your image in an element at all, it will center it based on the <code>BODY</code> element, which would not be good. So make sure you have a container element for the image to be centered against.<br> By default <strong>imgCenter</strong> scales each image up or down to fit inside it's parent element. It will also wait to make sure each image is loaded before doing any re-sizing. </p> <h3>How To Use It</h3> <p> Obviously you need to make sure you have the latest jQuery library already loaded in your page. After that it's really simple, just add the following code to your page: </p> <pre class="brush: js">$("img").imgCenter();</pre> <p> If you want this thing to work best cross-browser, make sure you put that code into the window.load event. Like this: </p> <pre class="brush: js">$(window).load(function(){ $("img").imgCenter(); });</pre> <p> Make sure your selector points to an actual image. If you wanted to center only a select group of images on your page, rather than all the images on your page, just do this: </p> <pre class="brush: js">$(window).load(function(){ $("#someElement div.someClass img").imgCenter(); });</pre> <p> You will need to make sure you have a <code>width</code> and <code>height</code> set on your parent element that contains the image, so it has values to do the re-sizing against. </p> <h3>Options</h3> <p> Yep, there are options as well: </p> <ul> <li><strong>parentSteps</strong>: <b>number (0 by default)</b> - Climbs up the number of parents elements specified starting from the current element, then applies the centering code based on that element.</li> <li><strong>scaleToFit</strong>: <b>true or false (true by default)</b> - Will size up (or down) the image to fill out the parent element if the image is too small while keeping proper centering.</li> <li><strong>centerVertical</strong>: <b>true or false (true by default)</b> - Will vertically center the image if set to true.</li> <li><strong>complete</strong>: <b>callback function</b> - Custom function that is run after each image has centering applied to it.</li> <li><strong>start</strong>: <b>callback function</b> - Custom function that is run only once, before image centering begins.</li> <li><strong>end</strong>: <b>callback function</b> - Custom function that is run only once, after image centering finishes on all images.</li> </ul> <p> Here is how to implement the plugin with some options: </p> <pre class="brush: js">$(window).load(function(){ $("#someElement div.someClass img").imgCenter({ parentSteps: 3, end: function(){ alert("All Done!"); } }); });</pre> <h3>License</h3> <p> This <strong>ImgCenter</strong> jQuery plug-in is dual licensed under the <a href="http://www.opensource.org/licenses/mit-license.php">MIT</a> and <a href="http://www.gnu.org/licenses/gpl.html">GPL</a> licenses. </p><br /> http://www.drewwilson.com/stream/code/imgcenter-jquery-plugin Tue, 01 Dec 2009 21:56:04 PST