# HG changeset patch # User Nathan Phillip Brink # Date 2010-10-16 23:52:06 # Node ID 0741fde6e76581f6560f5d0f6aa57ddf43ebd8af # Parent 886ce57dfcfe3764990f0859d842082dbfd6ac43 When restoring a saved schedule, use JS instead of PHP to generate the form data. This way, we avoid duplicating the same table/form generation code in both PHP and JS. diff --git a/inc/class.page.php b/inc/class.page.php --- a/inc/class.page.php +++ b/inc/class.page.php @@ -386,4 +386,25 @@ class page { return $this->school; } + + /** + * \brief + * Format a chunk of javascript suitable for adding to headcode. + * + * Takes into account whether or not the code should be wrapped in + * CDATA or not. + * + * \param $js + * The javascript to wrap up. + * \param $type + * The type="" attribute of the '; + } } diff --git a/input.php b/input.php --- a/input.php +++ b/input.php @@ -21,22 +21,35 @@ if (isset($_REQUEST['s'])) if ($sch) { $nclasses = $sch->nclasses_get(); - $my_hc = ''; - $inputPage->headcode_add('scheduleInput', $my_hc, TRUE); + { + $class = $sch->class_get($class_key); + $my_hc .= ' class_last = add_class_n(\'' . htmlentities($class->getName()) . "');\n"; + + $nsections = $class->getnsections(); + for ($section_key = 0; $section_key < $nsections; $section_key ++) + { + $section = $class->getSection($section_key); + $my_hc .= ' add_section_n(class_last, \'' . htmlentities($section->getLetter()) . '\', \'' + . htmlentities($section->getSynonym()) . '\', \'' + . $section->getStartTime() . '\', \'' + . $section->getEndTime() . '\', ' + . json_encode(array('m' => $section->getM(), 't' => $section->getTu(), 'w' => $section->getW(), 'h' => $section->getTh(), 'f' => $section->getF())) . ', \'' + . htmlentities($section->getProf()) . "');\n"; + } + } + $my_hc .= ' }); +'; + $inputPage->headcode_add('scheduleInput', $inputPage->script_wrap($my_hc), TRUE); } else - $inputPage->headcode_add('schduleInput', '', TRUE); + $inputPage->headcode_add('schduleInput', $inputPage->script_wrap('jQuery(document).ready( function() { add_class(); } );'), TRUE); $inputPage->head(); @@ -99,7 +112,6 @@ if (!empty($_REQUEST['selectschool']) - input_form_render(); ?> diff --git a/scripts/scheduleInput.js b/scripts/scheduleInput.js --- a/scripts/scheduleInput.js +++ b/scripts/scheduleInput.js @@ -1,10 +1,9 @@ - //-------------------------------------------------- // General Notes //-------------------------------------------------- - /* classNum is declared in the to enable loading of saved classes */ - /* sectionsOfClass is declared in the to enable loading of saved sections */ +var classNum = 0; +var sectionsOfClass = new Array(); //-------------------------------------------------- @@ -69,79 +68,103 @@ //-------------------------------------------------- // Returns the common inputs for each new section. //-------------------------------------------------- - function genSectionHtml(cnum){ - genSectionHtml(cnum,'','','','','',''); + function genSectionHtml(cnum) + { + genSectionHtml_n(cnum,'','','','','',''); } /* @TODO: This should select & set items based on args, if the args != '' */ - function genSectionHtml(cnum,name,synonym,stime,etime,days,prof) { + function genSectionHtml_n(cnum,name,synonym,stime,etime,days,prof) + { var snum = sectionsOfClass[cnum]; var result = ''; - result = result + ''; + result = result + ''; - result = result + '\ - \ - \ - \ - \ - \ - '; + result = result + '\ + \ + \ + \ + \ + \ + '; result = result + '
'; return result; } +/** + * Outputs an \n"; +} + //-------------------------------------------------- // Add a section to a class //-------------------------------------------------- - function add_section(cnum) { - jQuery('.pclass'+cnum).after(genSectionHtml(cnum)); + function add_section_n(cnum,name,synonym,stime,etime,days,prof) + { + jQuery('.pclass'+cnum).after(genSectionHtml_n(cnum,name,synonym,stime,etime,days,prof)); + sectionsOfClass[cnum] ++; + } + function add_section(cnum) + { + return add_section_n(cnum, '', '', '', '', {'m':false, 't':false, 'w':false, 'h':false, 'f':false}, ''); } - function add_section(cnum,name,synonym,stime,etime,days,prof) { - jQuery('.pclass'+cnum).after(genSectionHtml(cnum,name,synonym,stime,etime,days,prof)); - } //-------------------------------------------------- // Adds a new class to the input. //-------------------------------------------------- - function add_class(){ + function add_class_n(name) + { sectionsOfClass[classNum] = 0; // Initialize at 0 - jQuery('#jsrows').append('
'); + jQuery('#jsrows').append('
'); jQuery('.className' + classNum).autocomplete({ source: "auto.php" }); classNum++; + + return (classNum - 1); }; +function add_class() +{ + return add_class_n(''); +} //-------------------------------------------------- @@ -185,7 +208,6 @@ //-------------------------------------------------- jQuery('.addSection').live('click', function() { add_section(jQuery(this).parent().parent().attr("title"), sectionsOfClass[jQuery(this).parent().parent().attr("title")]); - sectionsOfClass[jQuery(this).parent().parent().attr("title")]++; // Increases sectionsOfClass[classNum] }); //-------------------------------------------------- @@ -217,10 +239,4 @@ jQuery('#showadvanced').hide(); jQuery('.advanced').slideToggle(); }); - - //-------------------------------------------------- - // Add initial class - //-------------------------------------------------- - add_class(); - });