Skip to content

Commit

Permalink
Mod: Move javascript from application.html.erb to asks.js
Browse files Browse the repository at this point in the history
  • Loading branch information
huacnlee committed Apr 5, 2011
1 parent 419ef2e commit a838df7
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 86 deletions.
86 changes: 0 additions & 86 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -14,92 +14,6 @@
<%= csrf_meta_tag %>
<%= javascript_include_tag "jquery.min","rails","facebox","jquery.jdialog","jquery.qeditor","jcaches","application","asks", :cache => "cached_application" %>
<%= yield :scripts %>
<script type="text/javascript">
function addAsk(){
var txtTitle = $("#hidden_new_ask textarea:nth-of-type(1)");
ask_search_text = $("#add_ask input").val();
if(ask_search_text.length == 0){
$("#add_ask input").focus();
$("#add_ask input").click();
return false;
}
txtTitle.text(ask_search_text);
$.facebox({ div : "#hidden_new_ask", overlay : false });
txtTitle.focus();
return false;
}

var searchCache = new jCaches(40,false);
var lastSearchText = null;
var searchTimer = null;
function showSearchComplete(el,type){
clearTimeout(searchTimer);
html = "";
if(type == "click" && $(el).val() == ""){
html = $(el).attr("placeholder");
searchCallback(el,html);
}
else{
searchTimer = setTimeout(function(){
t = $(el).val().trim();
if(t == lastSearchText){
return false;
}
lastSearchText = t;
cachedItems = searchCache.get(t);
if(cachedItems == null){
$.ajax({
url : "/search.json",
data : { w : t },
dataType : "json",
success : function(res){
searchCache.add(t,res);
searchAjaxCallback(el,res);
}
});
}
else{
searchAjaxCallback(el,cachedItems);
}
},200);
}

return false;
}

function searchAjaxCallback(el,res){
html = '<ul class="complete">';
App.loading(false);
if(res.length > 0){
for(var i=0;i<res.length;i++){
html += '<li onclick="location.href = $(\'a\',this).attr(\'href\');">';
if(res[i].topics.length > 0){
html += '<span class="cate">'+res[i].topics[0]+'</span>';
}
item_title = res[i].title;
html += '<a href="/asks/'+res[i]._id+'">'+item_title+'</a>';
html += '</li>';
}
html += '<li class="more" onclick="location.href=\'/search?w='+t+'\';">关于“'+t+'”更多搜索结果...</li>';
}
else{
html += '<li>没有找到关于“'+t+'”的结果: <a href="#" onclick="return addAsk();">添加这个问题</a></li>';
}
html += "</ul>";
searchCallback(el,html);
}

function searchCallback(el, html){
el_width = $(el).width();
$(el).jDialog({
content : html,
width : el_width + 250,
title_visiable : false,
top_offset : 1,
left_offset : -1
});
}
</script>
</head>
<body>
<div id="header">
Expand Down
86 changes: 86 additions & 0 deletions public/javascripts/asks.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,89 @@ var Asks = {
}

}

/* 添加问题 */
function addAsk(){
var txtTitle = $("#hidden_new_ask textarea:nth-of-type(1)");
ask_search_text = $("#add_ask input").val();
if(ask_search_text.length == 0){
$("#add_ask input").focus();
$("#add_ask input").click();
return false;
}
txtTitle.text(ask_search_text);
$.facebox({ div : "#hidden_new_ask", overlay : false });
txtTitle.focus();
return false;
}

var searchCache = new jCaches(40,false);
var lastSearchText = null;
var searchTimer = null;
function showSearchComplete(el,type){
clearTimeout(searchTimer);
html = "";
if(type == "click" && $(el).val() == ""){
html = $(el).attr("placeholder");
searchCallback(el,html);
}
else{
searchTimer = setTimeout(function(){
t = $(el).val().trim();
if(t == lastSearchText){
return false;
}
lastSearchText = t;
cachedItems = searchCache.get(t);
if(cachedItems == null){
$.ajax({
url : "/search.json",
data : { w : t },
dataType : "json",
success : function(res){
searchCache.add(t,res);
searchAjaxCallback(el,res);
}
});
}
else{
searchAjaxCallback(el,cachedItems);
}
},200);
}

return false;
}

function searchAjaxCallback(el,res){
html = '<ul class="complete">';
App.loading(false);
if(res.length > 0){
for(var i=0;i<res.length;i++){
html += '<li onclick="location.href = $(\'a\',this).attr(\'href\');">';
if(res[i].topics.length > 0){
html += '<span class="cate">'+res[i].topics[0]+'</span>';
}
item_title = res[i].title;
html += '<a href="/asks/'+res[i]._id+'">'+item_title+'</a>';
html += '</li>';
}
html += '<li class="more" onclick="location.href=\'/search?w='+t+'\';">关于“'+t+'”更多搜索结果...</li>';
}
else{
html += '<li>没有找到关于“'+t+'”的结果: <a href="#" onclick="return addAsk();">添加这个问题</a></li>';
}
html += "</ul>";
searchCallback(el,html);
}

function searchCallback(el, html){
el_width = $(el).width();
$(el).jDialog({
content : html,
width : el_width + 250,
title_visiable : false,
top_offset : 1,
left_offset : -1
});
}

0 comments on commit a838df7

Please sign in to comment.