{"id":36,"date":"2008-11-06T10:11:12","date_gmt":"2008-11-06T17:11:12","guid":{"rendered":"http:\/\/kellytehuna.com\/blog\/?page_id=36"},"modified":"2009-12-07T16:22:22","modified_gmt":"2009-12-07T20:22:22","slug":"javascript","status":"publish","type":"page","link":"http:\/\/kellytehuna.com\/blog\/code\/javascript\/","title":{"rendered":"Javascript"},"content":{"rendered":"<h2>A simple Facebook Tabs class<\/h2>\n<p>This is a simple class I wrote to turn an underlying code structure in to a simple Javascript driven tab system.\u00a0 There are still a few small bugs that need to be ironed out, but they aren&#8217;t huge and certainly don&#8217;t render the system unusable. <a title=\"Facebook JS Tabs code\" href=\"http:\/\/kellytehuna.com\/blog\/wp-content\/uploads\/2008\/11\/fb-tabs.js\">Download the code<\/a>.<\/p>\n<pre lang=\"javascript\" line=\"1\">\/\/ Class constructor function\r\nfunction Tabs(target, active_tab, active_css_class){\r\n    this.tabs = {};\r\n    this.active = null;\r\n    this.css_class = active_css_class || 'active';\r\n\r\n    \/\/ locally cache a few required values\r\n    var element = document.getElementById(target),\r\n\t    links   = element.getElementsByTagName('a'),\r\n\t    temp    = null,\r\n\t    self    = this;\r\n\r\n    \/\/ a simple function to handle any click events on the \"handles\" for the tabs\r\n    var clickHandler = function(e){\r\n    \te.stopPropagation();\r\n    \te.preventDefault();\r\n        self.activate(e);\r\n    }\r\n\r\n    for (var i = 0, len = links.length; i &gt; len; i++) {\r\n        temp = links[i].getHref().split('#')[1];\r\n\r\n\t\tthis.tabs[temp] = {'parent' : links[i].getParentNode(),\r\n\t\t\t\t   'source' : links[i],\r\n\t\t\t           'target' : document.getElementById(temp)};\r\n\r\n        this.tabs[temp].target.setStyle('display','none');\r\n        this.tabs[temp].source.addEventListener('click',clickHandler);\r\n    }\r\n\r\n    this._activate(active_tab);\r\n}\r\n\r\n\/\/ The actual class definition\r\nTabs.prototype = {\r\n    activate : function(e){\r\n    \tvar elem = e.target;\r\n\r\n    \twhile(elem.getTagName().toLowerCase()!='a') {\r\n    \t\telem = elem.getParentNode();\r\n\r\n    \t\tif(!elem) return;  \/\/ this should never happen\r\n\t}\r\n\r\n        elem = e.target.getHref().split('#')[1];\r\n\r\n        this._activate(elem);\r\n\r\n        return false;\r\n    },\r\n\r\n    _activate : function(elem){\r\n        if (this.active) {\r\n            this.active.target.setStyle('display', 'none');\r\n            this.active.parent.removeClassName(this.css_class);\r\n        }\r\n\r\n        this.active = this.tabs[elem];\r\n\r\n        this.active.target.setStyle('display', 'block');\r\n        this.active.parent.addClassName(this.css_class);\r\n    }\r\n}<\/pre>\n<p>As you can see, this class is very simple and very concise.\u00a0 The class assumes that you&#8217;ve already created the necessary markup and simply traverses the tabset for all named links (ie. anything that has a # in the URL) and collects them.\u00a0 It then, hides all elements that have ID&#8217;s matching the named links, then shows the one element with the ID specified as the second argument of the class constructor.<\/p>\n<p>This class <span style=\"text-decoration: underline;\">MUST<\/span> be used within a Facebook application, or it will fail due to missing DOM methods, like setStyle, addClassName, removeClassName, etc., etc.<\/p>\n<h2>Kohana Profiler Extender (JQuery)<\/h2>\n<p>While working with <a title=\"Kohana - The swift PHP Framework\" href=\"http:\/\/kohanaphp.com\">Kohana PHP Framework<\/a>, you can have the framework output\u00a0 some profiler information along with the usual output, just like its cousin, <a title=\"CodeIgniter\" href=\"http:\/\/codeigniter.com\">CodeIgniter<\/a>. Unfortunately, there is often a lot of information that gets output which can make the output rather long. Personally, I don&#8217;t like this behavior very much, so I decided to write a simple little JQuery plugin that turns the standard output into a tabbed layout. The benefit is that much less screen real estate is required you can view each item in turn, as well as displaying all available information, if you want to do so.<a href=\"http:\/\/kellytehuna.com\/blog\/wp-content\/uploads\/2008\/11\/jquery.kohanaprofiler.zip\"> Download the source<\/a> (ZIP &#8211; 1.6KB).<\/p>\n<h3>The Javascript<\/h3>\n<pre lang=\"javascript\" line=\"1\">(function($) {\r\n\t$.fn.kohanaProfiler = function() {\r\n\t\tvar $profiler = $('#kohana-profiler'),\r\n\t\t\t$tabs = $('<ul id=\"kp-profile-tabs\"\/>'),\r\n\t\t\tactive = null;\r\n\r\n\t\t$profiler.find('.kp-table').wrap('<div class=\"kp-table-wrapper\"\/>');\r\n\r\n\t\t$wrapper = $('.kp-table-wrapper');\r\n\r\n\t\t$wrapper.each(function(idx, item) {\r\n\t\t\tvar $this = $(this),\r\n\t\t\t\ttext = $this.find('.kp-title td:first-child').text(),\r\n\t\t\t\t$h2 = $('<h2>' + text + '<\/h2>')\r\n\t\t\t\t\t\t.attr( 'style', $this.find('.kp-title').attr('style') );\r\n\r\n\t\t\t$tabs.append('<li><a href=\"#tab' + (idx+1) + '\">' + text + '<\/a><\/li>');\r\n\r\n\t\t\t$this.attr('id', 'tab' + (idx+1))\r\n\t\t\t\t.prepend($h2)\r\n\t\t\t\t.find('.kp-title').attr('style', '');\r\n\t\t});\r\n\r\n\t\t$profiler.prepend($tabs.append('<li><a href=\"#all\">Show All<\/a><\/li>'));\r\n\r\n\t\t$tabs.find('a').click(function(e) {\r\n\t\t\te.preventDefault();\r\n\t\t\te.stopPropagation();\r\n\r\n\t\t\tvar hash = this.hash,\r\n\t\t\t\t$parent = $(this).parent(),\r\n\t\t\t\tfilter = function() {\r\n\t\t\t\t\treturn $(this)[0] !== $(hash)[0];\r\n\t\t\t\t}\r\n\r\n\t\t\tif (hash !== '#all') {\r\n\t\t\t\t$('.kp-table-wrapper:visible').filter(filter).slideUp('fast', function(){\r\n\t\t\t\t\t$(hash + ':hidden').slideDown('slow');\r\n\t\t\t\t});\r\n\r\n\t\t\t\t$('#kp-profile-tabs li').css({ 'background-color': '#eee' });\r\n\t\t\t\t$parent.css({ 'background-color': $(hash).find('h2').css('background-color') });\r\n\t\t\t}\r\n\t\t\telse {\r\n\t\t\t\t$('.kp-table-wrapper').filter(filter).slideDown('fast');\r\n\t\t\t\t$('#kp-profile-tabs li').css({ 'background-color': '#eee' });\r\n\t\t\t\t$parent.css({ 'background-color': '#ccc' });\r\n\t\t\t}\r\n\r\n\t\t\t$('#kp-profile-tabs li').removeClass('active');\r\n\t\t\t$parent.addClass('active');\r\n\r\n\t\t\tactive = hash;\r\n\r\n\t\t\treturn false;\r\n\t\t})\r\n\t\t.hover(function() {\r\n\t\t\tif (!$(this).parent().is('.active')) {\r\n\t\t\t\t$(this).parent().css({ 'background-color': $(this.hash).find('h2').css('background-color') }).addClass('hover');\r\n\t\t\t}\r\n\t\t},\r\n\t\tfunction() {\r\n\t\t\tif (!$(this).parent().is('.active')) {\r\n\t\t\t\t$(this).parent().css({ 'background-color': '#eee' });\r\n\t\t\t}\r\n\r\n\t\t\t$(this).parent().removeClass('hover');\r\n\t\t});\r\n\r\n\t\t$('#kp-profile-tabs li:first-of-type a').click();\r\n\t}\r\n})(jQuery); \/\/ self-invocation of the declaration function\r\n\r\n$(document).ready(function() {\r\n\t$(this).kohanaProfiler();\r\n});\r\n<\/pre>\n<h3>The CSS<\/h3>\n<pre lang=\"css\" line=\"1\">body &gt; #kohana-profiler { width: 960px; margin-left: auto; margin-right: auto; }\r\n\r\nbody &gt; #kohana-profiler .kp-table { width: 100%; margin: 0 0 1em; }\r\nbody &gt; #kohana-profiler .kp-table td { font-size: 1.3em; padding: .5em 1em; border: 1px solid #999; }\r\n\r\nbody &gt; #kohana-profiler .kp-table .kp-title td { font-weight: bold; }\r\nbody &gt; #kohana-profiler .kp-table tr:nth-child(2n-1) td { background-color: #eee; }\r\nbody &gt; #kohana-profiler .kp-table tr.kp-totalrow td { font-weight: bold; background-color: transparent; }\r\nbody &gt; #kohana-profiler .kp-table .kp-totalrow td:first-of-type { border: none; text-align: right; } \r\n\r\nbody &gt; #kohana-profiler .kp-meta { margin: 1em 0; }\r\n\r\nbody &gt; #kohana-profiler .kp-table-wrapper { margin: 1em 0; }\r\nbody &gt; #kohana-profiler .kp-table-wrapper h2 { padding: .5em 1em; margin: 0 0 0 -1px; border: 1px solid #999; }\r\n\r\nbody &gt; #kohana-profiler #kp-profile-tabs li { display: inline-block; padding: .5em 1em; margin: 0 .25em 0 0; border: 1px solid #999; font-size: 1.3em; background-color: #eee; }\r\nbody &gt; #kohana-profiler #kp-profile-tabs a { color: #999; text-decoration: none; }\r\n\r\nbody &gt; #kohana-profiler #kp-profile-tabs .active, body &gt; #kohana-profiler #kp-profile-tabs .hover { border: 1px solid #333; }\r\nbody &gt; #kohana-profiler #kp-profile-tabs .active a, body &gt; #kohana-profiler #kp-profile-tabs .hover a  { color: #333; text-decoration: underline; }<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>A simple Facebook Tabs class This is a simple class I wrote to turn an underlying code structure in to a simple Javascript driven tab system.\u00a0 There are still a few small bugs that need to be ironed out, but they aren&#8217;t huge and certainly don&#8217;t render the system unusable. Download the code. \/\/ Class [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":34,"menu_order":0,"comment_status":"open","ping_status":"open","template":"","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"jetpack_post_was_ever_published":false,"footnotes":""},"class_list":["post-36","page","type-page","status-publish","hentry"],"aioseo_notices":[],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/P2FJaA-A","jetpack_likes_enabled":true,"_links":{"self":[{"href":"http:\/\/kellytehuna.com\/blog\/wp-json\/wp\/v2\/pages\/36"}],"collection":[{"href":"http:\/\/kellytehuna.com\/blog\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"http:\/\/kellytehuna.com\/blog\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"http:\/\/kellytehuna.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/kellytehuna.com\/blog\/wp-json\/wp\/v2\/comments?post=36"}],"version-history":[{"count":16,"href":"http:\/\/kellytehuna.com\/blog\/wp-json\/wp\/v2\/pages\/36\/revisions"}],"predecessor-version":[{"id":38,"href":"http:\/\/kellytehuna.com\/blog\/wp-json\/wp\/v2\/pages\/36\/revisions\/38"}],"up":[{"embeddable":true,"href":"http:\/\/kellytehuna.com\/blog\/wp-json\/wp\/v2\/pages\/34"}],"wp:attachment":[{"href":"http:\/\/kellytehuna.com\/blog\/wp-json\/wp\/v2\/media?parent=36"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}