var mvpreview = new Object(); mvpreview.preview_divs = $A( document.getElementsByTagName('div') ).findAll( function(elem){ return (elem.getAttribute('mv:preview') != null); }); mvpreview.preview_remove_css_links = $A( document.getElementsByTagName('link') ).findAll( function(elem){ return (elem.getAttribute('mv:preview') != null); }); mvpreview.copySections = function(){ var mv_sections_needing_copy = this.preview_divs.findAll( function(elem){ var prevAttr = elem.getAttribute('mv:preview'); return (prevAttr.substring(0,7) == 'copyOf:'); }); //copy sections mv_sections_needing_copy.each( function(e){ var id = e.getAttribute('mv:preview').split(':')[1]; e.innerHTML = $(id).innerHTML; } ); }; mvpreview.removeSections = function(){ var mv_sections_needing_removal = this.preview_remove_css_links.findAll( function(elem){ var prevAttr = elem.getAttribute('mv:preview'); return (prevAttr == 'remove'); }); //copy sections mv_sections_needing_removal.each( function(e){ e.parentNode.removeChild(e); } ); }; mvpreview.sections = mvpreview.preview_divs.findAll( function(elem){ return (elem.getAttribute('mv:preview') == 'showOne'); }); mvpreview.change_messages = function(prefix){ $(prefix+'_messages').innerHTML = 'messages would be here - You are currently running this in browser preview mode. Javascript has overridden the links and buttons so that you can click on any of them to view other pages for this controller. This is to simulate app operation as a standalone prototype.'; } mvpreview.hideSections = function(){ this.sections.each( function(elem){ window.Element.hide(elem); } ); } mvpreview.findSectionToShow = function(id){ return this.sections.find( function(e){ return (e.id == id); } ); } mvpreview.showOneSection = function(id){ this.hideSections(); var sectionToShow = this.findSectionToShow(id); window.Element.show( sectionToShow ); sectionToShow.style.display = 'block'; //force this to override show_only_new.css design time style } mvpreview.registerShowOneSectionLink = function(elemOrId, sectionToShow){ var elem = $(elemOrId); if(elem.getAttribute('onclick')) elem.setAttribute('onclick', ''); //clear out any existing onclick Event.observe( elem, 'click', function(e){ mvpreview.showOneSection(sectionToShow); Event.stop(e); }, false ); } mvpreview.registerShowOneSectionLinks = function( prefix ){ var regexNew = new RegExp( "(^|\\s)" + 'new_link' + "(\\s|$)" ); var regexEdit = new RegExp( "(^|\\s)" + 'edit_link' + "(\\s|$)" ); var regexShow = new RegExp( "(^|\\s)" + 'show_link' + "(\\s|$)" ); var regexList = new RegExp( "(^|\\s)" + 'list_link' + "(\\s|$)" ); var regexPrevious = new RegExp( "(^|\\s)" + 'previous_link' + "(\\s|$)" ); var regexNext = new RegExp( "(^|\\s)" + 'next_link' + "(\\s|$)" ); var regexDestroy = new RegExp( "(^|\\s)" + 'destroy_link' + "(\\s|$)" ); var regexSave = new RegExp( "(^|\\s)" + 'save_button' + "(\\s|$)" ); var regexCancel = new RegExp( "(^|\\s)" + 'cancel_button' + "(\\s|$)" ); var allLinks = $A( document.getElementsByTagName('a') ); allLinks.each( function(link){ var className = link.className; if(className){ if(className.match(regexList)) mvpreview.registerShowOneSectionLink(link, prefix+'_list'); else if(className.match(regexNew)) mvpreview.registerShowOneSectionLink(link, prefix+'_new'); else if(className.match(regexEdit)) mvpreview.registerShowOneSectionLink(link, prefix+'_edit'); else if(className.match(regexShow)) mvpreview.registerShowOneSectionLink(link, prefix+'_show'); else if(className.match(regexDestroy)) mvpreview.registerShowOneSectionLink(link, prefix+'_destroy'); else if(className.match(regexPrevious)) mvpreview.registerShowOneSectionLink(link, prefix+'_list'); else if(className.match(regexNext)) mvpreview.registerShowOneSectionLink(link, prefix+'_list'); } } ); var allInputsAndButtons = $A( document.getElementsByTagName('input') ).concat( $A( document.getElementsByTagName('button') )); allInputsAndButtons.each( function(button){ var className = button.className; if(className){ if(className.match(regexSave)) mvpreview.registerShowOneSectionLink(button, prefix+'_list'); else if(className.match(regexCancel)) mvpreview.registerShowOneSectionLink(button, prefix+'_list'); } } ); } mvpreview.preparePage = function(prefix, options){ mvpreview.change_messages(prefix); mvpreview.copySections(); mvpreview.removeSections(); mvpreview.registerShowOneSectionLinks(prefix); if(options && options.showOneSection) mvpreview.showOneSection(options.showOneSection); }