// Place your application-specific JavaScript functions and classes here // This file is automatically included by javascript_include_tag :defaults //things to do when the page loads $j(document).ready(function() { setupToggled(); if ($j('form#work-form')) { hideFormFields(); } hideHideMe(); showShowMe(); handlePopUps(); attachCharacterCounters(); setupAccordion(); setupDropdown(); updateCachedTokens(); // add clear to items on the splash page in older browsers $j('.splash').children('div:nth-of-type(odd)').addClass('odd'); // make Share buttons on works and own bookmarks visible $j('.actions').children('.share').removeClass('hidden'); // make Approve buttons on inbox items visible $j('#inbox-form, .messages').find('.unreviewed').find('.review').find('a').removeClass('hidden'); prepareDeleteLinks(); thermometer(); $j('body').addClass('javascript'); }); /////////////////////////////////////////////////////////////////// // Autocomplete /////////////////////////////////////////////////////////////////// function get_token_input_options(self) { return { searchingText: self.data('autocomplete-searching-text'), hintText: self.data('autocomplete-hint-text'), noResultsText: self.data('autocomplete-no-results-text'), minChars: self.data('autocomplete-min-chars'), queryParam: "term", preventDuplicates: true, tokenLimit: self.data('autocomplete-token-limit'), liveParams: self.data('autocomplete-live-params'), makeSortable: self.data('autocomplete-sortable') }; } // Look for autocomplete_options in application helper and throughout the views to // see how to use this! var input = $j('input.autocomplete'); if (input.livequery) { jQuery(function($) { $('input.autocomplete').livequery(function(){ var self = $(this); var token_input_options = get_token_input_options(self); var method; try { method = $.parseJSON(self.data('autocomplete-method')); } catch (err) { method = self.data('autocomplete-method'); } self.tokenInput(method, token_input_options); }); }); } /////////////////////////////////////////////////////////////////// // expand, contract, shuffle jQuery(function($){ $(".expand").each(function(){ // start by hiding the list in the page list = $($(this).data("action-target")); if (!list.data("force-expand") || list.children().size() > 25 || list.data("force-contract")) { list.hide(); $(this).show(); } else { // show the shuffle and contract button only $(this).nextAll(".shuffle").show(); $(this).next(".contract").show(); } // set up click event to expand the list $(this).click(function(event){ list = $($(this).data("action-target")); list.show(); // show the contract & shuffle buttons and hide us $(this).next(".contract").show(); $(this).nextAll(".shuffle").show(); $(this).hide(); }); }); $(".contract").each(function(){ $(this).click(function(event){ // hide the list when clicked list = $($(this).data("action-target")); list.hide(); // show the expand and shuffle buttons and hide us $(this).prev(".expand").show(); $(this).nextAll(".shuffle").hide(); $(this).hide(); }); }); $(".shuffle").each(function(){ // shuffle the list's children when clicked $(this).click(function(event){ list = $($(this).data("action-target")); list.children().shuffle(); }); }); $(".expand_all").each(function(){ target = "." + $(this).data("target-class"); $(this).click(function(event) { $(this).closest(target).find(".expand").click(); }); }); $(".contract_all").each(function(){ target = "." + $(this).data("target-class"); $(this).click(function(event) { $(this).closest(target).find(".contract").click(); }); }); }); // check all or none within the parent fieldset, optionally with a string to match on the id attribute of the checkboxes // stored in the "data-checkbox-id-filter" attribute on the all/none links. // allow for some flexibility by checking the next and previous fieldset if the checkboxes aren't in this one jQuery(function($){ $('.check_all').each(function(){ $(this).click(function(event){ var filter = $(this).data('checkbox-id-filter'); var checkboxes; if (filter) { checkboxes = $(this).closest('fieldset').find('input[id*="' + filter + '"][type="checkbox"]'); } else { checkboxes = $(this).closest("fieldset").find(':checkbox'); if (checkboxes.length == 0) { checkboxes = $(this).closest("fieldset").next().find(':checkbox'); if (checkboxes.length == 0) { checkboxes = $(this).closest("fieldset").prev().find(':checkbox'); } } } checkboxes.prop('checked', true); event.preventDefault(); }); }); $('.check_none').each(function(){ $(this).click(function(event){ var filter = $(this).data('checkbox-id-filter'); var checkboxes; if (filter) { checkboxes = $(this).closest('fieldset').find('input[id*="' + filter + '"][type="checkbox"]'); } else { checkboxes = $(this).closest("fieldset").find(':checkbox'); if (checkboxes.length == 0) { checkboxes = $(this).closest("fieldset").next().find(':checkbox'); if (checkboxes.length == 0) { checkboxes = $(this).closest("fieldset").prev().find(':checkbox'); } } } checkboxes.prop('checked', false); event.preventDefault(); }); }); }); // Set up open and close toggles for a given object // Typical setup (this will leave the toggled item open for users without javascript but hide the controls from them): // //
// foo! // //
// // Notes: // - The open button CANNOT be inside the toggled div, the close button can be (but doesn't have to be) // - You can have multiple open and close buttons for the same div since those are labeled with classes // - You don't have to use div and a, those are just examples. Anything you put the toggled and _open/_close classes on will work. // - If you want the toggled item not to be visible to users without JavaScript by default, add the class "hidden" to the toggled item as well. // (and you can then add an alternative link for them using