r84182 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r84181‎ | r84182 | r84183 >
Date:17:14, 17 March 2011
Author:yaron
Status:deferred
Tags:
Comment:
Somewhat of an overhaul - this helper form now uses Javascript, not form submits, to add and remove fields, which should make the interface faster and a little nicer
Modified paths:
  • /trunk/extensions/SemanticForms/specials/SF_CreateTemplate.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticForms/specials/SF_CreateTemplate.php
@@ -48,7 +48,7 @@
4949 return $all_properties;
5050 }
5151
52 - public static function printPropertiesDropdown( $all_properties, $id, $selected_property ) {
 52+ public static function printPropertiesDropdown( $all_properties, $id, $selected_property = null ) {
5353 $selectBody = "<option value=\"\"></option>\n";
5454 foreach ( $all_properties as $prop_name ) {
5555 $optionAttrs = array( 'value' => $prop_name );
@@ -58,27 +58,24 @@
5959 return Xml::tags( 'select', array( 'name' => "semantic_property_$id" ), $selectBody ) . "\n";
6060 }
6161
62 - public static function printFieldEntryBox( $id, $f, $all_properties ) {
 62+ public static function printFieldEntryBox( $id, $all_properties, $display = true ) {
6363 SFUtils::loadMessages();
64 -
65 - $text = "\t" . '<div class="fieldBox">' . "\n";
 64+
 65+ $fieldString = $display ? '' : 'id="starterField" style="display: none"';
 66+ $text = "\t<div class=\"fieldBox\" $fieldString>\n";
6667 $text .= "\t<p>" . wfMsg( 'sf_createtemplate_fieldname' ) . ' ' .
6768 Xml::element( 'input',
68 - array( 'size' => '15', 'name' => 'name_' . $id, 'value' => $f->field_name ), null
 69+ array( 'size' => '15', 'name' => 'name_' . $id ), null
6970 ) . "\n";
7071 $text .= "\t" . wfMsg( 'sf_createtemplate_displaylabel' ) . ' ' .
7172 Xml::element( 'input',
72 - array( 'size' => '15', 'name' => 'label_' . $id, 'value' => $f->label ), null
 73+ array( 'size' => '15', 'name' => 'label_' . $id ), null
7374 ) . "\n";
7475
75 - $dropdown_html = self::printPropertiesDropdown( $all_properties, $id, $f->semantic_property );
 76+ $dropdown_html = self::printPropertiesDropdown( $all_properties, $id );
7677 $text .= "\t" . wfMsg( 'sf_createtemplate_semanticproperty' ) . ' ' . $dropdown_html . "</p>\n";
77 - $checked_str = ( $f->is_list ) ? " checked" : "";
78 - $text .= "\t<p>" . '<input type="checkbox" name="is_list_' . $id . '"' . $checked_str . '> ' . wfMsg( 'sf_createtemplate_fieldislist' ) . "\n";
79 -
80 - if ( $id != "new" ) {
81 - $text .= ' &#160;&#160;<input name="del_' . $id . '" type="submit" value="' . wfMsg( 'sf_createtemplate_deletefield' ) . '">' . "\n";
82 - }
 78+ $text .= "\t<p>" . '<input type="checkbox" name="is_list_' . $id . '" /> ' . wfMsg( 'sf_createtemplate_fieldislist' ) . "\n";
 79+ $text .= ' &#160;&#160;<input type="button" value="' . wfMsg( 'sf_createtemplate_deletefield' ) . '" class="deleteField" />' . "\n";
8380
8481 $text .= <<<END
8582 </p>
@@ -88,91 +85,128 @@
8986 return $text;
9087 }
9188
 89+ static function addJavascript() {
 90+ global $wgOut;
 91+
 92+ SFUtils::addJavascriptAndCSS();
 93+
 94+ // TODO - this should be in a JS file
 95+ $template_name_error_str = wfMsg( 'sf_blank_error' );
 96+ $jsText =<<<END
 97+<script type="text/javascript">
 98+var fieldNum = 1;
 99+function createTemplateAddField() {
 100+ fieldNum++;
 101+ newField = jQuery('#starterField').clone().css('display', '').removeAttr('id');
 102+ newHTML = newField.html().replace(/starter/g, fieldNum);
 103+ newField.html(newHTML);
 104+ newField.find(".deleteField").click( function() {
 105+ // Remove the encompassing div for this instance.
 106+ jQuery(this).closest(".fieldBox")
 107+ .fadeOut('fast', function() { jQuery(this).remove(); });
 108+ });
 109+ jQuery('#fieldsList').append(newField);
 110+}
 111+
 112+jQuery(".deleteField").click( function() {
 113+ // Remove the encompassing div for this instance.
 114+ jQuery(this).closest(".fieldBox")
 115+ .fadeOut('fast', function() { jQuery(this).remove(); });
 116+});
 117+
 118+function validateCreateTemplateForm() {
 119+ templateName = jQuery('#template_name').val();
 120+ if (templateName == '') {
 121+ scroll(0, 0);
 122+ jQuery('#template_name_p').append('<font color="red">$template_name_error_str</font>');
 123+ return false;
 124+ } else {
 125+ return true;
 126+ }
 127+}
 128+jQuery('#createTemplateForm').submit( function() { return validateCreateTemplateForm(); } );
 129+</script>
 130+
 131+END;
 132+ $wgOut->addScript( $jsText );
 133+ }
 134+
92135 static function printCreateTemplateForm() {
93136 global $wgOut, $wgRequest, $wgUser, $sfgScriptPath;
94137
95138 SFUtils::loadMessages();
 139+ self::addJavascript();
96140
97 - $all_properties = self::getAllPropertyNames();
98 -
99 - $template_name = $wgRequest->getVal( 'template_name' );
100 - $template_name_error_str = "";
101 - $category = $wgRequest->getVal( 'category' );
102 - $cur_id = 1;
103 - $fields = array();
104 - // Cycle through the query values, setting the appropriate
105 - // local variables.
106 - foreach ( $wgRequest->getValues() as $var => $val ) {
107 - $var_elements = explode( "_", $var );
108 - // we only care about query variables of the form "a_b"
109 - if ( count( $var_elements ) != 2 )
110 - continue;
111 - list ( $field_field, $old_id ) = $var_elements;
112 - if ( $field_field == "name" ) {
113 - if ( $old_id != "new" || ( $old_id == "new" && $val != "" ) ) {
114 - if ( $wgRequest->getVal( 'del_' . $old_id ) != '' ) {
115 - // Do nothing - this field won't get added to the new list
116 - } else {
117 - $field = SFTemplateField::create( $val, $wgRequest->getVal( 'label_' . $old_id ) );
118 - $field->semantic_property = $wgRequest->getVal( 'semantic_property_' . $old_id );
119 - $field->is_list = $wgRequest->getCheck( 'is_list_' . $old_id );
120 - $fields[] = $field;
121 - }
122 - }
123 - }
124 - }
125 - $aggregating_property = $wgRequest->getVal( 'semantic_property_aggregation' );
126 - $aggregation_label = $wgRequest->getVal( 'aggregation_label' );
127 - $template_format = $wgRequest->getVal( 'template_format' );
128 -
129 - $text = "";
130 - $save_button_text = wfMsg( 'savearticle' );
131 - $preview_button_text = wfMsg( 'preview' );
 141+ $text = '';
132142 $save_page = $wgRequest->getCheck( 'wpSave' );
133143 $preview_page = $wgRequest->getCheck( 'wpPreview' );
134144 if ( $save_page || $preview_page ) {
135 - # validate template name
136 - if ( $template_name == '' ) {
137 - $template_name_error_str = wfMsg( 'sf_blank_error' );
138 - } else {
139 - // redirect to wiki interface
140 - $wgOut->setArticleBodyOnly( true );
141 - $title = Title::makeTitleSafe( NS_TEMPLATE, $template_name );
142 - $full_text = SFTemplateField::createTemplateText( $template_name, $fields, null, $category, $aggregating_property, $aggregation_label, $template_format );
143 - $text = SFUtils::printRedirectForm( $title, $full_text, "", $save_page, $preview_page, false, false, false, null, null );
144 - $wgOut->addHTML( $text );
145 - return;
 145+ $fields = array();
 146+ // Cycle through the query values, setting the
 147+ // appropriate local variables.
 148+ foreach ( $wgRequest->getValues() as $var => $val ) {
 149+ $var_elements = explode( "_", $var );
 150+ // we only care about query variables of the form "a_b"
 151+ if ( count( $var_elements ) != 2 )
 152+ continue;
 153+ list ( $field_field, $id ) = $var_elements;
 154+ if ( $field_field == 'name' && $id != 'starter' ) {
 155+ $field = SFTemplateField::create( $val, $wgRequest->getVal( 'label_' . $id ) );
 156+ $field->semantic_property = $wgRequest->getVal( 'semantic_property_' . $id );
 157+ $field->is_list = $wgRequest->getCheck( 'is_list_' . $id );
 158+ $fields[] = $field;
 159+ }
146160 }
 161+
 162+ // Assemble the template text, and submit it as a wiki
 163+ // page.
 164+ $wgOut->setArticleBodyOnly( true );
 165+ $template_name = $wgRequest->getVal( 'template_name' );
 166+ $title = Title::makeTitleSafe( NS_TEMPLATE, $template_name );
 167+ $category = $wgRequest->getVal( 'category' );
 168+ $aggregating_property = $wgRequest->getVal( 'semantic_property_aggregation' );
 169+ $aggregation_label = $wgRequest->getVal( 'aggregation_label' );
 170+ $template_format = $wgRequest->getVal( 'template_format' );
 171+ $full_text = SFTemplateField::createTemplateText( $template_name, $fields, null, $category, $aggregating_property, $aggregation_label, $template_format );
 172+ $text = SFUtils::printRedirectForm( $title, $full_text, "", $save_page, $preview_page, false, false, false, null, null );
 173+ $wgOut->addHTML( $text );
 174+ return;
147175 }
148176
149 - $text .= ' <form action="" method="post">' . "\n";
 177+ $text .= ' <form id="createTemplateForm" action="" method="post">' . "\n";
150178
151 - // set 'title' field, in case there's no URL niceness
 179+ // Set 'title' field, in case there's no URL niceness
152180 $ct = Title::makeTitleSafe( NS_SPECIAL, 'CreateTemplate' );
153181 $text .= "\t" . Xml::hidden( 'title', SFUtils::titleURLString( $ct ) ) . "\n";
154 - $text .= "\t<p>" . wfMsg( 'sf_createtemplate_namelabel' ) . ' <input size="25" name="template_name" value="' . $template_name . '"> <font color="red">' . $template_name_error_str . '</font></p>' . "\n";
155 - $text .= "\t<p>" . wfMsg( 'sf_createtemplate_categorylabel' ) . ' <input size="25" name="category" value="' . $category . '"></p>' . "\n";
 182+ $text .= "\t<p id=\"template_name_p\">" . wfMsg( 'sf_createtemplate_namelabel' ) . ' <input size="25" id="template_name" name="template_name" /></p>' . "\n";
 183+ $text .= "\t<p>" . wfMsg( 'sf_createtemplate_categorylabel' ) . ' <input size="25" name="category" /></p>' . "\n";
156184 $text .= "\t<fieldset>\n";
157185 $text .= "\t" . Xml::element( 'legend', null, wfMsg( 'sf_createtemplate_templatefields' ) ) . "\n";
158186 $text .= "\t" . Xml::element( 'p', null, wfMsg( 'sf_createtemplate_fieldsdesc' ) ) . "\n";
159187
160 - foreach ( $fields as $i => $field ) {
161 - $text .= self::printFieldEntryBox( $i + 1, $field, $all_properties );
162 - }
163 - $new_field = new SFTemplateField();
164 - $text .= self::printFieldEntryBox( "new", $new_field, $all_properties );
 188+ $all_properties = self::getAllPropertyNames();
 189+ $text .= '<div id="fieldsList">' . "\n";
 190+ $text .= self::printFieldEntryBox( "1", $all_properties );
 191+ $text .= self::printFieldEntryBox( "starter", $all_properties, false );
 192+ $text .= "</div>\n";
165193
166 - $text .= "\t<p>" . Xml::element( 'input',
167 - array( 'type' => 'submit', 'value' => wfMsg( 'sf_createtemplate_addfield' ) ), null ) . "</p>\n";
 194+ $add_field_button = Xml::element( 'input',
 195+ array(
 196+ 'type' => 'button',
 197+ 'value' => wfMsg( 'sf_createtemplate_addfield' ),
 198+ 'onclick' => "createTemplateAddField()"
 199+ )
 200+ );
 201+ $text .= Xml::tags( 'p', null, $add_field_button ) . "\n";
168202 $text .= "\t</fieldset>\n";
169203 $text .= "\t<fieldset>\n";
170204 $text .= "\t" . Xml::element( 'legend', null, wfMsg( 'sf_createtemplate_aggregation' ) ) . "\n";
171205 $text .= "\t" . Xml::element( 'p', null, wfMsg( 'sf_createtemplate_aggregationdesc' ) ) . "\n";
172206 $text .= "\t<p>" . wfMsg( 'sf_createtemplate_semanticproperty' ) . ' ' .
173 - self::printPropertiesDropdown( $all_properties, "aggregation", $aggregating_property ) . "</p>\n";
 207+ self::printPropertiesDropdown( $all_properties, "aggregation" ) . "</p>\n";
174208 $text .= "\t<p>" . wfMsg( 'sf_createtemplate_aggregationlabel' ) . ' ' .
175209 Xml::element( 'input',
176 - array( 'size' => '25', 'name' => 'aggregation_label', 'value' => $aggregation_label ), null ) .
 210+ array( 'size' => '25', 'name' => 'aggregation_label' ), null ) .
177211 "</p>\n";
178212 $text .= "\t</fieldset>\n";
179213 $text .= "\t<p>" . wfMsg( 'sf_createtemplate_outputformat' ) . "\n";
@@ -185,6 +219,8 @@
186220 $text .= "\t" . Xml::element( 'input',
187221 array( 'type' => 'radio', 'name' => 'template_format', 'value' => 'infobox'), null ) .
188222 ' ' . wfMsg( 'sf_createtemplate_infoboxformat' ) . "</p>\n";
 223+ $save_button_text = wfMsg( 'savearticle' );
 224+ $preview_button_text = wfMsg( 'preview' );
189225 $text .= <<<END
190226 <div class="editButtons">
191227 <input type="submit" id="wpSave" name="wpSave" value="$save_button_text" />