/*////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ======================================================================================================================== || JavaScript for art.sdsu.edu CMS || || @ Author: Kyle Florence || @ Update: July 07, 2008 || || ==================================================================================================================== || || = Global Functions || ======================================================================================================================== //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////*/ // Global vars var ms; // Multi-file selection class var dateField; // For the mini-calendar /**********************************************************************************************************************\ ======================================================================================================================== = On Page Load ======================================================================================================================== \**********************************************************************************************************************/ // events.js - load when DOM is loaded //addEvent(window, 'DOMContentLoaded', function() //{ // stuff here //}); // Load when page is fully loaded Event.observe(window, 'load', function() { // Listen for clicks on 'location' if ($('location')) $('location').observe('change', checkRoomType); // List sections if ($('listsections')) $('listsections').observe('click', updateSectionsList); // List events if ($('listevents')) $('listevents').observe('click', updateEventsList); // Toggle repeat_weeks $('repeat_type').observe('change', showHideRepeatType); if ($('repeat_type')) { Event.observe('repeat_type', 'change', function(e) { if ($('repeat_type').value == 'weeks') { $('repeat_weeks').show(); } else { $('repeat_weeks').hide(); } }); } // Preview section if ($('preview')) $('preview').observe('click', previewPage); // Listen for clicks on 'after' if ($('after')) $('after').observe('click', selectAfter); // Listen for changes to the page location if ($('nav_lists')) { Event.observe('nav1', 'change', function(e){updateNavList(e);}); Event.observe('nav2', 'change', function(e){updateNavList(e);}); Event.observe('nav3', 'change', function(e){updateNavList(e);}); } // Multi-file upload if ($('files_list')) { // Initialize our class ms = new MultiSelector('files_list', 10).addElement('file_uploader'); } }); /*///////////////////////////////////////////////////////// ----------------------------------------------------------- | | = Multi-File Selector Class | | Based on the multifile.js class by Stickman | http://www.the-stickman.com | ----------------------------------------------------------- /////////////////////////////////////////////////////////*/ // MultiSelector Class var MultiSelector = Class.create( { // Called on initialization initialize: function (list, max) { // Element ID this.id = 0; // How many elements? this.count = 0; // Current element this.current_e = null; // Where to write the list this.list = $(list); // Is there a maximum? if (max) this.max = max; else this.max = -1; }, // Add a new file input element addElement: function (e) { // Make sure element is extended e = $(e); // Make sure it's a file input element if (e.tagName == 'INPUT' && e.type == 'file') { // Increase our element count this.count++; // Set this as our most recent element this.current_e = e; // If we've reached maximum number, disable input element if (this.max != -1 && this.count > this.max) e.disable(); // Class reference for inner functions var that = this; // What to do when a file is selected e.onchange = function() { // Create a new form input element var new_e = new Element( 'input', {'type' : 'file', 'name' : 'files[]'} ); // Add new element to DOM e.insert({after: new_e}); // Add element to our class that.addElement(new_e); // Update element list that.addListRow(e); // Hide the current input element e.setStyle({position: 'absolute', left: '-1000px'}); }; } // Not a file input element else alert('Error: not a file input element'); }, // Add a new row to the list of files addListRow: function (e) { // Hide/show filler text $('filler').hide(); // Create a new row with this element value var row = new Element('li').update(e.value); // Add the new element to our list this.list.insert({top: row}); // Create a delete button var button = new Element( 'img', { 'src' : '/site/template/images/cms/remove.gif', 'alt' : 'remove', 'title' : 'Remove File' } ); // Add it to this row row.insert({top: button}); // Input form reference for this row row.element = e; // Class reference for inner functions var that = this; // Delete function button.onclick = function() { // Decrement counter that.count--; // If our list is empty, show filler if (that.count <= 1) $('filler').show(); // Remove input element from form this.parentNode.element.remove(); // Remove this row this.parentNode.remove(); // Re-enable input element (if it's disabled) that.current_e.enable(); // Prevent page from refreshing return false; }; } }); /**********************************************************************************************************************\ ======================================================================================================================== = Useful Functions ======================================================================================================================== \**********************************************************************************************************************/ /*///////////////////////////////////////////////////////// ----------------------------------------------------------- | Function confirmPost: | - Display an alert where the user can confirm they wish | to procede with the posting of a form. | | @msg = The message to show in the confirmation box. ----------------------------------------------------------- /////////////////////////////////////////////////////////*/ function confirmPost(msg) { // Show confirmation window if (confirm(msg)) {return true} else {return false}; } /*///////////////////////////////////////////////////////// ----------------------------------------------------------- | Function getRadioValue: | | - Returns the value of a checked radio button from a | radio button group | | @form = The form element. | @radioGroup = The name of the radiobutton group. ----------------------------------------------------------- /////////////////////////////////////////////////////////*/ function getRadioValue(form, radioGroup) { var button = $(form).getInputs('radio', radioGroup).find(function(rb){return rb.checked;}); return (button) ? $F(button) : null; } /*///////////////////////////////////////////////////////// ----------------------------------------------------------- | Function getRadioValue: | | - Sets the value of a radio button from a radio button | group. | | @form = The form element. | @radioGroup = The name of the radiobutton group. ----------------------------------------------------------- /////////////////////////////////////////////////////////*/ function setRadioChecked(form, radioGroup, value) { var button = $(form).getInputs('radio', radioGroup).find(function(rb){return rb.value == value;}); return (button) ? button.checked = true : null; } /**********************************************************************************************************************\ ======================================================================================================================== = DHTML functions ======================================================================================================================== \**********************************************************************************************************************/ function showHideRepeatType(event) { if ($('repeat_type').value == 'weeks') $('repeat_weeks').show(); else $('repeat_weeks').hide(); } /*///////////////////////////////////////////////////////// ----------------------------------------------------------- | Function toggleCalendar: | - Shows or hides the mini-calendar | | @img = The image element tied to this mini-calendar, it | is used for positioning the calendar. | @input = The input field tied to this mini-calendar. ----------------------------------------------------------- /////////////////////////////////////////////////////////*/ function toggleCalendar(img, input) { // Store a reference to our miniCalendar var cal = $('mini-calendar'); // Store which input field we will be using dateField = $(input); // If it's empty, populate it if (cal.empty()) goToDate($F(dateField)); // Set position relative to the image clicked // then make it visible cal.setStyle({ position: 'absolute', fontSize: '12px' }).clonePosition(img, { setWidth: false, setHeight: false, offsetTop: -22, offsetLeft: 22 }).toggle(); } /*///////////////////////////////////////////////////////// ----------------------------------------------------------- | Function goToDate: | - This function is AJAX-driven, it will update the mini- | calendar based on the date argument. | | @date = The month/year to load in mm-dd-yyyy format. ----------------------------------------------------------- /////////////////////////////////////////////////////////*/ function goToDate(date) { // Script URL var url = '/ajax?do=miniCalendar&date=' + date; // AJAX Request updates mini-calendar information new Ajax.Request(url, { method: 'get', onSuccess: function(request) { // Update the miniCalendar $('mini-calendar').update(request.responseText); } }); } /*///////////////////////////////////////////////////////// ----------------------------------------------------------- | Function chooseDate: | - Stores the user-selected date in the correct form | field, then hides the mini-calendar. | | @date = The selected date. | @field = The field to populate with the date. ----------------------------------------------------------- /////////////////////////////////////////////////////////*/ function chooseDate(date) { // Update the field's value if (dateField) { // Update field value dateField.value = date; // Make calendar go away $('mini-calendar').toggle(); } // No field for whatever reason else alert('Field is null!'); } /*///////////////////////////////////////////////////////// ----------------------------------------------------------- | Function selectAfter: | - Automatically selects the 'After: ' radiobutton if the | after list is clicked on. | | @void ----------------------------------------------------------- /////////////////////////////////////////////////////////*/ function selectAfter() { if ($('cms_page_add')) setRadioChecked('cms_page_add', 'priority', 'after'); if ($('cms_page_update')) setRadioChecked('cms_page_update', 'priority', 'after'); } /**********************************************************************************************************************\ ======================================================================================================================== = AJAX Requests ======================================================================================================================== \**********************************************************************************************************************/ /*///////////////////////////////////////////////////////// ----------------------------------------------------------- | Function checkRoomType: | - Checks to see if the selected room is a SMART room | then shows or hides the SMART notice. | | @event = The event which is causing the method call. ----------------------------------------------------------- /////////////////////////////////////////////////////////*/ // Click triggered function checkRoomType(event) { // Grab the element var element = event.element(); // Script URL var url = '/ajax?do=checkRoomType&room=' + element.value; // AJAX Request updates mini-calendar information new Ajax.Request(url, { method: 'get', onSuccess: function(request) { // Show/hide the SMART room notice if (request.responseText == '1') $('smart-notice').show(); else $('smart-notice').hide(); } }); } /* Populates navList on page load */ function previewPage() { //alert($('pb_content').value); // Script URL var url = '/ajax?do=previewPage&content=' + encodeURIComponent($('pb_content').value); // AJAX Request to update navigation selection lists new Ajax.Request(url, { method: 'get', onSuccess: function(request) { // Grab our response text var text = request.responseText; var page = $('pagebuilder'); // If we have information, update the element if (text.length > 0) page.update(text); // Show, if hidden if (!page.visible()) page.show(); } }); } /*///////////////////////////////////////////////////////// ----------------------------------------------------------- | Function updateNavList: | - Updates navigation lists for pagebuilder based on the | event passed in by an observer. | | @event = the event passed by the observe method, or an | element passed manually into the function. ----------------------------------------------------------- /////////////////////////////////////////////////////////*/ function updateNavList(event) { // Grab the element //var element = (event instanceof Event) ? event.element() : $(event); var element = event.element(); // Script URL var url = '/ajax?do=updateNavList&value=' + element.value; // For nav3, no need for ajax request if (element.identify() == 'nav3') { // Update page location value $('location').value = element.value; // Update section list updateSectionList(element.value); } // AJAX Request to update navigation selection lists else { // AJAX Request to update navigation selection lists new Ajax.Request(url, { method: 'get', onSuccess: function(request) { // Grab our response text var text = request.responseText; // If user changed nav1, hide nav3 if (element.identify() == 'nav1') $('nav3').hide(); // If we have information, update the element if (text.length > 0) { // Grab the next element (to update) var next = element.next('select'); // Update it next.update(text); // If it's invisible, show it if (!next.visible()) next.show(); } // If we change nav1, and there is no nav2 for it, hide nav2 and nav3 else if (element.identify() == 'nav1') { $('nav2').hide(); $('nav3').hide(); } // If we change nav2, and there is no nav3 for it, hide nav3 else if (element.identify() == 'nav2') $('nav3').hide(); // Set our new page location value $('location').value = element.value; // Update section list updateSectionList(element.value); } }); } } /*///////////////////////////////////////////////////////// ----------------------------------------------------------- | Function updateSectionList: | - Updates the 'section location' list based on the | selected nav1, nav2, and nav3 page locations. | | @void ----------------------------------------------------------- /////////////////////////////////////////////////////////*/ function updateSectionList(value) { // Script URL var url = '/ajax?do=updateSectionList&value=' + value; // AJAX Request to update navigation selection lists new Ajax.Request(url, { method: 'get', onSuccess: function(request) { // Grab our response text var text = request.responseText; // If we have information, update the element if (text.length > 0) { $('after').update(text); if (!$('li-after').visible()) $('li-after').show(); } // Otherwise, hide the element else $('li-after').hide(); } }); } /*///////////////////////////////////////////////////////// ----------------------------------------------------------- | Function updateEventsList: | - Updates the event list for event selection | | @void ----------------------------------------------------------- /////////////////////////////////////////////////////////*/ function updateEventsList() { // Get vars var form = $('select-event'); var author = getRadioValue(form, 'author'); var type = form['type']; var month = form['month']; var year = form['year']; // Script URL var url = '/ajax?do=listEvents' + '&author=' + author + '&type=' + $F(type) + '&month=' + $F(month) + '&year=' + $F(year); // AJAX Request updates event list new Ajax.Request(url, { method: 'get', onSuccess: function(request) { // Update the results $('search-results').update(request.responseText); // Show results if not visible if (!$('results').visible()) $('results').show(); } }); } /*///////////////////////////////////////////////////////// ----------------------------------------------------------- | Function updateSectionsList: | - Updates the section list for page section selection | | @void ----------------------------------------------------------- /////////////////////////////////////////////////////////*/ function updateSectionsList() { // Get vars var form = $('select-section'); var author = getRadioValue(form, 'author'); var location = form['location']; // Script URL var url = '/ajax?do=listSections' + '&author=' + author + '&location=' + $F(location); // AJAX Request updates event list new Ajax.Request(url, { method: 'get', onSuccess: function(request) { // Update the results $('search-results').update(request.responseText); // Show results if not visible if (!$('results').visible()) $('results').show(); } }); }