From 6fa5f72b10055bb511e05b56b9cf4cccc74c7a70 Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Tue, 12 Jun 2012 10:59:07 +0800 Subject: [PATCH] move google search preview code from main.js to google_search_preview.js --- .jshintrc | 4 +- config.default.js | 1 + public/javascripts/google_search_preview.js | 44 ++++++++ public/javascripts/main.js | 115 ++++++-------------- views/layout.html | 9 +- 5 files changed, 90 insertions(+), 83 deletions(-) create mode 100644 public/javascripts/google_search_preview.js diff --git a/.jshintrc b/.jshintrc index 5e076bb..698262e 100644 --- a/.jshintrc +++ b/.jshintrc @@ -10,9 +10,11 @@ "describe", "before", "after", - "should" + "should", + "$" ], + "browser": true, "node" : true, "es5": true, "bitwise": true, diff --git a/config.default.js b/config.default.js index 6b168c5..c743184 100644 --- a/config.default.js +++ b/config.default.js @@ -18,6 +18,7 @@ exports.config = { [ '/about', '关于' ], ], site_static_host: '', // 静态文件存储域名 + site_enable_search_preview: false, // 开启google search preview db: 'mongodb://127.0.0.1/node_club_dev', session_secret: 'node_club', diff --git a/public/javascripts/google_search_preview.js b/public/javascripts/google_search_preview.js new file mode 100644 index 0000000..3b46a4e --- /dev/null +++ b/public/javascripts/google_search_preview.js @@ -0,0 +1,44 @@ +$(function () { + var id = -1; + var start = function () { + id = setInterval(check, 1000); + }; + var old = ''; + var check = function () { + var q = $in.val().trim(); + if (q === '' || old === q) { + return; + } + old = q; + var url = 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=site:cnodejs.org+' + q + '&callback=?'; + $.getJSON(url, function (d) { + if (!(d.responseData && Array.isArray(d.responseData.results))) { + return; + } + var list = d.responseData.results; + showList(list); + }); + }; + var stop = function () { + clearInterval(id); + $list.slideUp(500); + }; + var $in = $('input#q'); + $in.focusin(start).focusout(stop); + $in.after('') + .after(''); + var $list = $('#__quick_search_list'); + var showList = function (list) { + var html = ''; + list.forEach(function (line) { + html += '
' + line.title + '' + + '' + line.content + '
'; + }); + if (!html) { + html = '暂时没有相关结果。'; + } + var o1 = $in.offset(); + var o2 = {top: o1.top + $in.height() + 10, left: o1.left}; + $list.offset(o2).html(html).show(); + }; +}); \ No newline at end of file diff --git a/public/javascripts/main.js b/public/javascripts/main.js index e447842..bf792d0 100644 --- a/public/javascripts/main.js +++ b/public/javascripts/main.js @@ -1,82 +1,39 @@ -$(document).ready(function() { - $('#search_form').submit(function(e) { - //e.preventDefault(); - search(); - }); - - function search() { - var q = document.getElementById('q'); - if (q.value != '') { - /* - var hostname = window.location.hostname; - var url = 'http://www.google.com/search?q=site:' + hostname + '%20'; - window.open(url + q.value, '_blank'); - */ - return true; - } else { - return false; - } - } +$(document).ready(function () { + $('#search_form').submit(function (e) { + //e.preventDefault(); + search(); + }); - // 搜索预览 - (function () { - var id = -1; - var start = function () { - id = setInterval(check, 1000); - }; - var old = ''; - var check = function () { - var q = $in.val().trim(); - if (q === '' || old === q) - return; - old = q; console.log(q); - $.getJSON('http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=site:cnodejs.org+' + q + '&callback=?', function (d) { - if (!(d.responseData && Array.isArray(d.responseData.results))) - return; - var list = d.responseData.results - console.log(list); - showList(list); - }); - }; - var stop = function () { - clearInterval(id); - $list.slideUp(500); - }; - var $in = $('input#q'); - $in.focusin(start).focusout(stop); - $in.after('') - .after(''); - var $list = $('#__quick_search_list'); - var showList = function (list) { - var html = ''; - list.forEach(function (line) { - html += '
' + line.title + '' - + '' + line.content + '
'; - }); - if (html === '') - html = '暂时没有相关结果。'; - var o1 = $in.offset(); - var o2 = {top: o1.top + $in.height() + 10, left: o1.left}; - $list.offset(o2).html(html).show(); - }; - })(); + function search() { + var q = document.getElementById('q'); + if (q.value) { + /* + var hostname = window.location.hostname; + var url = 'http://www.google.com/search?q=site:' + hostname + '%20'; + window.open(url + q.value, '_blank'); + */ + return true; + } else { + return false; + } + } + + var $wrapper = $('#wrapper'); + var $backtotop = $('#backtotop'); + var top = $(window).height() - $backtotop.height() - 90; + $backtotop.css({ top: top, right: 100 }); + $backtotop.click(function () { + $('html,body').animate({ scrollTop: 0 }); + return false; + }); + $(window).scroll(function () { + var windowHeight = $(window).scrollTop(); + if (windowHeight > 200) { + $backtotop.fadeIn(); + } else { + $backtotop.fadeOut(); + } + }); - var $wrapper = $('#wrapper'); - var $backtotop = $('#backtotop'); - var top = $(window).height() - $backtotop.height() - 90; - $backtotop.css({ top: top, right: 100 }); - $backtotop.click(function() { - $('html,body').animate({ scrollTop: 0 }); - return false; - }); - $(window).scroll(function() { - var windowHeight = $(window).scrollTop(); - if (windowHeight > 200) { - $backtotop.fadeIn(); - } else { - $backtotop.fadeOut(); - } - }); - - $('.topic_content a,.reply_content a').attr('target', '_blank'); + $('.topic_content a,.reply_content a').attr('target', '_blank'); }); diff --git a/views/layout.html b/views/layout.html index 2b0d34a..209a8d4 100644 --- a/views/layout.html +++ b/views/layout.html @@ -15,9 +15,12 @@ - - - + + + + <% if (config.site_enable_search_preview) { %> + + <% } %> <% if (locals.topic) { %> <%= topic.title %> - <%= config.name %> <% } else { %>