| 1 |
// $Id$
|
| 2 |
|
| 3 |
/**
|
| 4 |
* @file
|
| 5 |
* Javascript implementation of Google Ajax Search API
|
| 6 |
*/
|
| 7 |
|
| 8 |
google.load('search', '1');
|
| 9 |
|
| 10 |
function OnLoad() {
|
| 11 |
var settings = Drupal.settings.gas;
|
| 12 |
var blockCount = settings.block_count;
|
| 13 |
var searchControls = new Array();
|
| 14 |
var drawOptions = new Array();
|
| 15 |
var searcherOptions = new Array();
|
| 16 |
var deltas = settings.current_block;
|
| 17 |
var firstBlockId = deltas[0];
|
| 18 |
//For all blocks, create searcher...
|
| 19 |
// For every delta, load settings for Google Ajax Search
|
| 20 |
jQuery.each(deltas, function(iteration, delta){
|
| 21 |
var blockId = this;
|
| 22 |
// Id of a setting. Needed because of this fucked up indexing of settings array -> indexing starts from delta of the first block
|
| 23 |
// var settingId = parseInt(firstBlockId)+parseInt(iteration);
|
| 24 |
// var blockSettings = settings[settingId];
|
| 25 |
var blockSettings = settings[delta];
|
| 26 |
//Function checking the Show/Hide form option
|
| 27 |
|
| 28 |
|
| 29 |
searchControls[delta] = new google.search.SearchControl();
|
| 30 |
|
| 31 |
//Set searcher place
|
| 32 |
searcherOptions[delta] = new google.search.SearcherOptions();
|
| 33 |
|
| 34 |
if(blockSettings.results_place) {
|
| 35 |
// If target place exists, show results in it. Otherwise show under textfield (like there's no place selected)
|
| 36 |
if(document.getElementById(blockSettings.results_place)) {
|
| 37 |
searcherOptions[delta].setRoot(document.getElementById(blockSettings.results_place));
|
| 38 |
}
|
| 39 |
}
|
| 40 |
//End of set searcher place
|
| 41 |
|
| 42 |
//If setting is on, turn on appropriate searcher type
|
| 43 |
//Set searcher type:
|
| 44 |
// Set settings for searcher type Web
|
| 45 |
if(blockSettings.search_options.web_search != 0) {
|
| 46 |
var Web = new google.search.WebSearch();
|
| 47 |
setLanguageAndDuplicateContent(blockSettings, Web);
|
| 48 |
searchSetSiteRestriction(blockSettings, Web, "web");
|
| 49 |
searchControls[delta].addSearcher(Web, searcherOptions[delta]);
|
| 50 |
}
|
| 51 |
|
| 52 |
// Set settings for searcher type Video
|
| 53 |
if(blockSettings.search_options.video_search != 0) {
|
| 54 |
var Video = new google.search.VideoSearch();
|
| 55 |
|
| 56 |
searchSetOrderBy(blockSettings, Video, "video");
|
| 57 |
searchControls[delta].addSearcher(Video, searcherOptions[delta]);
|
| 58 |
}
|
| 59 |
|
| 60 |
// Set settings for searcher type News
|
| 61 |
if(blockSettings.search_options.news_search != 0) {
|
| 62 |
var News = new google.search.NewsSearch();
|
| 63 |
|
| 64 |
searchSetSiteRestriction(blockSettings, News, "news");
|
| 65 |
searchSetOrderBy(blockSettings, News, "news");
|
| 66 |
setNewsTopic(blockSettings, News);
|
| 67 |
searchControls[delta].addSearcher(News, searcherOptions[delta]);
|
| 68 |
}
|
| 69 |
|
| 70 |
// Set settings for searcher type Image
|
| 71 |
if(blockSettings.search_options.image_search != 0) {
|
| 72 |
var Image = new google.search.ImageSearch();
|
| 73 |
|
| 74 |
searchSetSiteRestriction(blockSettings, Image, "image");
|
| 75 |
|
| 76 |
setImageRestriction(blockSettings.results_image_restrict_safesearch, Image);
|
| 77 |
setImageRestriction(blockSettings.results_image_restrict_imagesize, Image);
|
| 78 |
setImageRestriction(blockSettings.results_image_restrict_colorization, Image);
|
| 79 |
setImageRestriction(blockSettings.results_image_restrict_colorfilter, Image);
|
| 80 |
setImageRestriction(blockSettings.results_image_restrict_filetype, Image);
|
| 81 |
setImageRestriction(blockSettings.results_image_restrict_imagetype, Image);
|
| 82 |
|
| 83 |
searchControls[delta].addSearcher(Image, searcherOptions[delta]);
|
| 84 |
}
|
| 85 |
|
| 86 |
// Set settings for searcher type Blog
|
| 87 |
if(blockSettings.search_options.blog_search != 0) {
|
| 88 |
var Blog = new google.search.BlogSearch();
|
| 89 |
|
| 90 |
|
| 91 |
searchSetSiteRestriction(blockSettings, Blog, "blog");
|
| 92 |
searchSetOrderBy(blockSettings, Blog, "blog");
|
| 93 |
searchControls[delta].addSearcher(Blog, searcherOptions[delta]);
|
| 94 |
}
|
| 95 |
// Set settings for searcher type Book
|
| 96 |
if(blockSettings.search_options.book_search != 0) {
|
| 97 |
var Book = new google.search.BookSearch();
|
| 98 |
|
| 99 |
setBookType(blockSettings, Book);
|
| 100 |
searchControls[delta].addSearcher(Book, searcherOptions[delta]);
|
| 101 |
}
|
| 102 |
// Set settings for searcher type Patent
|
| 103 |
if(blockSettings.search_options.patent_search != 0) {
|
| 104 |
var Patent = new google.search.PatentSearch();
|
| 105 |
|
| 106 |
searchSetOrderBy(blockSettings, Patent, "patent");
|
| 107 |
searchControls[delta].addSearcher(Patent, searcherOptions[delta]);
|
| 108 |
}
|
| 109 |
|
| 110 |
drawOptions[delta] = new google.search.DrawOptions();
|
| 111 |
|
| 112 |
// End of set searcher type
|
| 113 |
|
| 114 |
|
| 115 |
// tell the searcher to display results inline or on tabs
|
| 116 |
drawOptions[delta].setDrawMode(google.search.SearchControl[blockSettings.results_display]);
|
| 117 |
|
| 118 |
// tell the searcher to display 4 or 8 results per page
|
| 119 |
searchControls[delta].setResultSetSize(google.search.Search[blockSettings.results_number]);
|
| 120 |
|
| 121 |
// place branding in branding placeholder if specified
|
| 122 |
if (blockSettings.form_display == 1 && blockSettings.branding_place) {
|
| 123 |
if (blockSettings.branding_orientation == "HORIZONTAL_BRANDING") {
|
| 124 |
google.search.Search.getBranding(document.getElementById(blockSettings.branding_place), google.search.Search.HORIZONTAL_BRANDING);
|
| 125 |
}
|
| 126 |
else {
|
| 127 |
google.search.Search.getBranding(document.getElementById(blockSettings.branding_place), google.search.Search.VERTICAL_BRANDING);
|
| 128 |
}
|
| 129 |
}
|
| 130 |
|
| 131 |
|
| 132 |
// set if the result link should be displayed in new window/frame etc...
|
| 133 |
switch (blockSettings.target_place) {
|
| 134 |
case "LINK_TARGET_BLANK":
|
| 135 |
searchControls[delta].setLinkTarget(google.search.Search.LINK_TARGET_BLANK);
|
| 136 |
break;
|
| 137 |
case "LINK_TARGET_SELF":
|
| 138 |
searchControls[delta].setLinkTarget(google.search.Search.LINK_TARGET_SELF);
|
| 139 |
break;
|
| 140 |
case "LINK_TARGET_TOP":
|
| 141 |
searchControls[delta].setLinkTarget(google.search.Search.LINK_TARGET_TOP);
|
| 142 |
break;
|
| 143 |
case "LINK_TARGET_PARENT":
|
| 144 |
searchControls[delta].setLinkTarget(google.search.Search.LINK_TARGET_PARENT);
|
| 145 |
break;
|
| 146 |
}
|
| 147 |
|
| 148 |
|
| 149 |
// tell the searcher to draw itself and tell it where to attach
|
| 150 |
searchControls[delta].draw(document.getElementById("searchcontrol-"+blockId), drawOptions[delta]);
|
| 151 |
|
| 152 |
// if there is a keyword to initial search... execute the search with this keyword
|
| 153 |
if (blockSettings.keywords) {
|
| 154 |
searchControls[delta].execute(blockSettings.keywords);
|
| 155 |
}
|
| 156 |
if (blockSettings.form_display) {
|
| 157 |
var form_display_settings = blockSettings.form_display;
|
| 158 |
formVisibilitySet(form_display_settings, delta);
|
| 159 |
}
|
| 160 |
|
| 161 |
});
|
| 162 |
}
|
| 163 |
|
| 164 |
google.setOnLoadCallback(OnLoad);
|
| 165 |
|
| 166 |
|
| 167 |
// Helper functions
|
| 168 |
function setLanguageAndDuplicateContent(blockSettings, Web) {
|
| 169 |
var code = blockSettings.results_web_language_restriction;
|
| 170 |
var duplicate = blockSettings.results_web_duplicate_content_filter;
|
| 171 |
|
| 172 |
if (code) {
|
| 173 |
if (duplicate == 1) {
|
| 174 |
Web.setRestriction(google.search.WebSearch.RESTRICT_EXTENDED_ARGS, {"lr" : code, "filter" : "1"});
|
| 175 |
}
|
| 176 |
else {
|
| 177 |
Web.setRestriction(google.search.WebSearch.RESTRICT_EXTENDED_ARGS, {"lr" : code});
|
| 178 |
}
|
| 179 |
}
|
| 180 |
else if (duplicate == 1) {
|
| 181 |
Web.setRestriction(google.search.WebSearch.RESTRICT_EXTENDED_ARGS, {"filter" : "1"});
|
| 182 |
}
|
| 183 |
}
|
| 184 |
|
| 185 |
function searchSetSiteRestriction(blockSettings, Web, option) {
|
| 186 |
if (option == "web" && (blockSettings.results_web_site_restriction)) {
|
| 187 |
Web.setSiteRestriction(blockSettings.results_web_site_restriction);
|
| 188 |
}
|
| 189 |
else if (option == "blog" && (blockSettings.results_blog_site_restriction)) {
|
| 190 |
Web.setSiteRestriction(blockSettings.results_blog_site_restriction);
|
| 191 |
}
|
| 192 |
else if (option == "image" && (blockSettings.results_image_site_restriction)) {
|
| 193 |
Web.setSiteRestriction(blockSettings.results_image_site_restriction);
|
| 194 |
}
|
| 195 |
else if (option == "news" && (blockSettings.results_news_site_restriction)) {
|
| 196 |
Web.setSiteRestriction(blockSettings.results_news_site_restriction);
|
| 197 |
}
|
| 198 |
}
|
| 199 |
|
| 200 |
function setRestrict(blockSettings, Web) {
|
| 201 |
var restrict = blockSettings.results_local_restrict_type;
|
| 202 |
|
| 203 |
if (restrict) {
|
| 204 |
Web.setRestriction(google.search.LocalSearch[restrict]);
|
| 205 |
}
|
| 206 |
}
|
| 207 |
|
| 208 |
function setNewsTopic(blockSettings, News) {
|
| 209 |
var topic = blockSettings.results_news_restrict_topic;
|
| 210 |
|
| 211 |
if (topic) {
|
| 212 |
Web.setRestriction(google.search.Search.RESTRICT_EXTENDED_ARGS, { "topic" : topic});
|
| 213 |
}
|
| 214 |
}
|
| 215 |
|
| 216 |
function searchSetOrderBy(blockSettings, Web, option) {
|
| 217 |
Web.setResultOrder(google.search.Search.ORDER_BY_DATE);
|
| 218 |
|
| 219 |
if (option == "video" && (blockSettings.results_video_order == "by relevance")) {
|
| 220 |
Web.setResultOrder(google.search.Search.ORDER_BY_RELEVANCE);
|
| 221 |
}
|
| 222 |
else if (option == "blog" && (blockSettings.results_blog_order == "by relevance")) {
|
| 223 |
Web.setResultOrder(google.search.Search.ORDER_BY_RELEVANCE);
|
| 224 |
}
|
| 225 |
else if (option == "patent" && (blockSettings.results_patent_order == "by relevance")) {
|
| 226 |
Web.setResultOrder(google.search.Search.ORDER_BY_RELEVANCE);
|
| 227 |
}
|
| 228 |
else if (option == "news" && (blockSettings.results_news_order == "by relevance")) {
|
| 229 |
Web.setResultOrder(google.search.Search.ORDER_BY_RELEVANCE);
|
| 230 |
}
|
| 231 |
}
|
| 232 |
|
| 233 |
function setBookType(blockSettings, Book) {
|
| 234 |
var type = blockSettings.results_book_restrict_type;
|
| 235 |
|
| 236 |
if (type == "TYPE_ALL_BOOKS") {
|
| 237 |
Book.setRestriction(google.search.BookSearch.TYPE_ALL_BOOKS)
|
| 238 |
}
|
| 239 |
else if (type == "TYPE_FULL_VIEW_BOOKS"){
|
| 240 |
Book.setRestriction(google.search.BookSearch.TYPE_FULL_VIEW_BOOKS);
|
| 241 |
}
|
| 242 |
}
|
| 243 |
|
| 244 |
function setImageRestriction(option, Image) {
|
| 245 |
option ? Image.setRestriction(google.search.ImageSearch[option]) : "";
|
| 246 |
}
|
| 247 |
|
| 248 |
function formVisibilitySet(form_display_settings, delta) {
|
| 249 |
var visible = form_display_settings;
|
| 250 |
|
| 251 |
if (visible == 1) {
|
| 252 |
$("#searchcontrol-" + delta + " div.gsc-control form.gsc-search-box").addClass("hidden");
|
| 253 |
}
|
| 254 |
}
|