Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Move JS out of template

  • Loading branch information...
commit 1139269883e0f241e28337079b8ab223252c7354 1 parent 5bdb648
@dannydb dannydb authored
Showing with 134 additions and 127 deletions.
  1. +125 −0 js/topic-filtering.js
  2. +9 −127 topic-index.php
View
125 js/topic-filtering.js
@@ -0,0 +1,125 @@
+// qs_score - Quicksilver Score
+//
+// A port of the Quicksilver string ranking algorithm
+//
+// "hello world".score("axl") //=> 0.0
+// "hello world".score("ow") //=> 0.6
+// "hello world".score("hello world") //=> 1.0
+//
+// The MIT License
+//
+// Copyright (c) 2008 Lachie Cox
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+
+String.prototype.score = function(abbreviation,offset) {
+ offset = offset || 0 // TODO: I think this is unused... remove
+
+ if(abbreviation.length == 0) return 0.9
+ if(abbreviation.length > this.length) return 0.0
+
+ for (var i = abbreviation.length; i > 0; i--) {
+ var sub_abbreviation = abbreviation.substring(0,i)
+ var index = this.indexOf(sub_abbreviation)
+
+
+ if(index < 0) continue;
+ if(index + abbreviation.length > this.length + offset) continue;
+
+ var next_string = this.substring(index+sub_abbreviation.length)
+ var next_abbreviation = null
+
+ if(i >= abbreviation.length)
+ next_abbreviation = ''
+ else
+ next_abbreviation = abbreviation.substring(i)
+
+ var remaining_score = next_string.score(next_abbreviation,offset+index)
+
+ if (remaining_score > 0) {
+ var score = this.length-next_string.length;
+
+ if(index != 0) {
+ var j = 0;
+
+ var c = this.charCodeAt(index-1)
+ if(c==32 || c == 9) {
+ for(var j=(index-2); j >= 0; j--) {
+ c = this.charCodeAt(j)
+ score -= ((c == 32 || c == 9) ? 1 : 0.15)
+ }
+ } else {
+ score -= index
+ }
+ }
+
+ score += remaining_score * next_string.length
+ score /= this.length;
+ return score
+ }
+ }
+ return 0.0
+}
+
+// John Resig's jQuery LiveSearch
+// http://ejohn.org/blog/jquery-livesearch/
+
+jQuery.fn.liveUpdate = function(list){
+ list = jQuery(list);
+ if ( list.length ) {
+ var rows = list.children('tr'),
+ cache = rows.map(function(){
+
+ return jQuery(this).first('a').text().toLowerCase();
+ });
+
+ this
+ .keyup(filter).keyup()
+ .parents('form').submit(function(){
+ return false;
+ });
+ }
+
+ return this;
+
+ function filter(){
+ var term = jQuery.trim( jQuery(this).val().toLowerCase() ), scores = [];
+
+ if ( !term ) {
+ rows.show();
+ } else {
+ rows.hide();
+
+ cache.each(function(i){
+ var score = this.score(term);
+ if (score > 0) { scores.push([score, i]); }
+ });
+
+ jQuery.each(scores.sort(function(a, b){return b[0] - a[0];}), function(){
+ jQuery(rows[ this[1] ]).show();
+ });
+ }
+ }
+};
+
+
+// Initialize filter
+jQuery(document).ready(function() {
+ jQuery('#term').liveUpdate('#tags tbody').focus();
+});
View
136 topic-index.php
@@ -4,6 +4,14 @@
*/
?>
+<?php
+function sw_topic_filtering() {
+ $js = get_bloginfo('stylesheet_directory') . "/js/topic-filtering.js";
+ wp_enqueue_script('topicfilter', $js);
+}
+add_action('wp_enqueue_scripts', 'sw_topic_filtering');
+?>
+
<?php get_header(); ?>
<article class="grid_8">
@@ -26,133 +34,6 @@
<?php endwhile; ?>
</div><!-- #content -->
-<!--
-<nav class="alpha-nav clearfix">
- <ul>
- <li><a href="#a">A</a></li>
- <li><a href="#b">B</a></li>
- <li><a href="#c">C</a></li>
- <li><a href="#d">D</a></li>
- <li><a href="#e">E</a></li>
- <li><a href="#f">F</a></li>
- <li><a href="#g">G</a></li>
- <li><a href="#h">H</a></li>
- <li><a href="#i">I</a></li>
- <li><a href="#j">J</a></li>
- <li><a href="#k">K</a></li>
- <li><a href="#l">L</a></li>
- <li><a href="#m">M</a></li>
- <li><a href="#n">N</a></li>
- <li><a href="#o">O</a></li>
- <li><a href="#p">P</a></li>
- <li><a href="#q">Q</a></li>
- <li><a href="#r">R</a></li>
- <li><a href="#s">S</a></li>
- <li><a href="#t">T</a></li>
- <li><a href="#u">U</a></li>
- <li><a href="#v">V</a></li>
- <li><a href="#w">W</a></li>
- <li><a href="#x">X</a></li>
- <li><a href="#y">Y</a></li>
- <li><a href="#z">Z</a></li>
- </ul>
-</nav>
--->
-<!-- /.alpha-nav -->
-
-<script type="text/javascript" charset="utf-8">
-String.prototype.score = function(abbreviation,offset) {
- offset = offset || 0 // TODO: I think this is unused... remove
-
- if(abbreviation.length == 0) return 0.9
- if(abbreviation.length > this.length) return 0.0
-
- for (var i = abbreviation.length; i > 0; i--) {
- var sub_abbreviation = abbreviation.substring(0,i)
- var index = this.indexOf(sub_abbreviation)
-
-
- if(index < 0) continue;
- if(index + abbreviation.length > this.length + offset) continue;
-
- var next_string = this.substring(index+sub_abbreviation.length)
- var next_abbreviation = null
-
- if(i >= abbreviation.length)
- next_abbreviation = ''
- else
- next_abbreviation = abbreviation.substring(i)
-
- var remaining_score = next_string.score(next_abbreviation,offset+index)
-
- if (remaining_score > 0) {
- var score = this.length-next_string.length;
-
- if(index != 0) {
- var j = 0;
-
- var c = this.charCodeAt(index-1)
- if(c==32 || c == 9) {
- for(var j=(index-2); j >= 0; j--) {
- c = this.charCodeAt(j)
- score -= ((c == 32 || c == 9) ? 1 : 0.15)
- }
- } else {
- score -= index
- }
- }
-
- score += remaining_score * next_string.length
- score /= this.length;
- return score
- }
- }
- return 0.0
-}
-jQuery.fn.liveUpdate = function(list){
- list = jQuery(list);
- if ( list.length ) {
- var rows = list.children('tr'),
- cache = rows.map(function(){
-
- return jQuery(this).first('a').text().toLowerCase();
- });
-
- this
- .keyup(filter).keyup()
- .parents('form').submit(function(){
- return false;
- });
- }
-
- return this;
-
- function filter(){
- var term = jQuery.trim( jQuery(this).val().toLowerCase() ), scores = [];
-
- if ( !term ) {
- rows.show();
- } else {
- rows.hide();
-
- cache.each(function(i){
- var score = this.score(term);
- if (score > 0) { scores.push([score, i]); }
- });
-
- jQuery.each(scores.sort(function(a, b){return b[0] - a[0];}), function(){
- jQuery(rows[ this[1] ]).show();
- });
- }
- }
-};
-
-
-
- jQuery(document).ready(function() {
- jQuery('#term').liveUpdate('#tags tbody').focus();
- });
-</script>
<form method="get" autocomplete="off">
<div>
@@ -200,4 +81,5 @@ function filter(){
<?php get_sidebar(); ?>
</aside>
<!-- /.grid_4 -->
+
<?php get_footer(); ?>
Please sign in to comment.
Something went wrong with that request. Please try again.