blob: f97feebdfde95a27a00f94118db8dcf9c273ea25 [file] [log] [blame]
Dirk Dougherty541b4942014-02-14 18:31:53 -08001var classesNav;
2var devdocNav;
3var sidenav;
4var cookie_namespace = 'android_developer';
5var NAV_PREF_TREE = "tree";
6var NAV_PREF_PANELS = "panels";
7var nav_pref;
8var isMobile = false; // true if mobile, so we can adjust some layout
9var mPagePath; // initialized in ready() function
10
11var basePath = getBaseUri(location.pathname);
12var SITE_ROOT = toRoot + basePath.substring(1,basePath.indexOf("/",1));
13var GOOGLE_DATA; // combined data for google service apis, used for search suggest
14
15// Ensure that all ajax getScript() requests allow caching
16$.ajaxSetup({
17 cache: true
18});
19
20/****** ON LOAD SET UP STUFF *********/
21
22var navBarIsFixed = false;
23$(document).ready(function() {
24
25 // load json file for JD doc search suggestions
26 $.getScript(toRoot + 'jd_lists_unified.js');
27 // load json file for Android API search suggestions
28 $.getScript(toRoot + 'reference/lists.js');
29 // load json files for Google services API suggestions
30 $.getScript(toRoot + 'reference/gcm_lists.js', function(data, textStatus, jqxhr) {
31 // once the GCM json (GCM_DATA) is loaded, load the GMS json (GMS_DATA) and merge the data
32 if(jqxhr.status === 200) {
33 $.getScript(toRoot + 'reference/gms_lists.js', function(data, textStatus, jqxhr) {
34 if(jqxhr.status === 200) {
35 // combine GCM and GMS data
36 GOOGLE_DATA = GMS_DATA;
37 var start = GOOGLE_DATA.length;
38 for (var i=0; i<GCM_DATA.length; i++) {
39 GOOGLE_DATA.push({id:start+i, label:GCM_DATA[i].label,
40 link:GCM_DATA[i].link, type:GCM_DATA[i].type});
41 }
42 }
43 });
44 }
45 });
46
47 // setup keyboard listener for search shortcut
48 $('body').keyup(function(event) {
49 if (event.which == 191) {
50 $('#search_autocomplete').focus();
51 }
52 });
53
54 // init the fullscreen toggle click event
55 $('#nav-swap .fullscreen').click(function(){
56 if ($(this).hasClass('disabled')) {
57 toggleFullscreen(true);
58 } else {
59 toggleFullscreen(false);
60 }
61 });
62
63 // initialize the divs with custom scrollbars
64 $('.scroll-pane').jScrollPane( {verticalGutter:0} );
65
66 // add HRs below all H2s (except for a few other h2 variants)
67 $('h2').not('#qv h2').not('#tb h2').not('.sidebox h2').not('#devdoc-nav h2').not('h2.norule').css({marginBottom:0}).after('<hr/>');
68
69 // set up the search close button
70 $('.search .close').click(function() {
71 $searchInput = $('#search_autocomplete');
72 $searchInput.attr('value', '');
73 $(this).addClass("hide");
74 $("#search-container").removeClass('active');
75 $("#search_autocomplete").blur();
76 search_focus_changed($searchInput.get(), false);
77 hideResults();
78 });
79
80 // Set up quicknav
81 var quicknav_open = false;
82 $("#btn-quicknav").click(function() {
83 if (quicknav_open) {
84 $(this).removeClass('active');
85 quicknav_open = false;
86 collapse();
87 } else {
88 $(this).addClass('active');
89 quicknav_open = true;
90 expand();
91 }
92 })
93
94 var expand = function() {
95 $('#header-wrap').addClass('quicknav');
96 $('#quicknav').stop().show().animate({opacity:'1'});
97 }
98
99 var collapse = function() {
100 $('#quicknav').stop().animate({opacity:'0'}, 100, function() {
101 $(this).hide();
102 $('#header-wrap').removeClass('quicknav');
103 });
104 }
105
106
107 //Set up search
108 $("#search_autocomplete").focus(function() {
109 $("#search-container").addClass('active');
110 })
111 $("#search-container").mouseover(function() {
112 $("#search-container").addClass('active');
113 $("#search_autocomplete").focus();
114 })
115 $("#search-container").mouseout(function() {
116 if ($("#search_autocomplete").is(":focus")) return;
117 if ($("#search_autocomplete").val() == '') {
118 setTimeout(function(){
119 $("#search-container").removeClass('active');
120 $("#search_autocomplete").blur();
121 },250);
122 }
123 })
124 $("#search_autocomplete").blur(function() {
125 if ($("#search_autocomplete").val() == '') {
126 $("#search-container").removeClass('active');
127 }
128 })
129
130
131 // prep nav expandos
132 var pagePath = document.location.pathname;
133 // account for intl docs by removing the intl/*/ path
134 if (pagePath.indexOf("/intl/") == 0) {
135 pagePath = pagePath.substr(pagePath.indexOf("/",6)); // start after intl/ to get last /
136 }
137
138 if (pagePath.indexOf(SITE_ROOT) == 0) {
139 if (pagePath == '' || pagePath.charAt(pagePath.length - 1) == '/') {
140 pagePath += 'index.html';
141 }
142 }
143
144 // Need a copy of the pagePath before it gets changed in the next block;
145 // it's needed to perform proper tab highlighting in offline docs (see rootDir below)
146 var pagePathOriginal = pagePath;
147 if (SITE_ROOT.match(/\.\.\//) || SITE_ROOT == '') {
148 // If running locally, SITE_ROOT will be a relative path, so account for that by
149 // finding the relative URL to this page. This will allow us to find links on the page
150 // leading back to this page.
151 var pathParts = pagePath.split('/');
152 var relativePagePathParts = [];
153 var upDirs = (SITE_ROOT.match(/(\.\.\/)+/) || [''])[0].length / 3;
154 for (var i = 0; i < upDirs; i++) {
155 relativePagePathParts.push('..');
156 }
157 for (var i = 0; i < upDirs; i++) {
158 relativePagePathParts.push(pathParts[pathParts.length - (upDirs - i) - 1]);
159 }
160 relativePagePathParts.push(pathParts[pathParts.length - 1]);
161 pagePath = relativePagePathParts.join('/');
162 } else {
163 // Otherwise the page path is already an absolute URL
164 }
165
166 // Highlight the header tabs...
167 // highlight Design tab
168 if ($("body").hasClass("design")) {
169 $("#header li.design a").addClass("selected");
Dirk Dougherty08032402014-02-15 10:14:35 -0800170 $("#sticky-header").addClass("design");
Dirk Dougherty541b4942014-02-14 18:31:53 -0800171
172 // highlight Develop tab
173 } else if ($("body").hasClass("develop") || $("body").hasClass("google")) {
174 $("#header li.develop a").addClass("selected");
Dirk Dougherty08032402014-02-15 10:14:35 -0800175 $("#sticky-header").addClass("develop");
Dirk Dougherty541b4942014-02-14 18:31:53 -0800176 // In Develop docs, also highlight appropriate sub-tab
177 var rootDir = pagePathOriginal.substring(1,pagePathOriginal.indexOf('/', 1));
178 if (rootDir == "training") {
179 $("#nav-x li.training a").addClass("selected");
180 } else if (rootDir == "guide") {
181 $("#nav-x li.guide a").addClass("selected");
182 } else if (rootDir == "reference") {
183 // If the root is reference, but page is also part of Google Services, select Google
184 if ($("body").hasClass("google")) {
185 $("#nav-x li.google a").addClass("selected");
186 } else {
187 $("#nav-x li.reference a").addClass("selected");
188 }
189 } else if ((rootDir == "tools") || (rootDir == "sdk")) {
190 $("#nav-x li.tools a").addClass("selected");
191 } else if ($("body").hasClass("google")) {
192 $("#nav-x li.google a").addClass("selected");
193 } else if ($("body").hasClass("samples")) {
194 $("#nav-x li.samples a").addClass("selected");
195 }
196
197 // highlight Distribute tab
198 } else if ($("body").hasClass("distribute")) {
199 $("#header li.distribute a").addClass("selected");
Dirk Dougherty08032402014-02-15 10:14:35 -0800200 $("#sticky-header").addClass("distribute");
Dirk Dougherty541b4942014-02-14 18:31:53 -0800201
Dirk Dougherty08032402014-02-15 10:14:35 -0800202 var baseFrag = pagePathOriginal.indexOf('/', 1) + 1;
203 var secondFrag = pagePathOriginal.substring(baseFrag, pagePathOriginal.indexOf('/', baseFrag));
204 if (secondFrag == "users") {
205 $("#nav-x li.users a").addClass("selected");
206 } else if (secondFrag == "engage") {
207 $("#nav-x li.engage a").addClass("selected");
208 } else if (secondFrag == "monetize") {
209 $("#nav-x li.monetize a").addClass("selected");
210 } else if (secondFrag == "tools") {
211 $("#nav-x li.disttools a").addClass("selected");
212 } else if (secondFrag == "stories") {
213 $("#nav-x li.stories a").addClass("selected");
214 } else if (secondFrag == "essentials") {
215 $("#nav-x li.essentials a").addClass("selected");
216 } else if (secondFrag == "googleplay") {
217 $("#nav-x li.googleplay a").addClass("selected");
218 }
219 }
Dirk Dougherty541b4942014-02-14 18:31:53 -0800220 // set global variable so we can highlight the sidenav a bit later (such as for google reference)
221 // and highlight the sidenav
222 mPagePath = pagePath;
223 highlightSidenav();
Scott Main20cf2a92014-04-02 21:57:20 -0700224 buildBreadcrumbs();
Dirk Dougherty541b4942014-02-14 18:31:53 -0800225
226 // set up prev/next links if they exist
227 var $selNavLink = $('#nav').find('a[href="' + pagePath + '"]');
228 var $selListItem;
229 if ($selNavLink.length) {
230 $selListItem = $selNavLink.closest('li');
231
232 // set up prev links
233 var $prevLink = [];
234 var $prevListItem = $selListItem.prev('li');
235
236 var crossBoundaries = ($("body.design").length > 0) || ($("body.guide").length > 0) ? true :
237false; // navigate across topic boundaries only in design docs
238 if ($prevListItem.length) {
239 if ($prevListItem.hasClass('nav-section')) {
240 // jump to last topic of previous section
241 $prevLink = $prevListItem.find('a:last');
242 } else if (!$selListItem.hasClass('nav-section')) {
243 // jump to previous topic in this section
244 $prevLink = $prevListItem.find('a:eq(0)');
245 }
246 } else {
247 // jump to this section's index page (if it exists)
248 var $parentListItem = $selListItem.parents('li');
249 $prevLink = $selListItem.parents('li').find('a');
250
251 // except if cross boundaries aren't allowed, and we're at the top of a section already
252 // (and there's another parent)
253 if (!crossBoundaries && $parentListItem.hasClass('nav-section')
254 && $selListItem.hasClass('nav-section')) {
255 $prevLink = [];
256 }
257 }
258
259 // set up next links
260 var $nextLink = [];
261 var startClass = false;
262 var training = $(".next-class-link").length; // decides whether to provide "next class" link
263 var isCrossingBoundary = false;
264
265 if ($selListItem.hasClass('nav-section') && $selListItem.children('div.empty').length == 0) {
266 // we're on an index page, jump to the first topic
267 $nextLink = $selListItem.find('ul:eq(0)').find('a:eq(0)');
268
269 // if there aren't any children, go to the next section (required for About pages)
270 if($nextLink.length == 0) {
271 $nextLink = $selListItem.next('li').find('a');
272 } else if ($('.topic-start-link').length) {
273 // as long as there's a child link and there is a "topic start link" (we're on a landing)
274 // then set the landing page "start link" text to be the first doc title
275 $('.topic-start-link').text($nextLink.text().toUpperCase());
276 }
277
278 // If the selected page has a description, then it's a class or article homepage
279 if ($selListItem.find('a[description]').length) {
280 // this means we're on a class landing page
281 startClass = true;
282 }
283 } else {
284 // jump to the next topic in this section (if it exists)
285 $nextLink = $selListItem.next('li').find('a:eq(0)');
286 if ($nextLink.length == 0) {
287 isCrossingBoundary = true;
288 // no more topics in this section, jump to the first topic in the next section
289 $nextLink = $selListItem.parents('li:eq(0)').next('li.nav-section').find('a:eq(0)');
290 if (!$nextLink.length) { // Go up another layer to look for next page (lesson > class > course)
291 $nextLink = $selListItem.parents('li:eq(1)').next('li.nav-section').find('a:eq(0)');
292 if ($nextLink.length == 0) {
293 // if that doesn't work, we're at the end of the list, so disable NEXT link
294 $('.next-page-link').attr('href','').addClass("disabled")
295 .click(function() { return false; });
296 }
297 }
298 }
299 }
300
301 if (startClass) {
302 $('.start-class-link').attr('href', $nextLink.attr('href')).removeClass("hide");
303
304 // if there's no training bar (below the start button),
305 // then we need to add a bottom border to button
306 if (!$("#tb").length) {
307 $('.start-class-link').css({'border-bottom':'1px solid #DADADA'});
308 }
309 } else if (isCrossingBoundary && !$('body.design').length) { // Design always crosses boundaries
310 $('.content-footer.next-class').show();
311 $('.next-page-link').attr('href','')
312 .removeClass("hide").addClass("disabled")
313 .click(function() { return false; });
314 if ($nextLink.length) {
315 $('.next-class-link').attr('href',$nextLink.attr('href'))
316 .removeClass("hide").append($nextLink.html());
317 $('.next-class-link').find('.new').empty();
318 }
319 } else {
320 $('.next-page-link').attr('href', $nextLink.attr('href')).removeClass("hide");
321 }
322
323 if (!startClass && $prevLink.length) {
324 var prevHref = $prevLink.attr('href');
325 if (prevHref == SITE_ROOT + 'index.html') {
326 // Don't show Previous when it leads to the homepage
327 } else {
328 $('.prev-page-link').attr('href', $prevLink.attr('href')).removeClass("hide");
329 }
330 }
331
332 // If this is a training 'article', there should be no prev/next nav
333 // ... if the grandparent is the "nav" ... and it has no child list items...
334 if (training && $selListItem.parents('ul').eq(1).is('[id="nav"]') &&
335 !$selListItem.find('li').length) {
336 $('.next-page-link,.prev-page-link').attr('href','').addClass("disabled")
337 .click(function() { return false; });
338 }
339
340 }
341
342
343
344 // Set up the course landing pages for Training with class names and descriptions
345 if ($('body.trainingcourse').length) {
346 var $classLinks = $selListItem.find('ul li a').not('#nav .nav-section .nav-section ul a');
347 var $classDescriptions = $classLinks.attr('description');
348
349 var $olClasses = $('<ol class="class-list"></ol>');
350 var $liClass;
351 var $imgIcon;
352 var $h2Title;
353 var $pSummary;
354 var $olLessons;
355 var $liLesson;
356 $classLinks.each(function(index) {
357 $liClass = $('<li></li>');
358 $h2Title = $('<a class="title" href="'+$(this).attr('href')+'"><h2>' + $(this).html()+'</h2><span></span></a>');
359 $pSummary = $('<p class="description">' + $(this).attr('description') + '</p>');
360
361 $olLessons = $('<ol class="lesson-list"></ol>');
362
363 $lessons = $(this).closest('li').find('ul li a');
364
365 if ($lessons.length) {
366 $imgIcon = $('<img src="'+toRoot+'assets/images/resource-tutorial.png" '
367 + ' width="64" height="64" alt=""/>');
368 $lessons.each(function(index) {
369 $olLessons.append('<li><a href="'+$(this).attr('href')+'">' + $(this).html()+'</a></li>');
370 });
371 } else {
372 $imgIcon = $('<img src="'+toRoot+'assets/images/resource-article.png" '
373 + ' width="64" height="64" alt=""/>');
374 $pSummary.addClass('article');
375 }
376
377 $liClass.append($h2Title).append($imgIcon).append($pSummary).append($olLessons);
378 $olClasses.append($liClass);
379 });
380 $('.jd-descr').append($olClasses);
381 }
382
383 // Set up expand/collapse behavior
384 initExpandableNavItems("#nav");
385
386
387 $(".scroll-pane").scroll(function(event) {
388 event.preventDefault();
389 return false;
390 });
391
392 /* Resize nav height when window height changes */
393 $(window).resize(function() {
394 if ($('#side-nav').length == 0) return;
395 var stylesheet = $('link[rel="stylesheet"][class="fullscreen"]');
396 setNavBarLeftPos(); // do this even if sidenav isn't fixed because it could become fixed
397 // make sidenav behave when resizing the window and side-scolling is a concern
398 if (navBarIsFixed) {
399 if ((stylesheet.attr("disabled") == "disabled") || stylesheet.length == 0) {
400 updateSideNavPosition();
401 } else {
402 updateSidenavFullscreenWidth();
403 }
404 }
405 resizeNav();
406 });
407
408
Dirk Dougherty541b4942014-02-14 18:31:53 -0800409 var navBarLeftPos;
410 if ($('#devdoc-nav').length) {
411 setNavBarLeftPos();
412 }
413
414
415 // Set up play-on-hover <video> tags.
416 $('video.play-on-hover').bind('click', function(){
417 $(this).get(0).load(); // in case the video isn't seekable
418 $(this).get(0).play();
419 });
420
421 // Set up tooltips
422 var TOOLTIP_MARGIN = 10;
423 $('acronym,.tooltip-link').each(function() {
424 var $target = $(this);
425 var $tooltip = $('<div>')
426 .addClass('tooltip-box')
427 .append($target.attr('title'))
428 .hide()
429 .appendTo('body');
430 $target.removeAttr('title');
431
432 $target.hover(function() {
433 // in
434 var targetRect = $target.offset();
435 targetRect.width = $target.width();
436 targetRect.height = $target.height();
437
438 $tooltip.css({
439 left: targetRect.left,
440 top: targetRect.top + targetRect.height + TOOLTIP_MARGIN
441 });
442 $tooltip.addClass('below');
443 $tooltip.show();
444 }, function() {
445 // out
446 $tooltip.hide();
447 });
448 });
449
450 // Set up <h2> deeplinks
451 $('h2').click(function() {
452 var id = $(this).attr('id');
453 if (id) {
454 document.location.hash = id;
455 }
456 });
457
458 //Loads the +1 button
459 var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
460 po.src = 'https://apis.google.com/js/plusone.js';
461 var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
462
463
464 // Revise the sidenav widths to make room for the scrollbar
465 // which avoids the visible width from changing each time the bar appears
466 var $sidenav = $("#side-nav");
467 var sidenav_width = parseInt($sidenav.innerWidth());
468
469 $("#devdoc-nav #nav").css("width", sidenav_width - 4 + "px"); // 4px is scrollbar width
470
471
472 $(".scroll-pane").removeAttr("tabindex"); // get rid of tabindex added by jscroller
473
474 if ($(".scroll-pane").length > 1) {
475 // Check if there's a user preference for the panel heights
476 var cookieHeight = readCookie("reference_height");
477 if (cookieHeight) {
478 restoreHeight(cookieHeight);
479 }
480 }
481
482 resizeNav();
483
484 /* init the language selector based on user cookie for lang */
485 loadLangPref();
486 changeNavLang(getLangPref());
487
488 /* setup event handlers to ensure the overflow menu is visible while picking lang */
489 $("#language select")
490 .mousedown(function() {
491 $("div.morehover").addClass("hover"); })
492 .blur(function() {
493 $("div.morehover").removeClass("hover"); });
494
495 /* some global variable setup */
496 resizePackagesNav = $("#resize-packages-nav");
497 classesNav = $("#classes-nav");
498 devdocNav = $("#devdoc-nav");
499
500 var cookiePath = "";
501 if (location.href.indexOf("/reference/") != -1) {
502 cookiePath = "reference_";
503 } else if (location.href.indexOf("/guide/") != -1) {
504 cookiePath = "guide_";
505 } else if (location.href.indexOf("/tools/") != -1) {
506 cookiePath = "tools_";
507 } else if (location.href.indexOf("/training/") != -1) {
508 cookiePath = "training_";
509 } else if (location.href.indexOf("/design/") != -1) {
510 cookiePath = "design_";
511 } else if (location.href.indexOf("/distribute/") != -1) {
512 cookiePath = "distribute_";
513 }
514
515});
516// END of the onload event
517
518
519function initExpandableNavItems(rootTag) {
520 $(rootTag + ' li.nav-section .nav-section-header').click(function() {
521 var section = $(this).closest('li.nav-section');
522 if (section.hasClass('expanded')) {
523 /* hide me and descendants */
524 section.find('ul').slideUp(250, function() {
525 // remove 'expanded' class from my section and any children
526 section.closest('li').removeClass('expanded');
527 $('li.nav-section', section).removeClass('expanded');
528 resizeNav();
529 });
530 } else {
531 /* show me */
532 // first hide all other siblings
533 var $others = $('li.nav-section.expanded', $(this).closest('ul')).not('.sticky');
534 $others.removeClass('expanded').children('ul').slideUp(250);
535
536 // now expand me
537 section.closest('li').addClass('expanded');
538 section.children('ul').slideDown(250, function() {
539 resizeNav();
540 });
541 }
542 });
543
544 // Stop expand/collapse behavior when clicking on nav section links
545 // (since we're navigating away from the page)
546 // This selector captures the first instance of <a>, but not those with "#" as the href.
547 $('.nav-section-header').find('a:eq(0)').not('a[href="#"]').click(function(evt) {
548 window.location.href = $(this).attr('href');
549 return false;
550 });
551}
552
Scott Main20cf2a92014-04-02 21:57:20 -0700553
554/** Create the list of breadcrumb links in the sticky header */
555function buildBreadcrumbs() {
556 var $breadcrumbUl = $("#sticky-header ul.breadcrumb");
557 // Add the secondary horizontal nav item, if provided
558 var $selectedSecondNav = $("div#nav-x ul.nav-x a.selected").clone().removeClass("selected");
559 if ($selectedSecondNav.length) {
560 $breadcrumbUl.prepend($("<li>").append($selectedSecondNav))
561 }
562 // Add the primary horizontal nav
563 var $selectedFirstNav = $("div#header-wrap ul.nav-x a.selected").clone().removeClass("selected");
564 $breadcrumbUl.prepend($("<li>").append($selectedFirstNav));
565}
566
567
568
Dirk Dougherty541b4942014-02-14 18:31:53 -0800569/** Highlight the current page in sidenav, expanding children as appropriate */
570function highlightSidenav() {
571 // if something is already highlighted, undo it. This is for dynamic navigation (Samples index)
572 if ($("ul#nav li.selected").length) {
573 unHighlightSidenav();
574 }
575 // look for URL in sidenav, including the hash
576 var $selNavLink = $('#nav').find('a[href="' + mPagePath + location.hash + '"]');
577
578 // If the selNavLink is still empty, look for it without the hash
579 if ($selNavLink.length == 0) {
580 $selNavLink = $('#nav').find('a[href="' + mPagePath + '"]');
581 }
582
583 var $selListItem;
584 if ($selNavLink.length) {
585 // Find this page's <li> in sidenav and set selected
586 $selListItem = $selNavLink.closest('li');
587 $selListItem.addClass('selected');
588
589 // Traverse up the tree and expand all parent nav-sections
590 $selNavLink.parents('li.nav-section').each(function() {
591 $(this).addClass('expanded');
592 $(this).children('ul').show();
593 });
594 }
595}
596
597function unHighlightSidenav() {
598 $("ul#nav li.selected").removeClass("selected");
599 $('ul#nav li.nav-section.expanded').removeClass('expanded').children('ul').hide();
600}
601
602function toggleFullscreen(enable) {
603 var delay = 20;
604 var enabled = true;
605 var stylesheet = $('link[rel="stylesheet"][class="fullscreen"]');
606 if (enable) {
607 // Currently NOT USING fullscreen; enable fullscreen
608 stylesheet.removeAttr('disabled');
609 $('#nav-swap .fullscreen').removeClass('disabled');
610 $('#devdoc-nav').css({left:''});
611 setTimeout(updateSidenavFullscreenWidth,delay); // need to wait a moment for css to switch
612 enabled = true;
613 } else {
614 // Currently USING fullscreen; disable fullscreen
615 stylesheet.attr('disabled', 'disabled');
616 $('#nav-swap .fullscreen').addClass('disabled');
617 setTimeout(updateSidenavFixedWidth,delay); // need to wait a moment for css to switch
618 enabled = false;
619 }
620 writeCookie("fullscreen", enabled, null, null);
621 setNavBarLeftPos();
622 resizeNav(delay);
623 updateSideNavPosition();
624 setTimeout(initSidenavHeightResize,delay);
625}
626
627
628function setNavBarLeftPos() {
629 navBarLeftPos = $('#body-content').offset().left;
630}
631
632
633function updateSideNavPosition() {
634 var newLeft = $(window).scrollLeft() - navBarLeftPos;
635 $('#devdoc-nav').css({left: -newLeft});
636 $('#devdoc-nav .totop').css({left: -(newLeft - parseInt($('#side-nav').css('margin-left')))});
637}
638
639// TODO: use $(document).ready instead
640function addLoadEvent(newfun) {
641 var current = window.onload;
642 if (typeof window.onload != 'function') {
643 window.onload = newfun;
644 } else {
645 window.onload = function() {
646 current();
647 newfun();
648 }
649 }
650}
651
652var agent = navigator['userAgent'].toLowerCase();
653// If a mobile phone, set flag and do mobile setup
654if ((agent.indexOf("mobile") != -1) || // android, iphone, ipod
655 (agent.indexOf("blackberry") != -1) ||
656 (agent.indexOf("webos") != -1) ||
657 (agent.indexOf("mini") != -1)) { // opera mini browsers
658 isMobile = true;
659}
660
661
662$(document).ready(function() {
663 $("pre:not(.no-pretty-print)").addClass("prettyprint");
664 prettyPrint();
665});
666
667
668
669
670/* ######### RESIZE THE SIDENAV HEIGHT ########## */
671
672function resizeNav(delay) {
673 var $nav = $("#devdoc-nav");
674 var $window = $(window);
675 var navHeight;
676
677 // Get the height of entire window and the total header height.
678 // Then figure out based on scroll position whether the header is visible
679 var windowHeight = $window.height();
680 var scrollTop = $window.scrollTop();
Scott Main20cf2a92014-04-02 21:57:20 -0700681 var headerHeight = $('#header-wrapper').outerHeight();
682 var headerVisible = scrollTop < stickyTop;
Dirk Dougherty541b4942014-02-14 18:31:53 -0800683
684 // get the height of space between nav and top of window.
685 // Could be either margin or top position, depending on whether the nav is fixed.
686 var topMargin = (parseInt($nav.css('margin-top')) || parseInt($nav.css('top'))) + 1;
687 // add 1 for the #side-nav bottom margin
688
689 // Depending on whether the header is visible, set the side nav's height.
690 if (headerVisible) {
691 // The sidenav height grows as the header goes off screen
Scott Main20cf2a92014-04-02 21:57:20 -0700692 navHeight = windowHeight - (headerHeight - scrollTop) - topMargin;
Dirk Dougherty541b4942014-02-14 18:31:53 -0800693 } else {
694 // Once header is off screen, the nav height is almost full window height
695 navHeight = windowHeight - topMargin;
696 }
697
698
699
700 $scrollPanes = $(".scroll-pane");
701 if ($scrollPanes.length > 1) {
702 // subtract the height of the api level widget and nav swapper from the available nav height
703 navHeight -= ($('#api-nav-header').outerHeight(true) + $('#nav-swap').outerHeight(true));
704
705 $("#swapper").css({height:navHeight + "px"});
706 if ($("#nav-tree").is(":visible")) {
707 $("#nav-tree").css({height:navHeight});
708 }
709
710 var classesHeight = navHeight - parseInt($("#resize-packages-nav").css("height")) - 10 + "px";
711 //subtract 10px to account for drag bar
712
713 // if the window becomes small enough to make the class panel height 0,
714 // then the package panel should begin to shrink
715 if (parseInt(classesHeight) <= 0) {
716 $("#resize-packages-nav").css({height:navHeight - 10}); //subtract 10px for drag bar
717 $("#packages-nav").css({height:navHeight - 10});
718 }
719
720 $("#classes-nav").css({'height':classesHeight, 'margin-top':'10px'});
721 $("#classes-nav .jspContainer").css({height:classesHeight});
722
723
724 } else {
725 $nav.height(navHeight);
726 }
727
728 if (delay) {
729 updateFromResize = true;
730 delayedReInitScrollbars(delay);
731 } else {
732 reInitScrollbars();
733 }
734
735}
736
737var updateScrollbars = false;
738var updateFromResize = false;
739
740/* Re-initialize the scrollbars to account for changed nav size.
741 * This method postpones the actual update by a 1/4 second in order to optimize the
742 * scroll performance while the header is still visible, because re-initializing the
743 * scroll panes is an intensive process.
744 */
745function delayedReInitScrollbars(delay) {
746 // If we're scheduled for an update, but have received another resize request
747 // before the scheduled resize has occured, just ignore the new request
748 // (and wait for the scheduled one).
749 if (updateScrollbars && updateFromResize) {
750 updateFromResize = false;
751 return;
752 }
753
754 // We're scheduled for an update and the update request came from this method's setTimeout
755 if (updateScrollbars && !updateFromResize) {
756 reInitScrollbars();
757 updateScrollbars = false;
758 } else {
759 updateScrollbars = true;
760 updateFromResize = false;
761 setTimeout('delayedReInitScrollbars()',delay);
762 }
763}
764
765/* Re-initialize the scrollbars to account for changed nav size. */
766function reInitScrollbars() {
767 var pane = $(".scroll-pane").each(function(){
768 var api = $(this).data('jsp');
769 if (!api) { setTimeout(reInitScrollbars,300); return;}
770 api.reinitialise( {verticalGutter:0} );
771 });
772 $(".scroll-pane").removeAttr("tabindex"); // get rid of tabindex added by jscroller
773}
774
775
776/* Resize the height of the nav panels in the reference,
777 * and save the new size to a cookie */
778function saveNavPanels() {
779 var basePath = getBaseUri(location.pathname);
780 var section = basePath.substring(1,basePath.indexOf("/",1));
781 writeCookie("height", resizePackagesNav.css("height"), section, null);
782}
783
784
785
786function restoreHeight(packageHeight) {
787 $("#resize-packages-nav").height(packageHeight);
788 $("#packages-nav").height(packageHeight);
789 // var classesHeight = navHeight - packageHeight;
790 // $("#classes-nav").css({height:classesHeight});
791 // $("#classes-nav .jspContainer").css({height:classesHeight});
792}
793
794
795
796/* ######### END RESIZE THE SIDENAV HEIGHT ########## */
797
798
799
800
801
802/** Scroll the jScrollPane to make the currently selected item visible
803 This is called when the page finished loading. */
804function scrollIntoView(nav) {
805 var $nav = $("#"+nav);
806 var element = $nav.jScrollPane({/* ...settings... */});
807 var api = element.data('jsp');
808
809 if ($nav.is(':visible')) {
810 var $selected = $(".selected", $nav);
811 if ($selected.length == 0) {
812 // If no selected item found, exit
813 return;
814 }
815 // get the selected item's offset from its container nav by measuring the item's offset
816 // relative to the document then subtract the container nav's offset relative to the document
817 var selectedOffset = $selected.offset().top - $nav.offset().top;
818 if (selectedOffset > $nav.height() * .8) { // multiply nav height by .8 so we move up the item
819 // if it's more than 80% down the nav
820 // scroll the item up by an amount equal to 80% the container nav's height
821 api.scrollTo(0, selectedOffset - ($nav.height() * .8), false);
822 }
823 }
824}
825
826
827
828
829
830
831/* Show popup dialogs */
832function showDialog(id) {
833 $dialog = $("#"+id);
834 $dialog.prepend('<div class="box-border"><div class="top"> <div class="left"></div> <div class="right"></div></div><div class="bottom"> <div class="left"></div> <div class="right"></div> </div> </div>');
835 $dialog.wrapInner('<div/>');
836 $dialog.removeClass("hide");
837}
838
839
840
841
842
843/* ######### COOKIES! ########## */
844
845function readCookie(cookie) {
846 var myCookie = cookie_namespace+"_"+cookie+"=";
847 if (document.cookie) {
848 var index = document.cookie.indexOf(myCookie);
849 if (index != -1) {
850 var valStart = index + myCookie.length;
851 var valEnd = document.cookie.indexOf(";", valStart);
852 if (valEnd == -1) {
853 valEnd = document.cookie.length;
854 }
855 var val = document.cookie.substring(valStart, valEnd);
856 return val;
857 }
858 }
859 return 0;
860}
861
862function writeCookie(cookie, val, section, expiration) {
863 if (val==undefined) return;
864 section = section == null ? "_" : "_"+section+"_";
865 if (expiration == null) {
866 var date = new Date();
867 date.setTime(date.getTime()+(10*365*24*60*60*1000)); // default expiration is one week
868 expiration = date.toGMTString();
869 }
870 var cookieValue = cookie_namespace + section + cookie + "=" + val
871 + "; expires=" + expiration+"; path=/";
872 document.cookie = cookieValue;
873}
874
875/* ######### END COOKIES! ########## */
876
877
878
879
Scott Maind6a8e662014-04-12 16:40:48 -0700880var stickyTop;
881/* Sets the vertical scoll position at which the sticky bar should appear.
882 This method is called to reset the position when search results appear or hide */
883function setStickyTop() {
884 stickyTop = $('#header-wrapper').outerHeight() - $('#sticky-header').outerHeight();
885}
886
Dirk Dougherty541b4942014-02-14 18:31:53 -0800887
Dirk Dougherty08032402014-02-15 10:14:35 -0800888/*
889 * Displays sticky nav bar on pages when dac header scrolls out of view
890 */
Dirk Dougherty08032402014-02-15 10:14:35 -0800891(function() {
892 $(document).ready(function() {
893
Scott Maind6a8e662014-04-12 16:40:48 -0700894 setStickyTop();
Dirk Dougherty08032402014-02-15 10:14:35 -0800895 var sticky = false;
896 var hiding = false;
897 var $stickyEl = $('#sticky-header');
898 var $menuEl = $('.menu-container');
899 //var scrollThrottle = -1;
900 var lastScroll = 0;
901 var autoScrolling = false;
902
Scott Main20cf2a92014-04-02 21:57:20 -0700903 var prevScrollLeft = 0; // used to compare current position to previous position of horiz scroll
Dirk Dougherty08032402014-02-15 10:14:35 -0800904
Scott Main20cf2a92014-04-02 21:57:20 -0700905 $(window).scroll(function() {
906 // Exit if there's no sidenav
907 if ($('#side-nav').length == 0) return;
908 // Exit if the mouse target is a DIV, because that means the event is coming
909 // from a scrollable div and so there's no need to make adjustments to our layout
910 if (event.target.nodeName == "DIV") {
911 return;
912 }
913
914
915 var top = $(window).scrollTop();
916 // we set the navbar fixed when the scroll position is beyond the height of the site header...
917 var shouldBeSticky = top >= stickyTop;
918 // ... except if the document content is shorter than the sidenav height.
919 // (this is necessary to avoid crazy behavior on OSX Lion due to overscroll bouncing)
920 if ($("#doc-col").height() < $("#side-nav").height()) {
921 shouldBeSticky = false;
922 }
923
924 // Don't continue if the header is sufficently far away
925 // (to avoid intensive resizing that slows scrolling)
926 if (sticky && shouldBeSticky) {
927 return;
928 }
929
930 // Account for horizontal scroll
931 var scrollLeft = $(window).scrollLeft();
932 // When the sidenav is fixed and user scrolls horizontally, reposition the sidenav to match
933 if (navBarIsFixed && (scrollLeft != prevScrollLeft)) {
934 updateSideNavPosition();
935 prevScrollLeft = scrollLeft;
936 }
937
938 // If sticky header visible and position is now near top, hide sticky
939 if (sticky && !shouldBeSticky) {
Dirk Dougherty08032402014-02-15 10:14:35 -0800940 sticky = false;
941 hiding = true;
Scott Main20cf2a92014-04-02 21:57:20 -0700942 // make the sidenav static again
943 $('#devdoc-nav')
944 .removeClass('fixed')
945 .css({'width':'auto','margin':''})
946 .prependTo('#side-nav');
947 // delay hide the sticky
948 $menuEl.removeClass('sticky-menu');
949 $stickyEl.fadeOut(250);
950 hiding = false;
951
952 // update the sidenaav position for side scrolling
953 updateSideNavPosition();
954 } else if (!sticky && shouldBeSticky) {
Dirk Dougherty08032402014-02-15 10:14:35 -0800955 sticky = true;
Scott Main20cf2a92014-04-02 21:57:20 -0700956 $stickyEl.fadeIn(10);
Dirk Dougherty08032402014-02-15 10:14:35 -0800957 $menuEl.addClass('sticky-menu');
958
Dirk Dougherty08032402014-02-15 10:14:35 -0800959
960 // If its a jump then make sure to modify the scroll because of the
961 // sticky nav
962 if (!autoScrolling && Math.abs(top - lastScroll > 100)) {
963 autoScrolling = true;
964 $('body,html').animate({scrollTop:(top = top - 60)}, '250', 'swing', function() { autoScrolling = false; });
965 }
Scott Main20cf2a92014-04-02 21:57:20 -0700966
967 // make the sidenav fixed
968 var width = $('#devdoc-nav').width();
969 $('#devdoc-nav')
970 .addClass('fixed')
971 .css({'width':width+'px'})
972 .prependTo('#body-content');
973
974 // update the sidenaav position for side scrolling
975 updateSideNavPosition();
976
Dirk Dougherty08032402014-02-15 10:14:35 -0800977 } else if (hiding && top < 15) {
978 $menuEl.removeClass('sticky-menu');
979 $stickyEl.hide();
980 hiding = false;
981 }
982
983 lastScroll = top;
Scott Main20cf2a92014-04-02 21:57:20 -0700984 resizeNav(250); // pass true in order to delay the scrollbar re-initialization for performance
Dirk Dougherty08032402014-02-15 10:14:35 -0800985 });
986
987 // Stack hover states
988 $('.section-card-menu').each(function(index, el) {
989 var height = $(el).height();
990 $(el).css({height:height+'px', position:'relative'});
991 var $cardInfo = $(el).find('.card-info');
992
993 $cardInfo.css({position: 'absolute', bottom:'0px', left:'0px', right:'0px', overflow:'visible'});
994 });
995
Scott Main20cf2a92014-04-02 21:57:20 -0700996 resizeNav(); // must resize once loading is finished
Dirk Dougherty08032402014-02-15 10:14:35 -0800997 });
998
999})();
Dirk Dougherty541b4942014-02-14 18:31:53 -08001000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014/* MISC LIBRARY FUNCTIONS */
1015
1016
1017
1018
1019
1020function toggle(obj, slide) {
1021 var ul = $("ul:first", obj);
1022 var li = ul.parent();
1023 if (li.hasClass("closed")) {
1024 if (slide) {
1025 ul.slideDown("fast");
1026 } else {
1027 ul.show();
1028 }
1029 li.removeClass("closed");
1030 li.addClass("open");
1031 $(".toggle-img", li).attr("title", "hide pages");
1032 } else {
1033 ul.slideUp("fast");
1034 li.removeClass("open");
1035 li.addClass("closed");
1036 $(".toggle-img", li).attr("title", "show pages");
1037 }
1038}
1039
1040
1041function buildToggleLists() {
1042 $(".toggle-list").each(
1043 function(i) {
1044 $("div:first", this).append("<a class='toggle-img' href='#' title='show pages' onClick='toggle(this.parentNode.parentNode, true); return false;'></a>");
1045 $(this).addClass("closed");
1046 });
1047}
1048
1049
1050
1051function hideNestedItems(list, toggle) {
1052 $list = $(list);
1053 // hide nested lists
1054 if($list.hasClass('showing')) {
1055 $("li ol", $list).hide('fast');
1056 $list.removeClass('showing');
1057 // show nested lists
1058 } else {
1059 $("li ol", $list).show('fast');
1060 $list.addClass('showing');
1061 }
1062 $(".more,.less",$(toggle)).toggle();
1063}
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092/* REFERENCE NAV SWAP */
1093
1094
1095function getNavPref() {
1096 var v = readCookie('reference_nav');
1097 if (v != NAV_PREF_TREE) {
1098 v = NAV_PREF_PANELS;
1099 }
1100 return v;
1101}
1102
1103function chooseDefaultNav() {
1104 nav_pref = getNavPref();
1105 if (nav_pref == NAV_PREF_TREE) {
1106 $("#nav-panels").toggle();
1107 $("#panel-link").toggle();
1108 $("#nav-tree").toggle();
1109 $("#tree-link").toggle();
1110 }
1111}
1112
1113function swapNav() {
1114 if (nav_pref == NAV_PREF_TREE) {
1115 nav_pref = NAV_PREF_PANELS;
1116 } else {
1117 nav_pref = NAV_PREF_TREE;
1118 init_default_navtree(toRoot);
1119 }
1120 var date = new Date();
1121 date.setTime(date.getTime()+(10*365*24*60*60*1000)); // keep this for 10 years
1122 writeCookie("nav", nav_pref, "reference", date.toGMTString());
1123
1124 $("#nav-panels").toggle();
1125 $("#panel-link").toggle();
1126 $("#nav-tree").toggle();
1127 $("#tree-link").toggle();
1128
1129 resizeNav();
1130
1131 // Gross nasty hack to make tree view show up upon first swap by setting height manually
1132 $("#nav-tree .jspContainer:visible")
1133 .css({'height':$("#nav-tree .jspContainer .jspPane").height() +'px'});
1134 // Another nasty hack to make the scrollbar appear now that we have height
1135 resizeNav();
1136
1137 if ($("#nav-tree").is(':visible')) {
1138 scrollIntoView("nav-tree");
1139 } else {
1140 scrollIntoView("packages-nav");
1141 scrollIntoView("classes-nav");
1142 }
1143}
1144
1145
1146
1147/* ############################################ */
1148/* ########## LOCALIZATION ############ */
1149/* ############################################ */
1150
1151function getBaseUri(uri) {
1152 var intlUrl = (uri.substring(0,6) == "/intl/");
1153 if (intlUrl) {
1154 base = uri.substring(uri.indexOf('intl/')+5,uri.length);
1155 base = base.substring(base.indexOf('/')+1, base.length);
1156 //alert("intl, returning base url: /" + base);
1157 return ("/" + base);
1158 } else {
1159 //alert("not intl, returning uri as found.");
1160 return uri;
1161 }
1162}
1163
1164function requestAppendHL(uri) {
1165//append "?hl=<lang> to an outgoing request (such as to blog)
1166 var lang = getLangPref();
1167 if (lang) {
1168 var q = 'hl=' + lang;
1169 uri += '?' + q;
1170 window.location = uri;
1171 return false;
1172 } else {
1173 return true;
1174 }
1175}
1176
1177
1178function changeNavLang(lang) {
1179 var $links = $("#devdoc-nav,#header,#nav-x,.training-nav-top,.content-footer").find("a["+lang+"-lang]");
1180 $links.each(function(i){ // for each link with a translation
1181 var $link = $(this);
1182 if (lang != "en") { // No need to worry about English, because a language change invokes new request
1183 // put the desired language from the attribute as the text
1184 $link.text($link.attr(lang+"-lang"))
1185 }
1186 });
1187}
1188
1189function changeLangPref(lang, submit) {
1190 var date = new Date();
1191 expires = date.toGMTString(date.setTime(date.getTime()+(10*365*24*60*60*1000)));
1192 // keep this for 50 years
1193 //alert("expires: " + expires)
1194 writeCookie("pref_lang", lang, null, expires);
1195
1196 // ####### TODO: Remove this condition once we're stable on devsite #######
1197 // This condition is only needed if we still need to support legacy GAE server
1198 if (devsite) {
1199 // Switch language when on Devsite server
1200 if (submit) {
1201 $("#setlang").submit();
1202 }
1203 } else {
1204 // Switch language when on legacy GAE server
1205 if (submit) {
1206 window.location = getBaseUri(location.pathname);
1207 }
1208 }
1209}
1210
1211function loadLangPref() {
1212 var lang = readCookie("pref_lang");
1213 if (lang != 0) {
1214 $("#language").find("option[value='"+lang+"']").attr("selected",true);
1215 }
1216}
1217
1218function getLangPref() {
1219 var lang = $("#language").find(":selected").attr("value");
1220 if (!lang) {
1221 lang = readCookie("pref_lang");
1222 }
1223 return (lang != 0) ? lang : 'en';
1224}
1225
1226/* ########## END LOCALIZATION ############ */
1227
1228
1229
1230
1231
1232
1233/* Used to hide and reveal supplemental content, such as long code samples.
1234 See the companion CSS in android-developer-docs.css */
1235function toggleContent(obj) {
1236 var div = $(obj).closest(".toggle-content");
1237 var toggleMe = $(".toggle-content-toggleme:eq(0)",div);
1238 if (div.hasClass("closed")) { // if it's closed, open it
1239 toggleMe.slideDown();
1240 $(".toggle-content-text:eq(0)", obj).toggle();
1241 div.removeClass("closed").addClass("open");
1242 $(".toggle-content-img:eq(0)", div).attr("title", "hide").attr("src", toRoot
1243 + "assets/images/triangle-opened.png");
1244 } else { // if it's open, close it
1245 toggleMe.slideUp('fast', function() { // Wait until the animation is done before closing arrow
1246 $(".toggle-content-text:eq(0)", obj).toggle();
1247 div.removeClass("open").addClass("closed");
1248 div.find(".toggle-content").removeClass("open").addClass("closed")
1249 .find(".toggle-content-toggleme").hide();
1250 $(".toggle-content-img", div).attr("title", "show").attr("src", toRoot
1251 + "assets/images/triangle-closed.png");
1252 });
1253 }
1254 return false;
1255}
1256
1257
1258/* New version of expandable content */
1259function toggleExpandable(link,id) {
1260 if($(id).is(':visible')) {
1261 $(id).slideUp();
1262 $(link).removeClass('expanded');
1263 } else {
1264 $(id).slideDown();
1265 $(link).addClass('expanded');
1266 }
1267}
1268
1269function hideExpandable(ids) {
1270 $(ids).slideUp();
1271 $(ids).prev('h4').find('a.expandable').removeClass('expanded');
1272}
1273
1274
1275
1276
1277
1278/*
1279 * Slideshow 1.0
1280 * Used on /index.html and /develop/index.html for carousel
1281 *
1282 * Sample usage:
1283 * HTML -
1284 * <div class="slideshow-container">
1285 * <a href="" class="slideshow-prev">Prev</a>
1286 * <a href="" class="slideshow-next">Next</a>
1287 * <ul>
1288 * <li class="item"><img src="images/marquee1.jpg"></li>
1289 * <li class="item"><img src="images/marquee2.jpg"></li>
1290 * <li class="item"><img src="images/marquee3.jpg"></li>
1291 * <li class="item"><img src="images/marquee4.jpg"></li>
1292 * </ul>
1293 * </div>
1294 *
1295 * <script type="text/javascript">
1296 * $('.slideshow-container').dacSlideshow({
1297 * auto: true,
1298 * btnPrev: '.slideshow-prev',
1299 * btnNext: '.slideshow-next'
1300 * });
1301 * </script>
1302 *
1303 * Options:
1304 * btnPrev: optional identifier for previous button
1305 * btnNext: optional identifier for next button
1306 * btnPause: optional identifier for pause button
1307 * auto: whether or not to auto-proceed
1308 * speed: animation speed
1309 * autoTime: time between auto-rotation
1310 * easing: easing function for transition
1311 * start: item to select by default
1312 * scroll: direction to scroll in
1313 * pagination: whether or not to include dotted pagination
1314 *
1315 */
1316
1317 (function($) {
1318 $.fn.dacSlideshow = function(o) {
1319
1320 //Options - see above
1321 o = $.extend({
1322 btnPrev: null,
1323 btnNext: null,
1324 btnPause: null,
1325 auto: true,
1326 speed: 500,
1327 autoTime: 12000,
1328 easing: null,
1329 start: 0,
1330 scroll: 1,
1331 pagination: true
1332
1333 }, o || {});
1334
1335 //Set up a carousel for each
1336 return this.each(function() {
1337
1338 var running = false;
1339 var animCss = o.vertical ? "top" : "left";
1340 var sizeCss = o.vertical ? "height" : "width";
1341 var div = $(this);
1342 var ul = $("ul", div);
1343 var tLi = $("li", ul);
1344 var tl = tLi.size();
1345 var timer = null;
1346
1347 var li = $("li", ul);
1348 var itemLength = li.size();
1349 var curr = o.start;
1350
1351 li.css({float: o.vertical ? "none" : "left"});
1352 ul.css({margin: "0", padding: "0", position: "relative", "list-style-type": "none", "z-index": "1"});
1353 div.css({position: "relative", "z-index": "2", left: "0px"});
1354
1355 var liSize = o.vertical ? height(li) : width(li);
1356 var ulSize = liSize * itemLength;
1357 var divSize = liSize;
1358
1359 li.css({width: li.width(), height: li.height()});
1360 ul.css(sizeCss, ulSize+"px").css(animCss, -(curr*liSize));
1361
1362 div.css(sizeCss, divSize+"px");
1363
1364 //Pagination
1365 if (o.pagination) {
1366 var pagination = $("<div class='pagination'></div>");
1367 var pag_ul = $("<ul></ul>");
1368 if (tl > 1) {
1369 for (var i=0;i<tl;i++) {
1370 var li = $("<li>"+i+"</li>");
1371 pag_ul.append(li);
1372 if (i==o.start) li.addClass('active');
1373 li.click(function() {
1374 go(parseInt($(this).text()));
1375 })
1376 }
1377 pagination.append(pag_ul);
1378 div.append(pagination);
1379 }
1380 }
1381
1382 //Previous button
1383 if(o.btnPrev)
1384 $(o.btnPrev).click(function(e) {
1385 e.preventDefault();
1386 return go(curr-o.scroll);
1387 });
1388
1389 //Next button
1390 if(o.btnNext)
1391 $(o.btnNext).click(function(e) {
1392 e.preventDefault();
1393 return go(curr+o.scroll);
1394 });
1395
1396 //Pause button
1397 if(o.btnPause)
1398 $(o.btnPause).click(function(e) {
1399 e.preventDefault();
1400 if ($(this).hasClass('paused')) {
1401 startRotateTimer();
1402 } else {
1403 pauseRotateTimer();
1404 }
1405 });
1406
1407 //Auto rotation
1408 if(o.auto) startRotateTimer();
1409
1410 function startRotateTimer() {
1411 clearInterval(timer);
1412 timer = setInterval(function() {
1413 if (curr == tl-1) {
1414 go(0);
1415 } else {
1416 go(curr+o.scroll);
1417 }
1418 }, o.autoTime);
1419 $(o.btnPause).removeClass('paused');
1420 }
1421
1422 function pauseRotateTimer() {
1423 clearInterval(timer);
1424 $(o.btnPause).addClass('paused');
1425 }
1426
1427 //Go to an item
1428 function go(to) {
1429 if(!running) {
1430
1431 if(to<0) {
1432 to = itemLength-1;
1433 } else if (to>itemLength-1) {
1434 to = 0;
1435 }
1436 curr = to;
1437
1438 running = true;
1439
1440 ul.animate(
1441 animCss == "left" ? { left: -(curr*liSize) } : { top: -(curr*liSize) } , o.speed, o.easing,
1442 function() {
1443 running = false;
1444 }
1445 );
1446
1447 $(o.btnPrev + "," + o.btnNext).removeClass("disabled");
1448 $( (curr-o.scroll<0 && o.btnPrev)
1449 ||
1450 (curr+o.scroll > itemLength && o.btnNext)
1451 ||
1452 []
1453 ).addClass("disabled");
1454
1455
1456 var nav_items = $('li', pagination);
1457 nav_items.removeClass('active');
1458 nav_items.eq(to).addClass('active');
1459
1460
1461 }
1462 if(o.auto) startRotateTimer();
1463 return false;
1464 };
1465 });
1466 };
1467
1468 function css(el, prop) {
1469 return parseInt($.css(el[0], prop)) || 0;
1470 };
1471 function width(el) {
1472 return el[0].offsetWidth + css(el, 'marginLeft') + css(el, 'marginRight');
1473 };
1474 function height(el) {
1475 return el[0].offsetHeight + css(el, 'marginTop') + css(el, 'marginBottom');
1476 };
1477
1478 })(jQuery);
1479
1480
1481/*
1482 * dacSlideshow 1.0
1483 * Used on develop/index.html for side-sliding tabs
1484 *
1485 * Sample usage:
1486 * HTML -
1487 * <div class="slideshow-container">
1488 * <a href="" class="slideshow-prev">Prev</a>
1489 * <a href="" class="slideshow-next">Next</a>
1490 * <ul>
1491 * <li class="item"><img src="images/marquee1.jpg"></li>
1492 * <li class="item"><img src="images/marquee2.jpg"></li>
1493 * <li class="item"><img src="images/marquee3.jpg"></li>
1494 * <li class="item"><img src="images/marquee4.jpg"></li>
1495 * </ul>
1496 * </div>
1497 *
1498 * <script type="text/javascript">
1499 * $('.slideshow-container').dacSlideshow({
1500 * auto: true,
1501 * btnPrev: '.slideshow-prev',
1502 * btnNext: '.slideshow-next'
1503 * });
1504 * </script>
1505 *
1506 * Options:
1507 * btnPrev: optional identifier for previous button
1508 * btnNext: optional identifier for next button
1509 * auto: whether or not to auto-proceed
1510 * speed: animation speed
1511 * autoTime: time between auto-rotation
1512 * easing: easing function for transition
1513 * start: item to select by default
1514 * scroll: direction to scroll in
1515 * pagination: whether or not to include dotted pagination
1516 *
1517 */
1518 (function($) {
1519 $.fn.dacTabbedList = function(o) {
1520
1521 //Options - see above
1522 o = $.extend({
1523 speed : 250,
1524 easing: null,
1525 nav_id: null,
1526 frame_id: null
1527 }, o || {});
1528
1529 //Set up a carousel for each
1530 return this.each(function() {
1531
1532 var curr = 0;
1533 var running = false;
1534 var animCss = "margin-left";
1535 var sizeCss = "width";
1536 var div = $(this);
1537
1538 var nav = $(o.nav_id, div);
1539 var nav_li = $("li", nav);
1540 var nav_size = nav_li.size();
1541 var frame = div.find(o.frame_id);
1542 var content_width = $(frame).find('ul').width();
1543 //Buttons
1544 $(nav_li).click(function(e) {
1545 go($(nav_li).index($(this)));
1546 })
1547
1548 //Go to an item
1549 function go(to) {
1550 if(!running) {
1551 curr = to;
1552 running = true;
1553
1554 frame.animate({ 'margin-left' : -(curr*content_width) }, o.speed, o.easing,
1555 function() {
1556 running = false;
1557 }
1558 );
1559
1560
1561 nav_li.removeClass('active');
1562 nav_li.eq(to).addClass('active');
1563
1564
1565 }
1566 return false;
1567 };
1568 });
1569 };
1570
1571 function css(el, prop) {
1572 return parseInt($.css(el[0], prop)) || 0;
1573 };
1574 function width(el) {
1575 return el[0].offsetWidth + css(el, 'marginLeft') + css(el, 'marginRight');
1576 };
1577 function height(el) {
1578 return el[0].offsetHeight + css(el, 'marginTop') + css(el, 'marginBottom');
1579 };
1580
1581 })(jQuery);
1582
1583
1584
1585
1586
1587/* ######################################################## */
1588/* ################ SEARCH SUGGESTIONS ################## */
1589/* ######################################################## */
1590
1591
1592
1593var gSelectedIndex = -1; // the index position of currently highlighted suggestion
1594var gSelectedColumn = -1; // which column of suggestion lists is currently focused
1595
1596var gMatches = new Array();
1597var gLastText = "";
1598var gInitialized = false;
1599var ROW_COUNT_FRAMEWORK = 20; // max number of results in list
1600var gListLength = 0;
1601
1602
1603var gGoogleMatches = new Array();
1604var ROW_COUNT_GOOGLE = 15; // max number of results in list
1605var gGoogleListLength = 0;
1606
1607var gDocsMatches = new Array();
1608var ROW_COUNT_DOCS = 100; // max number of results in list
1609var gDocsListLength = 0;
1610
1611function onSuggestionClick(link) {
1612 // When user clicks a suggested document, track it
1613 _gaq.push(['_trackEvent', 'Suggestion Click', 'clicked: ' + $(link).text(),
1614 'from: ' + $("#search_autocomplete").val()]);
1615}
1616
1617function set_item_selected($li, selected)
1618{
1619 if (selected) {
1620 $li.attr('class','jd-autocomplete jd-selected');
1621 } else {
1622 $li.attr('class','jd-autocomplete');
1623 }
1624}
1625
1626function set_item_values(toroot, $li, match)
1627{
1628 var $link = $('a',$li);
1629 $link.html(match.__hilabel || match.label);
1630 $link.attr('href',toroot + match.link);
1631}
1632
1633function set_item_values_jd(toroot, $li, match)
1634{
1635 var $link = $('a',$li);
1636 $link.html(match.title);
1637 $link.attr('href',toroot + match.url);
1638}
1639
1640function new_suggestion($list) {
1641 var $li = $("<li class='jd-autocomplete'></li>");
1642 $list.append($li);
1643
1644 $li.mousedown(function() {
1645 window.location = this.firstChild.getAttribute("href");
1646 });
1647 $li.mouseover(function() {
1648 $('.search_filtered_wrapper li').removeClass('jd-selected');
1649 $(this).addClass('jd-selected');
1650 gSelectedColumn = $(".search_filtered:visible").index($(this).closest('.search_filtered'));
1651 gSelectedIndex = $("li", $(".search_filtered:visible")[gSelectedColumn]).index(this);
1652 });
1653 $li.append("<a onclick='onSuggestionClick(this)'></a>");
1654 $li.attr('class','show-item');
1655 return $li;
1656}
1657
1658function sync_selection_table(toroot)
1659{
1660 var $li; //list item jquery object
1661 var i; //list item iterator
1662
1663 // if there are NO results at all, hide all columns
1664 if (!(gMatches.length > 0) && !(gGoogleMatches.length > 0) && !(gDocsMatches.length > 0)) {
1665 $('.suggest-card').hide(300);
1666 return;
1667 }
1668
1669 // if there are api results
1670 if ((gMatches.length > 0) || (gGoogleMatches.length > 0)) {
1671 // reveal suggestion list
1672 $('.suggest-card.dummy').show();
1673 $('.suggest-card.reference').show();
1674 var listIndex = 0; // list index position
1675
1676 // reset the lists
1677 $(".search_filtered_wrapper.reference li").remove();
1678
1679 // ########### ANDROID RESULTS #############
1680 if (gMatches.length > 0) {
1681
1682 // determine android results to show
1683 gListLength = gMatches.length < ROW_COUNT_FRAMEWORK ?
1684 gMatches.length : ROW_COUNT_FRAMEWORK;
1685 for (i=0; i<gListLength; i++) {
1686 var $li = new_suggestion($(".suggest-card.reference ul"));
1687 set_item_values(toroot, $li, gMatches[i]);
1688 set_item_selected($li, i == gSelectedIndex);
1689 }
1690 }
1691
1692 // ########### GOOGLE RESULTS #############
1693 if (gGoogleMatches.length > 0) {
1694 // show header for list
1695 $(".suggest-card.reference ul").append("<li class='header'>in Google Services:</li>");
1696
1697 // determine google results to show
1698 gGoogleListLength = gGoogleMatches.length < ROW_COUNT_GOOGLE ? gGoogleMatches.length : ROW_COUNT_GOOGLE;
1699 for (i=0; i<gGoogleListLength; i++) {
1700 var $li = new_suggestion($(".suggest-card.reference ul"));
1701 set_item_values(toroot, $li, gGoogleMatches[i]);
1702 set_item_selected($li, i == gSelectedIndex);
1703 }
1704 }
1705 } else {
1706 $('.suggest-card.reference').hide();
1707 $('.suggest-card.dummy').hide();
1708 }
1709
1710 // ########### JD DOC RESULTS #############
1711 if (gDocsMatches.length > 0) {
1712 // reset the lists
1713 $(".search_filtered_wrapper.docs li").remove();
1714
1715 // determine google results to show
1716 // NOTE: The order of the conditions below for the sugg.type MUST BE SPECIFIC:
1717 // The order must match the reverse order that each section appears as a card in
1718 // the suggestion UI... this may be only for the "develop" grouped items though.
1719 gDocsListLength = gDocsMatches.length < ROW_COUNT_DOCS ? gDocsMatches.length : ROW_COUNT_DOCS;
1720 for (i=0; i<gDocsListLength; i++) {
1721 var sugg = gDocsMatches[i];
1722 var $li;
1723 if (sugg.type == "design") {
1724 $li = new_suggestion($(".suggest-card.design ul"));
1725 } else
1726 if (sugg.type == "distribute") {
1727 $li = new_suggestion($(".suggest-card.distribute ul"));
1728 } else
1729 if (sugg.type == "samples") {
1730 $li = new_suggestion($(".suggest-card.develop .child-card.samples"));
1731 } else
1732 if (sugg.type == "training") {
1733 $li = new_suggestion($(".suggest-card.develop .child-card.training"));
1734 } else
1735 if (sugg.type == "about"||"guide"||"tools"||"google") {
1736 $li = new_suggestion($(".suggest-card.develop .child-card.guides"));
1737 } else {
1738 continue;
1739 }
1740
1741 set_item_values_jd(toroot, $li, sugg);
1742 set_item_selected($li, i == gSelectedIndex);
1743 }
1744
1745 // add heading and show or hide card
1746 if ($(".suggest-card.design li").length > 0) {
1747 $(".suggest-card.design ul").prepend("<li class='header'>Design:</li>");
1748 $(".suggest-card.design").show(300);
1749 } else {
1750 $('.suggest-card.design').hide(300);
1751 }
1752 if ($(".suggest-card.distribute li").length > 0) {
1753 $(".suggest-card.distribute ul").prepend("<li class='header'>Distribute:</li>");
1754 $(".suggest-card.distribute").show(300);
1755 } else {
1756 $('.suggest-card.distribute').hide(300);
1757 }
1758 if ($(".child-card.guides li").length > 0) {
1759 $(".child-card.guides").prepend("<li class='header'>Guides:</li>");
1760 $(".child-card.guides li").appendTo(".suggest-card.develop ul");
1761 }
1762 if ($(".child-card.training li").length > 0) {
1763 $(".child-card.training").prepend("<li class='header'>Training:</li>");
1764 $(".child-card.training li").appendTo(".suggest-card.develop ul");
1765 }
1766 if ($(".child-card.samples li").length > 0) {
1767 $(".child-card.samples").prepend("<li class='header'>Samples:</li>");
1768 $(".child-card.samples li").appendTo(".suggest-card.develop ul");
1769 }
1770
1771 if ($(".suggest-card.develop li").length > 0) {
1772 $(".suggest-card.develop").show(300);
1773 } else {
1774 $('.suggest-card.develop').hide(300);
1775 }
1776
1777 } else {
1778 $('.search_filtered_wrapper.docs .suggest-card:not(.dummy)').hide(300);
1779 }
1780}
1781
1782/** Called by the search input's onkeydown and onkeyup events.
1783 * Handles navigation with keyboard arrows, Enter key to invoke search,
1784 * otherwise invokes search suggestions on key-up event.
1785 * @param e The JS event
1786 * @param kd True if the event is key-down
1787 * @param toroot A string for the site's root path
1788 * @returns True if the event should bubble up
1789 */
1790function search_changed(e, kd, toroot)
1791{
1792 var currentLang = getLangPref();
1793 var search = document.getElementById("search_autocomplete");
1794 var text = search.value.replace(/(^ +)|( +$)/g, '');
1795 // get the ul hosting the currently selected item
1796 gSelectedColumn = gSelectedColumn >= 0 ? gSelectedColumn : 0;
1797 var $columns = $(".search_filtered_wrapper").find(".search_filtered:visible");
1798 var $selectedUl = $columns[gSelectedColumn];
1799
1800 // show/hide the close button
1801 if (text != '') {
1802 $(".search .close").removeClass("hide");
1803 } else {
1804 $(".search .close").addClass("hide");
1805 }
1806 // 27 = esc
1807 if (e.keyCode == 27) {
1808 // close all search results
1809 if (kd) $('.search .close').trigger('click');
1810 return true;
1811 }
1812 // 13 = enter
1813 else if (e.keyCode == 13) {
1814 if (gSelectedIndex < 0) {
1815 $('.suggest-card').hide();
1816 if ($("#searchResults").is(":hidden") && (search.value != "")) {
1817 // if results aren't showing (and text not empty), return true to allow search to execute
Dirk Dougherty08032402014-02-15 10:14:35 -08001818 $('body,html').animate({scrollTop:0}, '500', 'swing', function() { autoScrolling = false; });
Dirk Dougherty541b4942014-02-14 18:31:53 -08001819 return true;
1820 } else {
1821 // otherwise, results are already showing, so allow ajax to auto refresh the results
1822 // and ignore this Enter press to avoid the reload.
1823 return false;
1824 }
1825 } else if (kd && gSelectedIndex >= 0) {
1826 // click the link corresponding to selected item
1827 $("a",$("li",$selectedUl)[gSelectedIndex]).get()[0].click();
1828 return false;
1829 }
1830 }
1831 // Stop here if Google results are showing
1832 else if ($("#searchResults").is(":visible")) {
1833 return true;
1834 }
1835 // 38 UP ARROW
1836 else if (kd && (e.keyCode == 38)) {
1837 // if the next item is a header, skip it
1838 if ($($("li", $selectedUl)[gSelectedIndex-1]).hasClass("header")) {
1839 gSelectedIndex--;
1840 }
1841 if (gSelectedIndex >= 0) {
1842 $('li', $selectedUl).removeClass('jd-selected');
1843 gSelectedIndex--;
1844 $('li:nth-child('+(gSelectedIndex+1)+')', $selectedUl).addClass('jd-selected');
1845 // If user reaches top, reset selected column
1846 if (gSelectedIndex < 0) {
1847 gSelectedColumn = -1;
1848 }
1849 }
1850 return false;
1851 }
1852 // 40 DOWN ARROW
1853 else if (kd && (e.keyCode == 40)) {
1854 // if the next item is a header, skip it
1855 if ($($("li", $selectedUl)[gSelectedIndex+1]).hasClass("header")) {
1856 gSelectedIndex++;
1857 }
1858 if ((gSelectedIndex < $("li", $selectedUl).length-1) ||
1859 ($($("li", $selectedUl)[gSelectedIndex+1]).hasClass("header"))) {
1860 $('li', $selectedUl).removeClass('jd-selected');
1861 gSelectedIndex++;
1862 $('li:nth-child('+(gSelectedIndex+1)+')', $selectedUl).addClass('jd-selected');
1863 }
1864 return false;
1865 }
1866 // Consider left/right arrow navigation
1867 // NOTE: Order of suggest columns are reverse order (index position 0 is on right)
1868 else if (kd && $columns.length > 1 && gSelectedColumn >= 0) {
1869 // 37 LEFT ARROW
1870 // go left only if current column is not left-most column (last column)
1871 if (e.keyCode == 37 && gSelectedColumn < $columns.length - 1) {
1872 $('li', $selectedUl).removeClass('jd-selected');
1873 gSelectedColumn++;
1874 $selectedUl = $columns[gSelectedColumn];
1875 // keep or reset the selected item to last item as appropriate
1876 gSelectedIndex = gSelectedIndex >
1877 $("li", $selectedUl).length-1 ?
1878 $("li", $selectedUl).length-1 : gSelectedIndex;
1879 // if the corresponding item is a header, move down
1880 if ($($("li", $selectedUl)[gSelectedIndex]).hasClass("header")) {
1881 gSelectedIndex++;
1882 }
1883 // set item selected
1884 $('li:nth-child('+(gSelectedIndex+1)+')', $selectedUl).addClass('jd-selected');
1885 return false;
1886 }
1887 // 39 RIGHT ARROW
1888 // go right only if current column is not the right-most column (first column)
1889 else if (e.keyCode == 39 && gSelectedColumn > 0) {
1890 $('li', $selectedUl).removeClass('jd-selected');
1891 gSelectedColumn--;
1892 $selectedUl = $columns[gSelectedColumn];
1893 // keep or reset the selected item to last item as appropriate
1894 gSelectedIndex = gSelectedIndex >
1895 $("li", $selectedUl).length-1 ?
1896 $("li", $selectedUl).length-1 : gSelectedIndex;
1897 // if the corresponding item is a header, move down
1898 if ($($("li", $selectedUl)[gSelectedIndex]).hasClass("header")) {
1899 gSelectedIndex++;
1900 }
1901 // set item selected
1902 $('li:nth-child('+(gSelectedIndex+1)+')', $selectedUl).addClass('jd-selected');
1903 return false;
1904 }
1905 }
1906
1907 // if key-up event and not arrow down/up/left/right,
1908 // read the search query and add suggestions to gMatches
1909 else if (!kd && (e.keyCode != 40)
1910 && (e.keyCode != 38)
1911 && (e.keyCode != 37)
1912 && (e.keyCode != 39)) {
1913 gSelectedIndex = -1;
1914 gMatches = new Array();
1915 matchedCount = 0;
1916 gGoogleMatches = new Array();
1917 matchedCountGoogle = 0;
1918 gDocsMatches = new Array();
1919 matchedCountDocs = 0;
1920
1921 // Search for Android matches
1922 for (var i=0; i<DATA.length; i++) {
1923 var s = DATA[i];
1924 if (text.length != 0 &&
1925 s.label.toLowerCase().indexOf(text.toLowerCase()) != -1) {
1926 gMatches[matchedCount] = s;
1927 matchedCount++;
1928 }
1929 }
1930 rank_autocomplete_api_results(text, gMatches);
1931 for (var i=0; i<gMatches.length; i++) {
1932 var s = gMatches[i];
1933 }
1934
1935
1936 // Search for Google matches
1937 for (var i=0; i<GOOGLE_DATA.length; i++) {
1938 var s = GOOGLE_DATA[i];
1939 if (text.length != 0 &&
1940 s.label.toLowerCase().indexOf(text.toLowerCase()) != -1) {
1941 gGoogleMatches[matchedCountGoogle] = s;
1942 matchedCountGoogle++;
1943 }
1944 }
1945 rank_autocomplete_api_results(text, gGoogleMatches);
1946 for (var i=0; i<gGoogleMatches.length; i++) {
1947 var s = gGoogleMatches[i];
1948 }
1949
1950 highlight_autocomplete_result_labels(text);
1951
1952
1953
1954 // Search for matching JD docs
1955 if (text.length >= 3) {
1956 // Regex to match only the beginning of a word
1957 var textRegex = new RegExp("\\b" + text.toLowerCase(), "g");
1958
1959
1960 // Search for Training classes
1961 for (var i=0; i<TRAINING_RESOURCES.length; i++) {
1962 // current search comparison, with counters for tag and title,
1963 // used later to improve ranking
1964 var s = TRAINING_RESOURCES[i];
1965 s.matched_tag = 0;
1966 s.matched_title = 0;
1967 var matched = false;
1968
1969 // Check if query matches any tags; work backwards toward 1 to assist ranking
1970 for (var j = s.keywords.length - 1; j >= 0; j--) {
1971 // it matches a tag
1972 if (s.keywords[j].toLowerCase().match(textRegex)) {
1973 matched = true;
1974 s.matched_tag = j + 1; // add 1 to index position
1975 }
1976 }
1977 // Don't consider doc title for lessons (only for class landing pages),
1978 // unless the lesson has a tag that already matches
1979 if ((s.lang == currentLang) &&
1980 (!(s.type == "training" && s.url.indexOf("index.html") == -1) || matched)) {
1981 // it matches the doc title
1982 if (s.title.toLowerCase().match(textRegex)) {
1983 matched = true;
1984 s.matched_title = 1;
1985 }
1986 }
1987 if (matched) {
1988 gDocsMatches[matchedCountDocs] = s;
1989 matchedCountDocs++;
1990 }
1991 }
1992
1993
1994 // Search for API Guides
1995 for (var i=0; i<GUIDE_RESOURCES.length; i++) {
1996 // current search comparison, with counters for tag and title,
1997 // used later to improve ranking
1998 var s = GUIDE_RESOURCES[i];
1999 s.matched_tag = 0;
2000 s.matched_title = 0;
2001 var matched = false;
2002
2003 // Check if query matches any tags; work backwards toward 1 to assist ranking
2004 for (var j = s.keywords.length - 1; j >= 0; j--) {
2005 // it matches a tag
2006 if (s.keywords[j].toLowerCase().match(textRegex)) {
2007 matched = true;
2008 s.matched_tag = j + 1; // add 1 to index position
2009 }
2010 }
2011 // Check if query matches the doc title, but only for current language
2012 if (s.lang == currentLang) {
2013 // if query matches the doc title
2014 if (s.title.toLowerCase().match(textRegex)) {
2015 matched = true;
2016 s.matched_title = 1;
2017 }
2018 }
2019 if (matched) {
2020 gDocsMatches[matchedCountDocs] = s;
2021 matchedCountDocs++;
2022 }
2023 }
2024
2025
2026 // Search for Tools Guides
2027 for (var i=0; i<TOOLS_RESOURCES.length; i++) {
2028 // current search comparison, with counters for tag and title,
2029 // used later to improve ranking
2030 var s = TOOLS_RESOURCES[i];
2031 s.matched_tag = 0;
2032 s.matched_title = 0;
2033 var matched = false;
2034
2035 // Check if query matches any tags; work backwards toward 1 to assist ranking
2036 for (var j = s.keywords.length - 1; j >= 0; j--) {
2037 // it matches a tag
2038 if (s.keywords[j].toLowerCase().match(textRegex)) {
2039 matched = true;
2040 s.matched_tag = j + 1; // add 1 to index position
2041 }
2042 }
2043 // Check if query matches the doc title, but only for current language
2044 if (s.lang == currentLang) {
2045 // if query matches the doc title
2046 if (s.title.toLowerCase().match(textRegex)) {
2047 matched = true;
2048 s.matched_title = 1;
2049 }
2050 }
2051 if (matched) {
2052 gDocsMatches[matchedCountDocs] = s;
2053 matchedCountDocs++;
2054 }
2055 }
2056
2057
2058 // Search for About docs
2059 for (var i=0; i<ABOUT_RESOURCES.length; i++) {
2060 // current search comparison, with counters for tag and title,
2061 // used later to improve ranking
2062 var s = ABOUT_RESOURCES[i];
2063 s.matched_tag = 0;
2064 s.matched_title = 0;
2065 var matched = false;
2066
2067 // Check if query matches any tags; work backwards toward 1 to assist ranking
2068 for (var j = s.keywords.length - 1; j >= 0; j--) {
2069 // it matches a tag
2070 if (s.keywords[j].toLowerCase().match(textRegex)) {
2071 matched = true;
2072 s.matched_tag = j + 1; // add 1 to index position
2073 }
2074 }
2075 // Check if query matches the doc title, but only for current language
2076 if (s.lang == currentLang) {
2077 // if query matches the doc title
2078 if (s.title.toLowerCase().match(textRegex)) {
2079 matched = true;
2080 s.matched_title = 1;
2081 }
2082 }
2083 if (matched) {
2084 gDocsMatches[matchedCountDocs] = s;
2085 matchedCountDocs++;
2086 }
2087 }
2088
2089
2090 // Search for Design guides
2091 for (var i=0; i<DESIGN_RESOURCES.length; i++) {
2092 // current search comparison, with counters for tag and title,
2093 // used later to improve ranking
2094 var s = DESIGN_RESOURCES[i];
2095 s.matched_tag = 0;
2096 s.matched_title = 0;
2097 var matched = false;
2098
2099 // Check if query matches any tags; work backwards toward 1 to assist ranking
2100 for (var j = s.keywords.length - 1; j >= 0; j--) {
2101 // it matches a tag
2102 if (s.keywords[j].toLowerCase().match(textRegex)) {
2103 matched = true;
2104 s.matched_tag = j + 1; // add 1 to index position
2105 }
2106 }
2107 // Check if query matches the doc title, but only for current language
2108 if (s.lang == currentLang) {
2109 // if query matches the doc title
2110 if (s.title.toLowerCase().match(textRegex)) {
2111 matched = true;
2112 s.matched_title = 1;
2113 }
2114 }
2115 if (matched) {
2116 gDocsMatches[matchedCountDocs] = s;
2117 matchedCountDocs++;
2118 }
2119 }
2120
2121
2122 // Search for Distribute guides
2123 for (var i=0; i<DISTRIBUTE_RESOURCES.length; i++) {
2124 // current search comparison, with counters for tag and title,
2125 // used later to improve ranking
2126 var s = DISTRIBUTE_RESOURCES[i];
2127 s.matched_tag = 0;
2128 s.matched_title = 0;
2129 var matched = false;
2130
2131 // Check if query matches any tags; work backwards toward 1 to assist ranking
2132 for (var j = s.keywords.length - 1; j >= 0; j--) {
2133 // it matches a tag
2134 if (s.keywords[j].toLowerCase().match(textRegex)) {
2135 matched = true;
2136 s.matched_tag = j + 1; // add 1 to index position
2137 }
2138 }
2139 // Check if query matches the doc title, but only for current language
2140 if (s.lang == currentLang) {
2141 // if query matches the doc title
2142 if (s.title.toLowerCase().match(textRegex)) {
2143 matched = true;
2144 s.matched_title = 1;
2145 }
2146 }
2147 if (matched) {
2148 gDocsMatches[matchedCountDocs] = s;
2149 matchedCountDocs++;
2150 }
2151 }
2152
2153
2154 // Search for Google guides
2155 for (var i=0; i<GOOGLE_RESOURCES.length; i++) {
2156 // current search comparison, with counters for tag and title,
2157 // used later to improve ranking
2158 var s = GOOGLE_RESOURCES[i];
2159 s.matched_tag = 0;
2160 s.matched_title = 0;
2161 var matched = false;
2162
2163 // Check if query matches any tags; work backwards toward 1 to assist ranking
2164 for (var j = s.keywords.length - 1; j >= 0; j--) {
2165 // it matches a tag
2166 if (s.keywords[j].toLowerCase().match(textRegex)) {
2167 matched = true;
2168 s.matched_tag = j + 1; // add 1 to index position
2169 }
2170 }
2171 // Check if query matches the doc title, but only for current language
2172 if (s.lang == currentLang) {
2173 // if query matches the doc title
2174 if (s.title.toLowerCase().match(textRegex)) {
2175 matched = true;
2176 s.matched_title = 1;
2177 }
2178 }
2179 if (matched) {
2180 gDocsMatches[matchedCountDocs] = s;
2181 matchedCountDocs++;
2182 }
2183 }
2184
2185
2186 // Search for Samples
2187 for (var i=0; i<SAMPLES_RESOURCES.length; i++) {
2188 // current search comparison, with counters for tag and title,
2189 // used later to improve ranking
2190 var s = SAMPLES_RESOURCES[i];
2191 s.matched_tag = 0;
2192 s.matched_title = 0;
2193 var matched = false;
2194 // Check if query matches any tags; work backwards toward 1 to assist ranking
2195 for (var j = s.keywords.length - 1; j >= 0; j--) {
2196 // it matches a tag
2197 if (s.keywords[j].toLowerCase().match(textRegex)) {
2198 matched = true;
2199 s.matched_tag = j + 1; // add 1 to index position
2200 }
2201 }
2202 // Check if query matches the doc title, but only for current language
2203 if (s.lang == currentLang) {
2204 // if query matches the doc title.t
2205 if (s.title.toLowerCase().match(textRegex)) {
2206 matched = true;
2207 s.matched_title = 1;
2208 }
2209 }
2210 if (matched) {
2211 gDocsMatches[matchedCountDocs] = s;
2212 matchedCountDocs++;
2213 }
2214 }
2215
2216 // Rank/sort all the matched pages
2217 rank_autocomplete_doc_results(text, gDocsMatches);
2218 }
2219
2220 // draw the suggestions
2221 sync_selection_table(toroot);
2222 return true; // allow the event to bubble up to the search api
2223 }
2224}
2225
2226/* Order the jd doc result list based on match quality */
2227function rank_autocomplete_doc_results(query, matches) {
2228 query = query || '';
2229 if (!matches || !matches.length)
2230 return;
2231
2232 var _resultScoreFn = function(match) {
2233 var score = 1.0;
2234
2235 // if the query matched a tag
2236 if (match.matched_tag > 0) {
2237 // multiply score by factor relative to position in tags list (max of 3)
2238 score *= 3 / match.matched_tag;
2239
2240 // if it also matched the title
2241 if (match.matched_title > 0) {
2242 score *= 2;
2243 }
2244 } else if (match.matched_title > 0) {
2245 score *= 3;
2246 }
2247
2248 return score;
2249 };
2250
2251 for (var i=0; i<matches.length; i++) {
2252 matches[i].__resultScore = _resultScoreFn(matches[i]);
2253 }
2254
2255 matches.sort(function(a,b){
2256 var n = b.__resultScore - a.__resultScore;
2257 if (n == 0) // lexicographical sort if scores are the same
2258 n = (a.label < b.label) ? -1 : 1;
2259 return n;
2260 });
2261}
2262
2263/* Order the result list based on match quality */
2264function rank_autocomplete_api_results(query, matches) {
2265 query = query || '';
2266 if (!matches || !matches.length)
2267 return;
2268
2269 // helper function that gets the last occurence index of the given regex
2270 // in the given string, or -1 if not found
2271 var _lastSearch = function(s, re) {
2272 if (s == '')
2273 return -1;
2274 var l = -1;
2275 var tmp;
2276 while ((tmp = s.search(re)) >= 0) {
2277 if (l < 0) l = 0;
2278 l += tmp;
2279 s = s.substr(tmp + 1);
2280 }
2281 return l;
2282 };
2283
2284 // helper function that counts the occurrences of a given character in
2285 // a given string
2286 var _countChar = function(s, c) {
2287 var n = 0;
2288 for (var i=0; i<s.length; i++)
2289 if (s.charAt(i) == c) ++n;
2290 return n;
2291 };
2292
2293 var queryLower = query.toLowerCase();
2294 var queryAlnum = (queryLower.match(/\w+/) || [''])[0];
2295 var partPrefixAlnumRE = new RegExp('\\b' + queryAlnum);
2296 var partExactAlnumRE = new RegExp('\\b' + queryAlnum + '\\b');
2297
2298 var _resultScoreFn = function(result) {
2299 // scores are calculated based on exact and prefix matches,
2300 // and then number of path separators (dots) from the last
2301 // match (i.e. favoring classes and deep package names)
2302 var score = 1.0;
2303 var labelLower = result.label.toLowerCase();
2304 var t;
2305 t = _lastSearch(labelLower, partExactAlnumRE);
2306 if (t >= 0) {
2307 // exact part match
2308 var partsAfter = _countChar(labelLower.substr(t + 1), '.');
2309 score *= 200 / (partsAfter + 1);
2310 } else {
2311 t = _lastSearch(labelLower, partPrefixAlnumRE);
2312 if (t >= 0) {
2313 // part prefix match
2314 var partsAfter = _countChar(labelLower.substr(t + 1), '.');
2315 score *= 20 / (partsAfter + 1);
2316 }
2317 }
2318
2319 return score;
2320 };
2321
2322 for (var i=0; i<matches.length; i++) {
2323 // if the API is deprecated, default score is 0; otherwise, perform scoring
2324 if (matches[i].deprecated == "true") {
2325 matches[i].__resultScore = 0;
2326 } else {
2327 matches[i].__resultScore = _resultScoreFn(matches[i]);
2328 }
2329 }
2330
2331 matches.sort(function(a,b){
2332 var n = b.__resultScore - a.__resultScore;
2333 if (n == 0) // lexicographical sort if scores are the same
2334 n = (a.label < b.label) ? -1 : 1;
2335 return n;
2336 });
2337}
2338
2339/* Add emphasis to part of string that matches query */
2340function highlight_autocomplete_result_labels(query) {
2341 query = query || '';
2342 if ((!gMatches || !gMatches.length) && (!gGoogleMatches || !gGoogleMatches.length))
2343 return;
2344
2345 var queryLower = query.toLowerCase();
2346 var queryAlnumDot = (queryLower.match(/[\w\.]+/) || [''])[0];
2347 var queryRE = new RegExp(
2348 '(' + queryAlnumDot.replace(/\./g, '\\.') + ')', 'ig');
2349 for (var i=0; i<gMatches.length; i++) {
2350 gMatches[i].__hilabel = gMatches[i].label.replace(
2351 queryRE, '<b>$1</b>');
2352 }
2353 for (var i=0; i<gGoogleMatches.length; i++) {
2354 gGoogleMatches[i].__hilabel = gGoogleMatches[i].label.replace(
2355 queryRE, '<b>$1</b>');
2356 }
2357}
2358
2359function search_focus_changed(obj, focused)
2360{
2361 if (!focused) {
2362 if(obj.value == ""){
2363 $(".search .close").addClass("hide");
2364 }
2365 $(".suggest-card").hide();
2366 }
2367}
2368
2369function submit_search() {
2370 var query = document.getElementById('search_autocomplete').value;
2371 location.hash = 'q=' + query;
2372 loadSearchResults();
Scott Maind6a8e662014-04-12 16:40:48 -07002373 $("#searchResults").slideDown('slow', setStickyTop);
Dirk Dougherty541b4942014-02-14 18:31:53 -08002374 return false;
2375}
2376
2377
2378function hideResults() {
Scott Maind6a8e662014-04-12 16:40:48 -07002379 $("#searchResults").slideUp('fast', setStickyTop);
Dirk Dougherty541b4942014-02-14 18:31:53 -08002380 $(".search .close").addClass("hide");
2381 location.hash = '';
2382
2383 $("#search_autocomplete").val("").blur();
2384
2385 // reset the ajax search callback to nothing, so results don't appear unless ENTER
2386 searchControl.setSearchStartingCallback(this, function(control, searcher, query) {});
2387
2388 // forcefully regain key-up event control (previously jacked by search api)
2389 $("#search_autocomplete").keyup(function(event) {
2390 return search_changed(event, false, toRoot);
2391 });
2392
2393 return false;
2394}
2395
2396
2397
2398/* ########################################################## */
2399/* ################ CUSTOM SEARCH ENGINE ################## */
2400/* ########################################################## */
2401
2402var searchControl;
2403google.load('search', '1', {"callback" : function() {
2404 searchControl = new google.search.SearchControl();
2405 } });
2406
2407function loadSearchResults() {
2408 document.getElementById("search_autocomplete").style.color = "#000";
2409
2410 searchControl = new google.search.SearchControl();
2411
2412 // use our existing search form and use tabs when multiple searchers are used
2413 drawOptions = new google.search.DrawOptions();
2414 drawOptions.setDrawMode(google.search.SearchControl.DRAW_MODE_TABBED);
2415 drawOptions.setInput(document.getElementById("search_autocomplete"));
2416
2417 // configure search result options
2418 searchOptions = new google.search.SearcherOptions();
2419 searchOptions.setExpandMode(GSearchControl.EXPAND_MODE_OPEN);
2420
2421 // configure each of the searchers, for each tab
2422 devSiteSearcher = new google.search.WebSearch();
2423 devSiteSearcher.setUserDefinedLabel("All");
2424 devSiteSearcher.setSiteRestriction("001482626316274216503:zu90b7s047u");
2425
2426 designSearcher = new google.search.WebSearch();
2427 designSearcher.setUserDefinedLabel("Design");
2428 designSearcher.setSiteRestriction("http://developer.android.com/design/");
2429
2430 trainingSearcher = new google.search.WebSearch();
2431 trainingSearcher.setUserDefinedLabel("Training");
2432 trainingSearcher.setSiteRestriction("http://developer.android.com/training/");
2433
2434 guidesSearcher = new google.search.WebSearch();
2435 guidesSearcher.setUserDefinedLabel("Guides");
2436 guidesSearcher.setSiteRestriction("http://developer.android.com/guide/");
2437
2438 referenceSearcher = new google.search.WebSearch();
2439 referenceSearcher.setUserDefinedLabel("Reference");
2440 referenceSearcher.setSiteRestriction("http://developer.android.com/reference/");
2441
2442 googleSearcher = new google.search.WebSearch();
2443 googleSearcher.setUserDefinedLabel("Google Services");
2444 googleSearcher.setSiteRestriction("http://developer.android.com/google/");
2445
2446 blogSearcher = new google.search.WebSearch();
2447 blogSearcher.setUserDefinedLabel("Blog");
2448 blogSearcher.setSiteRestriction("http://android-developers.blogspot.com");
2449
2450 // add each searcher to the search control
2451 searchControl.addSearcher(devSiteSearcher, searchOptions);
2452 searchControl.addSearcher(designSearcher, searchOptions);
2453 searchControl.addSearcher(trainingSearcher, searchOptions);
2454 searchControl.addSearcher(guidesSearcher, searchOptions);
2455 searchControl.addSearcher(referenceSearcher, searchOptions);
2456 searchControl.addSearcher(googleSearcher, searchOptions);
2457 searchControl.addSearcher(blogSearcher, searchOptions);
2458
2459 // configure result options
2460 searchControl.setResultSetSize(google.search.Search.LARGE_RESULTSET);
2461 searchControl.setLinkTarget(google.search.Search.LINK_TARGET_SELF);
2462 searchControl.setTimeoutInterval(google.search.SearchControl.TIMEOUT_SHORT);
2463 searchControl.setNoResultsString(google.search.SearchControl.NO_RESULTS_DEFAULT_STRING);
2464
2465 // upon ajax search, refresh the url and search title
2466 searchControl.setSearchStartingCallback(this, function(control, searcher, query) {
2467 updateResultTitle(query);
2468 var query = document.getElementById('search_autocomplete').value;
2469 location.hash = 'q=' + query;
2470 });
2471
2472 // once search results load, set up click listeners
2473 searchControl.setSearchCompleteCallback(this, function(control, searcher, query) {
2474 addResultClickListeners();
2475 });
2476
2477 // draw the search results box
2478 searchControl.draw(document.getElementById("leftSearchControl"), drawOptions);
2479
2480 // get query and execute the search
2481 searchControl.execute(decodeURI(getQuery(location.hash)));
2482
2483 document.getElementById("search_autocomplete").focus();
2484 addTabListeners();
2485}
2486// End of loadSearchResults
2487
2488
2489google.setOnLoadCallback(function(){
2490 if (location.hash.indexOf("q=") == -1) {
2491 // if there's no query in the url, don't search and make sure results are hidden
2492 $('#searchResults').hide();
2493 return;
2494 } else {
2495 // first time loading search results for this page
Scott Maind6a8e662014-04-12 16:40:48 -07002496 $('#searchResults').slideDown('slow', setStickyTop);
Dirk Dougherty541b4942014-02-14 18:31:53 -08002497 $(".search .close").removeClass("hide");
2498 loadSearchResults();
2499 }
2500}, true);
2501
2502// when an event on the browser history occurs (back, forward, load) requery hash and do search
2503$(window).hashchange( function(){
2504 // Exit if the hash isn't a search query or there's an error in the query
2505 if ((location.hash.indexOf("q=") == -1) || (query == "undefined")) {
2506 // If the results pane is open, close it.
2507 if (!$("#searchResults").is(":hidden")) {
2508 hideResults();
2509 }
2510 return;
2511 }
2512
2513 // Otherwise, we have a search to do
2514 var query = decodeURI(getQuery(location.hash));
2515 searchControl.execute(query);
Scott Maind6a8e662014-04-12 16:40:48 -07002516 $('#searchResults').slideDown('slow', setStickyTop);
Dirk Dougherty541b4942014-02-14 18:31:53 -08002517 $("#search_autocomplete").focus();
2518 $(".search .close").removeClass("hide");
2519
2520 updateResultTitle(query);
2521});
2522
2523function updateResultTitle(query) {
2524 $("#searchTitle").html("Results for <em>" + escapeHTML(query) + "</em>");
2525}
2526
2527// forcefully regain key-up event control (previously jacked by search api)
2528$("#search_autocomplete").keyup(function(event) {
2529 return search_changed(event, false, toRoot);
2530});
2531
2532// add event listeners to each tab so we can track the browser history
2533function addTabListeners() {
2534 var tabHeaders = $(".gsc-tabHeader");
2535 for (var i = 0; i < tabHeaders.length; i++) {
2536 $(tabHeaders[i]).attr("id",i).click(function() {
2537 /*
2538 // make a copy of the page numbers for the search left pane
2539 setTimeout(function() {
2540 // remove any residual page numbers
2541 $('#searchResults .gsc-tabsArea .gsc-cursor-box.gs-bidi-start-align').remove();
2542 // move the page numbers to the left position; make a clone,
2543 // because the element is drawn to the DOM only once
2544 // and because we're going to remove it (previous line),
2545 // we need it to be available to move again as the user navigates
2546 $('#searchResults .gsc-webResult .gsc-cursor-box.gs-bidi-start-align:visible')
2547 .clone().appendTo('#searchResults .gsc-tabsArea');
2548 }, 200);
2549 */
2550 });
2551 }
2552 setTimeout(function(){$(tabHeaders[0]).click()},200);
2553}
2554
2555// add analytics tracking events to each result link
2556function addResultClickListeners() {
2557 $("#searchResults a.gs-title").each(function(index, link) {
2558 // When user clicks enter for Google search results, track it
2559 $(link).click(function() {
2560 _gaq.push(['_trackEvent', 'Google Click', 'clicked: ' + $(this).text(),
2561 'from: ' + $("#search_autocomplete").val()]);
2562 });
2563 });
2564}
2565
2566
2567function getQuery(hash) {
2568 var queryParts = hash.split('=');
2569 return queryParts[1];
2570}
2571
2572/* returns the given string with all HTML brackets converted to entities
2573 TODO: move this to the site's JS library */
2574function escapeHTML(string) {
2575 return string.replace(/</g,"&lt;")
2576 .replace(/>/g,"&gt;");
2577}
2578
2579
2580
2581
2582
2583
2584
2585/* ######################################################## */
2586/* ################# JAVADOC REFERENCE ################### */
2587/* ######################################################## */
2588
2589/* Initialize some droiddoc stuff, but only if we're in the reference */
2590if (location.pathname.indexOf("/reference") == 0) {
2591 if(!(location.pathname.indexOf("/reference-gms/packages.html") == 0)
2592 && !(location.pathname.indexOf("/reference-gcm/packages.html") == 0)
2593 && !(location.pathname.indexOf("/reference/com/google") == 0)) {
2594 $(document).ready(function() {
2595 // init available apis based on user pref
2596 changeApiLevel();
2597 initSidenavHeightResize()
2598 });
2599 }
2600}
2601
2602var API_LEVEL_COOKIE = "api_level";
2603var minLevel = 1;
2604var maxLevel = 1;
2605
2606/******* SIDENAV DIMENSIONS ************/
2607
2608 function initSidenavHeightResize() {
2609 // Change the drag bar size to nicely fit the scrollbar positions
2610 var $dragBar = $(".ui-resizable-s");
2611 $dragBar.css({'width': $dragBar.parent().width() - 5 + "px"});
2612
2613 $( "#resize-packages-nav" ).resizable({
2614 containment: "#nav-panels",
2615 handles: "s",
2616 alsoResize: "#packages-nav",
2617 resize: function(event, ui) { resizeNav(); }, /* resize the nav while dragging */
2618 stop: function(event, ui) { saveNavPanels(); } /* once stopped, save the sizes to cookie */
2619 });
2620
2621 }
2622
2623function updateSidenavFixedWidth() {
2624 if (!navBarIsFixed) return;
2625 $('#devdoc-nav').css({
2626 'width' : $('#side-nav').css('width'),
2627 'margin' : $('#side-nav').css('margin')
2628 });
2629 $('#devdoc-nav a.totop').css({'display':'block','width':$("#nav").innerWidth()+'px'});
2630
2631 initSidenavHeightResize();
2632}
2633
2634function updateSidenavFullscreenWidth() {
2635 if (!navBarIsFixed) return;
2636 $('#devdoc-nav').css({
2637 'width' : $('#side-nav').css('width'),
2638 'margin' : $('#side-nav').css('margin')
2639 });
2640 $('#devdoc-nav .totop').css({'left': 'inherit'});
2641
2642 initSidenavHeightResize();
2643}
2644
2645function buildApiLevelSelector() {
2646 maxLevel = SINCE_DATA.length;
2647 var userApiLevel = parseInt(readCookie(API_LEVEL_COOKIE));
2648 userApiLevel = userApiLevel == 0 ? maxLevel : userApiLevel; // If there's no cookie (zero), use the max by default
2649
2650 minLevel = parseInt($("#doc-api-level").attr("class"));
2651 // Handle provisional api levels; the provisional level will always be the highest possible level
2652 // Provisional api levels will also have a length; other stuff that's just missing a level won't,
2653 // so leave those kinds of entities at the default level of 1 (for example, the R.styleable class)
2654 if (isNaN(minLevel) && minLevel.length) {
2655 minLevel = maxLevel;
2656 }
2657 var select = $("#apiLevelSelector").html("").change(changeApiLevel);
2658 for (var i = maxLevel-1; i >= 0; i--) {
2659 var option = $("<option />").attr("value",""+SINCE_DATA[i]).append(""+SINCE_DATA[i]);
2660 // if (SINCE_DATA[i] < minLevel) option.addClass("absent"); // always false for strings (codenames)
2661 select.append(option);
2662 }
2663
2664 // get the DOM element and use setAttribute cuz IE6 fails when using jquery .attr('selected',true)
2665 var selectedLevelItem = $("#apiLevelSelector option[value='"+userApiLevel+"']").get(0);
2666 selectedLevelItem.setAttribute('selected',true);
2667}
2668
2669function changeApiLevel() {
2670 maxLevel = SINCE_DATA.length;
2671 var selectedLevel = maxLevel;
2672
2673 selectedLevel = parseInt($("#apiLevelSelector option:selected").val());
2674 toggleVisisbleApis(selectedLevel, "body");
2675
2676 var date = new Date();
2677 date.setTime(date.getTime()+(10*365*24*60*60*1000)); // keep this for 10 years
2678 var expiration = date.toGMTString();
2679 writeCookie(API_LEVEL_COOKIE, selectedLevel, null, expiration);
2680
2681 if (selectedLevel < minLevel) {
2682 var thing = ($("#jd-header").html().indexOf("package") != -1) ? "package" : "class";
2683 $("#naMessage").show().html("<div><p><strong>This " + thing
2684 + " requires API level " + minLevel + " or higher.</strong></p>"
2685 + "<p>This document is hidden because your selected API level for the documentation is "
2686 + selectedLevel + ". You can change the documentation API level with the selector "
2687 + "above the left navigation.</p>"
2688 + "<p>For more information about specifying the API level your app requires, "
2689 + "read <a href='" + toRoot + "training/basics/supporting-devices/platforms.html'"
2690 + ">Supporting Different Platform Versions</a>.</p>"
2691 + "<input type='button' value='OK, make this page visible' "
2692 + "title='Change the API level to " + minLevel + "' "
2693 + "onclick='$(\"#apiLevelSelector\").val(\"" + minLevel + "\");changeApiLevel();' />"
2694 + "</div>");
2695 } else {
2696 $("#naMessage").hide();
2697 }
2698}
2699
2700function toggleVisisbleApis(selectedLevel, context) {
2701 var apis = $(".api",context);
2702 apis.each(function(i) {
2703 var obj = $(this);
2704 var className = obj.attr("class");
2705 var apiLevelIndex = className.lastIndexOf("-")+1;
2706 var apiLevelEndIndex = className.indexOf(" ", apiLevelIndex);
2707 apiLevelEndIndex = apiLevelEndIndex != -1 ? apiLevelEndIndex : className.length;
2708 var apiLevel = className.substring(apiLevelIndex, apiLevelEndIndex);
2709 if (apiLevel.length == 0) { // for odd cases when the since data is actually missing, just bail
2710 return;
2711 }
2712 apiLevel = parseInt(apiLevel);
2713
2714 // Handle provisional api levels; if this item's level is the provisional one, set it to the max
2715 var selectedLevelNum = parseInt(selectedLevel)
2716 var apiLevelNum = parseInt(apiLevel);
2717 if (isNaN(apiLevelNum)) {
2718 apiLevelNum = maxLevel;
2719 }
2720
2721 // Grey things out that aren't available and give a tooltip title
2722 if (apiLevelNum > selectedLevelNum) {
2723 obj.addClass("absent").attr("title","Requires API Level \""
2724 + apiLevel + "\" or higher. To reveal, change the target API level "
2725 + "above the left navigation.");
2726 }
2727 else obj.removeClass("absent").removeAttr("title");
2728 });
2729}
2730
2731
2732
2733
2734/* ################# SIDENAV TREE VIEW ################### */
2735
2736function new_node(me, mom, text, link, children_data, api_level)
2737{
2738 var node = new Object();
2739 node.children = Array();
2740 node.children_data = children_data;
2741 node.depth = mom.depth + 1;
2742
2743 node.li = document.createElement("li");
2744 mom.get_children_ul().appendChild(node.li);
2745
2746 node.label_div = document.createElement("div");
2747 node.label_div.className = "label";
2748 if (api_level != null) {
2749 $(node.label_div).addClass("api");
2750 $(node.label_div).addClass("api-level-"+api_level);
2751 }
2752 node.li.appendChild(node.label_div);
2753
2754 if (children_data != null) {
2755 node.expand_toggle = document.createElement("a");
2756 node.expand_toggle.href = "javascript:void(0)";
2757 node.expand_toggle.onclick = function() {
2758 if (node.expanded) {
2759 $(node.get_children_ul()).slideUp("fast");
2760 node.plus_img.src = me.toroot + "assets/images/triangle-closed-small.png";
2761 node.expanded = false;
2762 } else {
2763 expand_node(me, node);
2764 }
2765 };
2766 node.label_div.appendChild(node.expand_toggle);
2767
2768 node.plus_img = document.createElement("img");
2769 node.plus_img.src = me.toroot + "assets/images/triangle-closed-small.png";
2770 node.plus_img.className = "plus";
2771 node.plus_img.width = "8";
2772 node.plus_img.border = "0";
2773 node.expand_toggle.appendChild(node.plus_img);
2774
2775 node.expanded = false;
2776 }
2777
2778 var a = document.createElement("a");
2779 node.label_div.appendChild(a);
2780 node.label = document.createTextNode(text);
2781 a.appendChild(node.label);
2782 if (link) {
2783 a.href = me.toroot + link;
2784 } else {
2785 if (children_data != null) {
2786 a.className = "nolink";
2787 a.href = "javascript:void(0)";
2788 a.onclick = node.expand_toggle.onclick;
2789 // This next line shouldn't be necessary. I'll buy a beer for the first
2790 // person who figures out how to remove this line and have the link
2791 // toggle shut on the first try. --joeo@android.com
2792 node.expanded = false;
2793 }
2794 }
2795
2796
2797 node.children_ul = null;
2798 node.get_children_ul = function() {
2799 if (!node.children_ul) {
2800 node.children_ul = document.createElement("ul");
2801 node.children_ul.className = "children_ul";
2802 node.children_ul.style.display = "none";
2803 node.li.appendChild(node.children_ul);
2804 }
2805 return node.children_ul;
2806 };
2807
2808 return node;
2809}
2810
2811
2812
2813
2814function expand_node(me, node)
2815{
2816 if (node.children_data && !node.expanded) {
2817 if (node.children_visited) {
2818 $(node.get_children_ul()).slideDown("fast");
2819 } else {
2820 get_node(me, node);
2821 if ($(node.label_div).hasClass("absent")) {
2822 $(node.get_children_ul()).addClass("absent");
2823 }
2824 $(node.get_children_ul()).slideDown("fast");
2825 }
2826 node.plus_img.src = me.toroot + "assets/images/triangle-opened-small.png";
2827 node.expanded = true;
2828
2829 // perform api level toggling because new nodes are new to the DOM
2830 var selectedLevel = $("#apiLevelSelector option:selected").val();
2831 toggleVisisbleApis(selectedLevel, "#side-nav");
2832 }
2833}
2834
2835function get_node(me, mom)
2836{
2837 mom.children_visited = true;
2838 for (var i in mom.children_data) {
2839 var node_data = mom.children_data[i];
2840 mom.children[i] = new_node(me, mom, node_data[0], node_data[1],
2841 node_data[2], node_data[3]);
2842 }
2843}
2844
2845function this_page_relative(toroot)
2846{
2847 var full = document.location.pathname;
2848 var file = "";
2849 if (toroot.substr(0, 1) == "/") {
2850 if (full.substr(0, toroot.length) == toroot) {
2851 return full.substr(toroot.length);
2852 } else {
2853 // the file isn't under toroot. Fail.
2854 return null;
2855 }
2856 } else {
2857 if (toroot != "./") {
2858 toroot = "./" + toroot;
2859 }
2860 do {
2861 if (toroot.substr(toroot.length-3, 3) == "../" || toroot == "./") {
2862 var pos = full.lastIndexOf("/");
2863 file = full.substr(pos) + file;
2864 full = full.substr(0, pos);
2865 toroot = toroot.substr(0, toroot.length-3);
2866 }
2867 } while (toroot != "" && toroot != "/");
2868 return file.substr(1);
2869 }
2870}
2871
2872function find_page(url, data)
2873{
2874 var nodes = data;
2875 var result = null;
2876 for (var i in nodes) {
2877 var d = nodes[i];
2878 if (d[1] == url) {
2879 return new Array(i);
2880 }
2881 else if (d[2] != null) {
2882 result = find_page(url, d[2]);
2883 if (result != null) {
2884 return (new Array(i).concat(result));
2885 }
2886 }
2887 }
2888 return null;
2889}
2890
2891function init_default_navtree(toroot) {
2892 // load json file for navtree data
2893 $.getScript(toRoot + 'navtree_data.js', function(data, textStatus, jqxhr) {
2894 // when the file is loaded, initialize the tree
2895 if(jqxhr.status === 200) {
2896 init_navtree("tree-list", toroot, NAVTREE_DATA);
2897 }
2898 });
2899
2900 // perform api level toggling because because the whole tree is new to the DOM
2901 var selectedLevel = $("#apiLevelSelector option:selected").val();
2902 toggleVisisbleApis(selectedLevel, "#side-nav");
2903}
2904
2905function init_navtree(navtree_id, toroot, root_nodes)
2906{
2907 var me = new Object();
2908 me.toroot = toroot;
2909 me.node = new Object();
2910
2911 me.node.li = document.getElementById(navtree_id);
2912 me.node.children_data = root_nodes;
2913 me.node.children = new Array();
2914 me.node.children_ul = document.createElement("ul");
2915 me.node.get_children_ul = function() { return me.node.children_ul; };
2916 //me.node.children_ul.className = "children_ul";
2917 me.node.li.appendChild(me.node.children_ul);
2918 me.node.depth = 0;
2919
2920 get_node(me, me.node);
2921
2922 me.this_page = this_page_relative(toroot);
2923 me.breadcrumbs = find_page(me.this_page, root_nodes);
2924 if (me.breadcrumbs != null && me.breadcrumbs.length != 0) {
2925 var mom = me.node;
2926 for (var i in me.breadcrumbs) {
2927 var j = me.breadcrumbs[i];
2928 mom = mom.children[j];
2929 expand_node(me, mom);
2930 }
2931 mom.label_div.className = mom.label_div.className + " selected";
2932 addLoadEvent(function() {
2933 scrollIntoView("nav-tree");
2934 });
2935 }
2936}
2937
2938
2939
2940
2941
2942
2943
2944
2945/* TODO: eliminate redundancy with non-google functions */
2946function init_google_navtree(navtree_id, toroot, root_nodes)
2947{
2948 var me = new Object();
2949 me.toroot = toroot;
2950 me.node = new Object();
2951
2952 me.node.li = document.getElementById(navtree_id);
2953 me.node.children_data = root_nodes;
2954 me.node.children = new Array();
2955 me.node.children_ul = document.createElement("ul");
2956 me.node.get_children_ul = function() { return me.node.children_ul; };
2957 //me.node.children_ul.className = "children_ul";
2958 me.node.li.appendChild(me.node.children_ul);
2959 me.node.depth = 0;
2960
2961 get_google_node(me, me.node);
2962}
2963
2964function new_google_node(me, mom, text, link, children_data, api_level)
2965{
2966 var node = new Object();
2967 var child;
2968 node.children = Array();
2969 node.children_data = children_data;
2970 node.depth = mom.depth + 1;
2971 node.get_children_ul = function() {
2972 if (!node.children_ul) {
2973 node.children_ul = document.createElement("ul");
2974 node.children_ul.className = "tree-list-children";
2975 node.li.appendChild(node.children_ul);
2976 }
2977 return node.children_ul;
2978 };
2979 node.li = document.createElement("li");
2980
2981 mom.get_children_ul().appendChild(node.li);
2982
2983
2984 if(link) {
2985 child = document.createElement("a");
2986
2987 }
2988 else {
2989 child = document.createElement("span");
2990 child.className = "tree-list-subtitle";
2991
2992 }
2993 if (children_data != null) {
2994 node.li.className="nav-section";
2995 node.label_div = document.createElement("div");
2996 node.label_div.className = "nav-section-header-ref";
2997 node.li.appendChild(node.label_div);
2998 get_google_node(me, node);
2999 node.label_div.appendChild(child);
3000 }
3001 else {
3002 node.li.appendChild(child);
3003 }
3004 if(link) {
3005 child.href = me.toroot + link;
3006 }
3007 node.label = document.createTextNode(text);
3008 child.appendChild(node.label);
3009
3010 node.children_ul = null;
3011
3012 return node;
3013}
3014
3015function get_google_node(me, mom)
3016{
3017 mom.children_visited = true;
3018 var linkText;
3019 for (var i in mom.children_data) {
3020 var node_data = mom.children_data[i];
3021 linkText = node_data[0];
3022
3023 if(linkText.match("^"+"com.google.android")=="com.google.android"){
3024 linkText = linkText.substr(19, linkText.length);
3025 }
3026 mom.children[i] = new_google_node(me, mom, linkText, node_data[1],
3027 node_data[2], node_data[3]);
3028 }
3029}
3030
3031
3032
3033
3034
3035
3036/****** NEW version of script to build google and sample navs dynamically ******/
3037// TODO: update Google reference docs to tolerate this new implementation
3038
3039var NODE_NAME = 0;
3040var NODE_HREF = 1;
3041var NODE_GROUP = 2;
3042var NODE_TAGS = 3;
3043var NODE_CHILDREN = 4;
3044
3045function init_google_navtree2(navtree_id, data)
3046{
3047 var $containerUl = $("#"+navtree_id);
3048 for (var i in data) {
3049 var node_data = data[i];
3050 $containerUl.append(new_google_node2(node_data));
3051 }
3052
3053 // Make all third-generation list items 'sticky' to prevent them from collapsing
3054 $containerUl.find('li li li.nav-section').addClass('sticky');
3055
3056 initExpandableNavItems("#"+navtree_id);
3057}
3058
3059function new_google_node2(node_data)
3060{
3061 var linkText = node_data[NODE_NAME];
3062 if(linkText.match("^"+"com.google.android")=="com.google.android"){
3063 linkText = linkText.substr(19, linkText.length);
3064 }
3065 var $li = $('<li>');
3066 var $a;
3067 if (node_data[NODE_HREF] != null) {
3068 $a = $('<a href="' + toRoot + node_data[NODE_HREF] + '" title="' + linkText + '" >'
3069 + linkText + '</a>');
3070 } else {
3071 $a = $('<a href="#" onclick="return false;" title="' + linkText + '" >'
3072 + linkText + '/</a>');
3073 }
3074 var $childUl = $('<ul>');
3075 if (node_data[NODE_CHILDREN] != null) {
3076 $li.addClass("nav-section");
3077 $a = $('<div class="nav-section-header">').append($a);
3078 if (node_data[NODE_HREF] == null) $a.addClass('empty');
3079
3080 for (var i in node_data[NODE_CHILDREN]) {
3081 var child_node_data = node_data[NODE_CHILDREN][i];
3082 $childUl.append(new_google_node2(child_node_data));
3083 }
3084 $li.append($childUl);
3085 }
3086 $li.prepend($a);
3087
3088 return $li;
3089}
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101function showGoogleRefTree() {
3102 init_default_google_navtree(toRoot);
3103 init_default_gcm_navtree(toRoot);
3104}
3105
3106function init_default_google_navtree(toroot) {
3107 // load json file for navtree data
3108 $.getScript(toRoot + 'gms_navtree_data.js', function(data, textStatus, jqxhr) {
3109 // when the file is loaded, initialize the tree
3110 if(jqxhr.status === 200) {
3111 init_google_navtree("gms-tree-list", toroot, GMS_NAVTREE_DATA);
3112 highlightSidenav();
3113 resizeNav();
3114 }
3115 });
3116}
3117
3118function init_default_gcm_navtree(toroot) {
3119 // load json file for navtree data
3120 $.getScript(toRoot + 'gcm_navtree_data.js', function(data, textStatus, jqxhr) {
3121 // when the file is loaded, initialize the tree
3122 if(jqxhr.status === 200) {
3123 init_google_navtree("gcm-tree-list", toroot, GCM_NAVTREE_DATA);
3124 highlightSidenav();
3125 resizeNav();
3126 }
3127 });
3128}
3129
3130function showSamplesRefTree() {
3131 init_default_samples_navtree(toRoot);
3132}
3133
3134function init_default_samples_navtree(toroot) {
3135 // load json file for navtree data
3136 $.getScript(toRoot + 'samples_navtree_data.js', function(data, textStatus, jqxhr) {
3137 // when the file is loaded, initialize the tree
3138 if(jqxhr.status === 200) {
3139 // hack to remove the "about the samples" link then put it back in
3140 // after we nuke the list to remove the dummy static list of samples
3141 var $firstLi = $("#nav.samples-nav > li:first-child").clone();
3142 $("#nav.samples-nav").empty();
3143 $("#nav.samples-nav").append($firstLi);
3144
3145 init_google_navtree2("nav.samples-nav", SAMPLES_NAVTREE_DATA);
3146 highlightSidenav();
3147 resizeNav();
3148 if ($("#jd-content #samples").length) {
3149 showSamples();
3150 }
3151 }
3152 });
3153}
3154
3155/* TOGGLE INHERITED MEMBERS */
3156
3157/* Toggle an inherited class (arrow toggle)
3158 * @param linkObj The link that was clicked.
3159 * @param expand 'true' to ensure it's expanded. 'false' to ensure it's closed.
3160 * 'null' to simply toggle.
3161 */
3162function toggleInherited(linkObj, expand) {
3163 var base = linkObj.getAttribute("id");
3164 var list = document.getElementById(base + "-list");
3165 var summary = document.getElementById(base + "-summary");
3166 var trigger = document.getElementById(base + "-trigger");
3167 var a = $(linkObj);
3168 if ( (expand == null && a.hasClass("closed")) || expand ) {
3169 list.style.display = "none";
3170 summary.style.display = "block";
3171 trigger.src = toRoot + "assets/images/triangle-opened.png";
3172 a.removeClass("closed");
3173 a.addClass("opened");
3174 } else if ( (expand == null && a.hasClass("opened")) || (expand == false) ) {
3175 list.style.display = "block";
3176 summary.style.display = "none";
3177 trigger.src = toRoot + "assets/images/triangle-closed.png";
3178 a.removeClass("opened");
3179 a.addClass("closed");
3180 }
3181 return false;
3182}
3183
3184/* Toggle all inherited classes in a single table (e.g. all inherited methods)
3185 * @param linkObj The link that was clicked.
3186 * @param expand 'true' to ensure it's expanded. 'false' to ensure it's closed.
3187 * 'null' to simply toggle.
3188 */
3189function toggleAllInherited(linkObj, expand) {
3190 var a = $(linkObj);
3191 var table = $(a.parent().parent().parent()); // ugly way to get table/tbody
3192 var expandos = $(".jd-expando-trigger", table);
3193 if ( (expand == null && a.text() == "[Expand]") || expand ) {
3194 expandos.each(function(i) {
3195 toggleInherited(this, true);
3196 });
3197 a.text("[Collapse]");
3198 } else if ( (expand == null && a.text() == "[Collapse]") || (expand == false) ) {
3199 expandos.each(function(i) {
3200 toggleInherited(this, false);
3201 });
3202 a.text("[Expand]");
3203 }
3204 return false;
3205}
3206
3207/* Toggle all inherited members in the class (link in the class title)
3208 */
3209function toggleAllClassInherited() {
3210 var a = $("#toggleAllClassInherited"); // get toggle link from class title
3211 var toggles = $(".toggle-all", $("#body-content"));
3212 if (a.text() == "[Expand All]") {
3213 toggles.each(function(i) {
3214 toggleAllInherited(this, true);
3215 });
3216 a.text("[Collapse All]");
3217 } else {
3218 toggles.each(function(i) {
3219 toggleAllInherited(this, false);
3220 });
3221 a.text("[Expand All]");
3222 }
3223 return false;
3224}
3225
3226/* Expand all inherited members in the class. Used when initiating page search */
3227function ensureAllInheritedExpanded() {
3228 var toggles = $(".toggle-all", $("#body-content"));
3229 toggles.each(function(i) {
3230 toggleAllInherited(this, true);
3231 });
3232 $("#toggleAllClassInherited").text("[Collapse All]");
3233}
3234
3235
3236/* HANDLE KEY EVENTS
3237 * - Listen for Ctrl+F (Cmd on Mac) and expand all inherited members (to aid page search)
3238 */
3239var agent = navigator['userAgent'].toLowerCase();
3240var mac = agent.indexOf("macintosh") != -1;
3241
3242$(document).keydown( function(e) {
3243var control = mac ? e.metaKey && !e.ctrlKey : e.ctrlKey; // get ctrl key
3244 if (control && e.which == 70) { // 70 is "F"
3245 ensureAllInheritedExpanded();
3246 }
3247});
3248
3249
3250
3251
3252
3253
3254/* On-demand functions */
3255
3256/** Move sample code line numbers out of PRE block and into non-copyable column */
3257function initCodeLineNumbers() {
3258 var numbers = $("#codesample-block a.number");
3259 if (numbers.length) {
3260 $("#codesample-line-numbers").removeClass("hidden").append(numbers);
3261 }
3262
3263 $(document).ready(function() {
3264 // select entire line when clicked
3265 $("span.code-line").click(function() {
3266 if (!shifted) {
3267 selectText(this);
3268 }
3269 });
3270 // invoke line link on double click
3271 $(".code-line").dblclick(function() {
3272 document.location.hash = $(this).attr('id');
3273 });
3274 // highlight the line when hovering on the number
3275 $("#codesample-line-numbers a.number").mouseover(function() {
3276 var id = $(this).attr('href');
3277 $(id).css('background','#e7e7e7');
3278 });
3279 $("#codesample-line-numbers a.number").mouseout(function() {
3280 var id = $(this).attr('href');
3281 $(id).css('background','none');
3282 });
3283 });
3284}
3285
3286// create SHIFT key binder to avoid the selectText method when selecting multiple lines
3287var shifted = false;
3288$(document).bind('keyup keydown', function(e){shifted = e.shiftKey; return true;} );
3289
3290// courtesy of jasonedelman.com
3291function selectText(element) {
3292 var doc = document
3293 , range, selection
3294 ;
3295 if (doc.body.createTextRange) { //ms
3296 range = doc.body.createTextRange();
3297 range.moveToElementText(element);
3298 range.select();
3299 } else if (window.getSelection) { //all others
3300 selection = window.getSelection();
3301 range = doc.createRange();
3302 range.selectNodeContents(element);
3303 selection.removeAllRanges();
3304 selection.addRange(range);
3305 }
3306}
3307
3308
3309
3310
3311/** Display links and other information about samples that match the
3312 group specified by the URL */
3313function showSamples() {
3314 var group = $("#samples").attr('class');
3315 $("#samples").html("<p>Here are some samples for <b>" + group + "</b> apps:</p>");
3316
3317 var $ul = $("<ul>");
3318 $selectedLi = $("#nav li.selected");
3319
3320 $selectedLi.children("ul").children("li").each(function() {
3321 var $li = $("<li>").append($(this).find("a").first().clone());
3322 $ul.append($li);
3323 });
3324
3325 $("#samples").append($ul);
3326
3327}
Dirk Dougherty08032402014-02-15 10:14:35 -08003328
3329
3330
3331/* ########################################################## */
3332/* ################### RESOURCE CARDS ##################### */
3333/* ########################################################## */
3334
3335/** Handle resource queries, collections, and grids (sections). Requires
3336 jd_tag_helpers.js and the *_unified_data.js to be loaded. */
3337
3338(function() {
3339 // Prevent the same resource from being loaded more than once per page.
3340 var addedPageResources = {};
3341
3342 $(document).ready(function() {
3343 $('.resource-widget').each(function() {
3344 initResourceWidget(this);
3345 });
3346
3347 // Might remove this, but adds ellipsis to card descriptions rather
3348 // than just cutting them off, not sure if it performs well
3349 $('.card-info .text').ellipsis();
3350 });
3351
3352 /*
3353 Three types of resource layouts:
3354 Flow - Uses a fixed row-height flow using float left style.
3355 Carousel - Single card slideshow all same dimension absoute.
3356 Stack - Uses fixed columns and flexible element height.
3357 */
3358 function initResourceWidget(widget) {
3359 var $widget = $(widget);
3360 var isFlow = $widget.hasClass('resource-flow-layout'),
3361 isCarousel = $widget.hasClass('resource-carousel-layout'),
3362 isStack = $widget.hasClass('resource-stack-layout');
3363
3364 // find size of widget by pulling out its class name
3365 var sizeCols = 1;
3366 var m = $widget.get(0).className.match(/\bcol-(\d+)\b/);
3367 if (m) {
3368 sizeCols = parseInt(m[1], 10);
3369 }
3370
3371 var opts = {
3372 cardSizes: ($widget.data('cardsizes') || '').split(','),
3373 maxResults: parseInt($widget.data('maxresults') || '100', 10),
3374 itemsPerPage: $widget.data('itemsperpage'),
3375 sortOrder: $widget.data('sortorder'),
3376 query: $widget.data('query'),
3377 section: $widget.data('section'),
3378 sizeCols: sizeCols
3379 };
3380
3381 // run the search for the set of resources to show
3382
3383 var resources = buildResourceList(opts);
3384
3385 if (isFlow) {
3386 drawResourcesFlowWidget($widget, opts, resources);
3387 } else if (isCarousel) {
3388 drawResourcesCarouselWidget($widget, opts, resources);
3389 } else if (isStack) {
3390 var sections = buildSectionList(opts);
3391 opts['numStacks'] = $widget.data('numstacks');
3392 drawResourcesStackWidget($widget, opts, resources, sections);
3393 }
3394 }
3395
3396 /* Initializes a Resource Carousel Widget */
3397 function drawResourcesCarouselWidget($widget, opts, resources) {
3398 $widget.empty();
3399
3400 $widget.addClass('resource-card slideshow-container')
3401 .append($('<a>').addClass('slideshow-prev').text('Prev'))
3402 .append($('<a>').addClass('slideshow-next').text('Next'));
3403
3404 var css = { 'width': $widget.width() + 'px',
3405 'height': $widget.height() + 'px' };
3406
3407 var $ul = $('<ul>');
3408
3409 for (var i = 0; i < resources.length; ++i) {
3410 //keep url clean for matching and offline mode handling
3411 var urlPrefix = resources[i].url.indexOf("//") > -1 ? "" : toRoot;
3412 var $card = $('<a>')
3413 .attr('href', urlPrefix + resources[i].url)
3414 .decorateResourceCard(resources[i]);
3415
3416 $('<li>').css(css)
3417 .append($card)
3418 .appendTo($ul);
3419 }
3420
3421 $('<div>').addClass('frame')
3422 .append($ul)
3423 .appendTo($widget);
3424
3425 $widget.dacSlideshow({
3426 auto: true,
3427 btnPrev: '.slideshow-prev',
3428 btnNext: '.slideshow-next'
3429 });
3430 };
3431
3432 /* Initializes a Resource Card Stack Widget (column-based layout) */
3433 function drawResourcesStackWidget($widget, opts, resources, sections) {
3434 // Don't empty widget, grab all items inside since they will be the first
3435 // items stacked, followed by the resource query
3436
3437 var cards = $widget.find('.resource-card').detach().toArray();
3438 var numStacks = opts.numStacks || 1;
3439 var $stacks = [];
3440 var urlString;
3441
3442 for (var i = 0; i < numStacks; ++i) {
3443 $stacks[i] = $('<div>').addClass('resource-card-stack')
3444 .appendTo($widget);
3445 }
3446
3447 var sectionResources = [];
3448
3449 // Extract any subsections that are actually resource cards
3450 for (var i = 0; i < sections.length; ++i) {
3451 if (!sections[i].sections || !sections[i].sections.length) {
3452 //keep url clean for matching and offline mode handling
3453 urlPrefix = sections[i].url.indexOf("//") > -1 ? "" : toRoot;
3454 // Render it as a resource card
3455
3456 sectionResources.push(
3457 $('<a>')
3458 .addClass('resource-card section-card')
3459 .attr('href', urlPrefix + sections[i].resource.url)
3460 .decorateResourceCard(sections[i].resource)[0]
3461 );
3462
3463 } else {
3464 cards.push(
3465 $('<div>')
3466 .addClass('resource-card section-card-menu')
3467 .decorateResourceSection(sections[i])[0]
3468 );
3469 }
3470 }
3471
3472 cards = cards.concat(sectionResources);
3473
3474 for (var i = 0; i < resources.length; ++i) {
3475 //keep url clean for matching and offline mode handling
3476 urlPrefix = resources[i].url.indexOf("//") > -1 ? "" : toRoot;
3477 var $card = $('<a>')
3478 .addClass('resource-card related-card')
3479 .attr('href', urlPrefix + resources[i].url)
3480 .decorateResourceCard(resources[i]);
3481
3482 cards.push($card[0]);
3483 }
3484
3485 for (var i = 0; i < cards.length; ++i) {
3486 // Find the stack with the shortest height, but give preference to
3487 // left to right order.
3488 var minHeight = $stacks[0].height();
3489 var minIndex = 0;
3490
3491 for (var j = 1; j < numStacks; ++j) {
3492 var height = $stacks[j].height();
3493 if (height < minHeight - 45) {
3494 minHeight = height;
3495 minIndex = j;
3496 }
3497 }
3498
3499 $stacks[minIndex].append($(cards[i]));
3500 }
3501
3502 };
3503
3504 /* Initializes a flow widget, see distribute.scss for generating accompanying css */
3505 function drawResourcesFlowWidget($widget, opts, resources) {
3506 $widget.empty();
3507 var cardSizes = opts.cardSizes || ['6x6'];
3508 var i = 0, j = 0;
3509
3510 while (i < resources.length) {
3511 var cardSize = cardSizes[j++ % cardSizes.length];
3512 cardSize = cardSize.replace(/^\s+|\s+$/,'');
3513
3514 // A stack has a third dimension which is the number of stacked items
3515 var isStack = cardSize.match(/(\d+)x(\d+)x(\d+)/);
3516 var stackCount = 0;
3517 var $stackDiv = null;
3518
3519 if (isStack) {
3520 // Create a stack container which should have the dimensions defined
3521 // by the product of the items inside.
3522 $stackDiv = $('<div>').addClass('resource-card-stack resource-card-' + isStack[1]
3523 + 'x' + isStack[2] * isStack[3]) .appendTo($widget);
3524 }
3525
3526 // Build each stack item or just a single item
3527 do {
3528 var resource = resources[i];
3529 //keep url clean for matching and offline mode handling
3530 urlPrefix = resource.url.indexOf("//") > -1 ? "" : toRoot;
3531 var $card = $('<a>')
3532 .addClass('resource-card resource-card-' + cardSize + ' resource-card-' + resource.type)
3533 .attr('href', urlPrefix + resource.url);
3534
3535 if (isStack) {
3536 $card.addClass('resource-card-' + isStack[1] + 'x' + isStack[2]);
3537 if (++stackCount == parseInt(isStack[3])) {
3538 $card.addClass('resource-card-row-stack-last');
3539 stackCount = 0;
3540 }
3541 } else {
3542 stackCount = 0;
3543 }
3544
3545 $card.decorateResourceCard(resource)
3546 .appendTo($stackDiv || $widget);
3547
3548 } while (++i < resources.length && stackCount > 0);
3549 }
3550 }
3551
3552 /* Build a site map of resources using a section as a root. */
3553 function buildSectionList(opts) {
3554 if (opts.section && SECTION_BY_ID[opts.section]) {
3555 return SECTION_BY_ID[opts.section].sections || [];
3556 }
3557 return [];
3558 }
3559
3560 function buildResourceList(opts) {
3561 var maxResults = opts.maxResults || 100;
3562
3563 var query = opts.query || '';
3564 var expressions = parseResourceQuery(query);
3565 var addedResourceIndices = {};
3566 var results = [];
3567
3568 for (var i = 0; i < expressions.length; i++) {
3569 var clauses = expressions[i];
3570
3571 // build initial set of resources from first clause
3572 var firstClause = clauses[0];
3573 var resources = [];
3574 switch (firstClause.attr) {
3575 case 'type':
3576 resources = ALL_RESOURCES_BY_TYPE[firstClause.value];
3577 break;
3578 case 'lang':
3579 resources = ALL_RESOURCES_BY_LANG[firstClause.value];
3580 break;
3581 case 'tag':
3582 resources = ALL_RESOURCES_BY_TAG[firstClause.value];
3583 break;
3584 case 'collection':
3585 var urls = RESOURCE_COLLECTIONS[firstClause.value].resources || [];
3586 resources = urls.map(function(url){ return ALL_RESOURCES_BY_URL[url]; });
3587 break;
3588 case 'section':
3589 var urls = SITE_MAP[firstClause.value].sections || [];
3590 resources = urls.map(function(url){ return ALL_RESOURCES_BY_URL[url]; });
3591 break;
3592 }
Scott Main20cf2a92014-04-02 21:57:20 -07003593 // console.log(firstClause.attr + ':' + firstClause.value);
Dirk Dougherty08032402014-02-15 10:14:35 -08003594 resources = resources || [];
3595
3596 // use additional clauses to filter corpus
3597 if (clauses.length > 1) {
3598 var otherClauses = clauses.slice(1);
3599 resources = resources.filter(getResourceMatchesClausesFilter(otherClauses));
3600 }
3601
3602 // filter out resources already added
3603 if (i > 1) {
3604 resources = resources.filter(getResourceNotAlreadyAddedFilter(addedResourceIndices));
3605 }
3606
3607 // add to list of already added indices
3608 for (var j = 0; j < resources.length; j++) {
Scott Main20cf2a92014-04-02 21:57:20 -07003609 // console.log(resources[j].title);
Dirk Dougherty08032402014-02-15 10:14:35 -08003610 addedResourceIndices[resources[j].index] = 1;
3611 }
3612
3613 // concat to final results list
3614 results = results.concat(resources);
3615 }
3616
3617 if (opts.sortOrder && results.length) {
3618 var attr = opts.sortOrder;
3619
3620 if (opts.sortOrder == 'random') {
3621 var i = results.length, j, temp;
3622 while (--i) {
3623 j = Math.floor(Math.random() * (i + 1));
3624 temp = results[i];
3625 results[i] = results[j];
3626 results[j] = temp;
3627 }
3628 } else {
3629 var desc = attr.charAt(0) == '-';
3630 if (desc) {
3631 attr = attr.substring(1);
3632 }
3633 results = results.sort(function(x,y) {
3634 return (desc ? -1 : 1) * (parseInt(x[attr], 10) - parseInt(y[attr], 10));
3635 });
3636 }
3637 }
3638
3639 results = results.filter(getResourceNotAlreadyAddedFilter(addedPageResources));
3640 results = results.slice(0, maxResults);
3641
3642 for (var j = 0; j < results.length; ++j) {
3643 addedPageResources[results[j].index] = 1;
3644 }
3645
3646 return results;
3647 }
3648
3649
3650 function getResourceNotAlreadyAddedFilter(addedResourceIndices) {
3651 return function(resource) {
3652 return !addedResourceIndices[resource.index];
3653 };
3654 }
3655
3656
3657 function getResourceMatchesClausesFilter(clauses) {
3658 return function(resource) {
3659 return doesResourceMatchClauses(resource, clauses);
3660 };
3661 }
3662
3663
3664 function doesResourceMatchClauses(resource, clauses) {
3665 for (var i = 0; i < clauses.length; i++) {
3666 var map;
3667 switch (clauses[i].attr) {
3668 case 'type':
3669 map = IS_RESOURCE_OF_TYPE[clauses[i].value];
3670 break;
3671 case 'lang':
3672 map = IS_RESOURCE_IN_LANG[clauses[i].value];
3673 break;
3674 case 'tag':
3675 map = IS_RESOURCE_TAGGED[clauses[i].value];
3676 break;
3677 }
3678
3679 if (!map || (!!clauses[i].negative ? map[resource.index] : !map[resource.index])) {
3680 return clauses[i].negative;
3681 }
3682 }
3683 return true;
3684 }
3685
3686
3687 function parseResourceQuery(query) {
3688 // Parse query into array of expressions (expression e.g. 'tag:foo + type:video')
3689 var expressions = [];
3690 var expressionStrs = query.split(',') || [];
3691 for (var i = 0; i < expressionStrs.length; i++) {
3692 var expr = expressionStrs[i] || '';
3693
3694 // Break expression into clauses (clause e.g. 'tag:foo')
3695 var clauses = [];
3696 var clauseStrs = expr.split(/(?=[\+\-])/);
3697 for (var j = 0; j < clauseStrs.length; j++) {
3698 var clauseStr = clauseStrs[j] || '';
3699
3700 // Get attribute and value from clause (e.g. attribute='tag', value='foo')
3701 var parts = clauseStr.split(':');
3702 var clause = {};
3703
3704 clause.attr = parts[0].replace(/^\s+|\s+$/g,'');
3705 if (clause.attr) {
3706 if (clause.attr.charAt(0) == '+') {
3707 clause.attr = clause.attr.substring(1);
3708 } else if (clause.attr.charAt(0) == '-') {
3709 clause.negative = true;
3710 clause.attr = clause.attr.substring(1);
3711 }
3712 }
3713
3714 if (parts.length > 1) {
3715 clause.value = parts[1].replace(/^\s+|\s+$/g,'');
3716 }
3717
3718 clauses.push(clause);
3719 }
3720
3721 if (!clauses.length) {
3722 continue;
3723 }
3724
3725 expressions.push(clauses);
3726 }
3727
3728 return expressions;
3729 }
3730})();
3731
3732(function($) {
3733 /* Simple jquery function to create dom for a standard resource card */
3734 $.fn.decorateResourceCard = function(resource) {
3735 var section = resource.group || resource.type;
3736 var imgUrl;
3737 if (resource.image) {
3738 //keep url clean for matching and offline mode handling
3739 var urlPrefix = resource.image.indexOf("//") > -1 ? "" : toRoot;
3740 imgUrl = urlPrefix + resource.image;
3741 }
3742
3743 $('<div>')
3744 .addClass('card-bg')
3745 .css('background-image', 'url(' + (imgUrl || toRoot + 'assets/images/resource-card-default-android.jpg') + ')')
3746 .appendTo(this);
3747
3748 $('<div>').addClass('card-info' + (!resource.summary ? ' empty-desc' : ''))
3749 .append($('<div>').addClass('section').text(section))
3750 .append($('<div>').addClass('title').html(resource.title))
3751 .append($('<div>').addClass('description')
3752 .append($('<div>').addClass('text').html(resource.summary))
3753 .append($('<div>').addClass('util')
3754 .append($('<div>').addClass('g-plusone')
3755 .attr('data-size', 'small')
3756 .attr('data-align', 'right')
3757 .attr('data-href', resource.url))))
3758 .appendTo(this);
3759
3760 return this;
3761 };
3762
3763 /* Simple jquery function to create dom for a resource section card (menu) */
3764 $.fn.decorateResourceSection = function(section) {
3765 var resource = section.resource;
3766 //keep url clean for matching and offline mode handling
3767 var urlPrefix = resource.image.indexOf("//") > -1 ? "" : toRoot;
3768 var $base = $('<a>')
3769 .addClass('card-bg')
3770 .attr('href', resource.url)
3771 .append($('<div>').addClass('card-section-icon')
3772 .append($('<div>').addClass('icon'))
3773 .append($('<div>').addClass('section').html(resource.title)))
3774 .appendTo(this);
3775
3776 var $cardInfo = $('<div>').addClass('card-info').appendTo(this);
3777
3778 if (section.sections && section.sections.length) {
3779 // Recurse the section sub-tree to find a resource image.
3780 var stack = [section];
3781
3782 while (stack.length) {
3783 if (stack[0].resource.image) {
3784 $base.css('background-image', 'url(' + urlPrefix + stack[0].resource.image + ')');
3785 break;
3786 }
3787
3788 if (stack[0].sections) {
3789 stack = stack.concat(stack[0].sections);
3790 }
3791
3792 stack.shift();
3793 }
3794
3795 var $ul = $('<ul>')
3796 .appendTo($cardInfo);
3797
3798 var max = section.sections.length > 3 ? 3 : section.sections.length;
3799
3800 for (var i = 0; i < max; ++i) {
3801
3802 var subResource = section.sections[i];
3803 $('<li>')
3804 .append($('<a>').attr('href', subResource.url)
3805 .append($('<div>').addClass('title').html(subResource.title))
3806 .append($('<div>').addClass('description')
3807 .append($('<div>').addClass('text').html(subResource.summary))
3808 .append($('<div>').addClass('util')
3809 .append($('<div>').addClass('g-plusone')
3810 .attr('data-size', 'small')
3811 .attr('data-align', 'right')
3812 .attr('data-href', resource.url)))))
3813 .appendTo($ul);
3814 }
3815
3816 // Add a more row
3817 if (max < section.sections.length) {
3818 $('<li>')
3819 .append($('<a>').attr('href', resource.url)
3820 .append($('<div>')
3821 .addClass('title')
3822 .text('More')))
3823 .appendTo($ul);
3824 }
3825 } else {
3826 // No sub-resources, just render description?
3827 }
3828
3829 return this;
3830 };
3831})(jQuery);
3832
3833
3834(function($) {
3835 $.fn.ellipsis = function(options) {
3836
3837 // default option
3838 var defaults = {
3839 'row' : 1, // show rows
3840 'onlyFullWords': true, // set to true to avoid cutting the text in the middle of a word
Dirk Dougherty46b443a2014-04-06 15:27:33 -07003841 'char' : '\u2026', // ellipsis
Dirk Dougherty08032402014-02-15 10:14:35 -08003842 'callback': function() {},
3843 'position': 'tail' // middle, tail
3844 };
3845
3846 options = $.extend(defaults, options);
3847
3848 this.each(function() {
3849 // get element text
3850 var $this = $(this);
3851
3852 var targetHeight = $this.height();
3853 $this.css({'height': 'auto'});
3854 var text = $this.text();
3855 var origText = text;
3856 var origLength = origText.length;
3857 var origHeight = $this.height();
3858
3859 if (origHeight <= targetHeight) {
3860 $this.text(text);
3861 options.callback.call(this);
3862 return;
3863 }
3864
3865 var start = 1, length = 0;
3866 var end = text.length;
3867
3868 if(options.position === 'tail') {
3869 while (start < end) { // Binary search for max length
3870 length = Math.ceil((start + end) / 2);
3871
3872 $this.text(text.slice(0, length) + options['char']);
3873
3874 if ($this.height() <= targetHeight) {
3875 start = length;
3876 } else {
3877 end = length - 1;
3878 }
3879 }
3880
3881 text = text.slice(0, start);
3882
3883 if (options.onlyFullWords) {
3884 // remove fragment of the last word together with possible soft-hyphen chars
3885 text = text.replace(/[\u00AD\w\uac00-\ud7af]+$/, '');
3886 }
3887 text += options['char'];
3888
3889 }else if(options.position === 'middle') {
3890
3891 var sliceLength = 0;
3892 while (start < end) { // Binary search for max length
3893 length = Math.ceil((start + end) / 2);
3894 sliceLength = Math.max(origLength - length, 0);
3895
3896 $this.text(
3897 origText.slice(0, Math.floor((origLength - sliceLength) / 2)) +
3898 options['char'] +
3899 origText.slice(Math.floor((origLength + sliceLength) / 2), origLength)
3900 );
3901
3902 if ($this.height() <= targetHeight) {
3903 start = length;
3904 } else {
3905 end = length - 1;
3906 }
3907 }
3908
3909 sliceLength = Math.max(origLength - start, 0);
3910 var head = origText.slice(0, Math.floor((origLength - sliceLength) / 2));
3911 var tail = origText.slice(Math.floor((origLength + sliceLength) / 2), origLength);
3912
3913 if (options.onlyFullWords) {
3914 // remove fragment of the last or first word together with possible soft-hyphen characters
3915 head = head.replace(/[\u00AD\w\uac00-\ud7af]+$/, '');
3916 }
3917
3918 text = head + options['char'] + tail;
3919 }
3920
3921 $this.text(text);
3922 options.callback.call(this);
3923 });
3924
3925 return this;
3926 };
Scott Main20cf2a92014-04-02 21:57:20 -07003927}) (jQuery);