MDL-55417 theme_noname: Update mform templates to re-use javascript
authorDamyon Wiese <damyon@moodle.com>
Fri, 12 Aug 2016 03:43:48 +0000 (11:43 +0800)
committerDan Poltawski <dan@moodle.com>
Fri, 23 Sep 2016 09:53:30 +0000 (10:53 +0100)
Use an amd module instead of repeating JS in the templates.

Part of MDL-55071

theme/noname/amd/build/form-display-errors.min.js [new file with mode: 0644]
theme/noname/amd/src/form-display-errors.js [new file with mode: 0644]
theme/noname/templates/core_form/element-checkbox-inline.mustache
theme/noname/templates/core_form/element-checkbox.mustache
theme/noname/templates/core_form/element-radio-inline.mustache
theme/noname/templates/core_form/element-radio.mustache
theme/noname/templates/core_form/element-template-inline.mustache
theme/noname/templates/core_form/element-template.mustache

diff --git a/theme/noname/amd/build/form-display-errors.min.js b/theme/noname/amd/build/form-display-errors.min.js
new file mode 100644 (file)
index 0000000..432391f
Binary files /dev/null and b/theme/noname/amd/build/form-display-errors.min.js differ
diff --git a/theme/noname/amd/src/form-display-errors.js b/theme/noname/amd/src/form-display-errors.js
new file mode 100644 (file)
index 0000000..21ef87b
--- /dev/null
@@ -0,0 +1,47 @@
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * Custom form error event handler to manipulate the bootstrap markup and show
+ * nicely styled errors in an mform.
+ *
+ * @module     theme_noname/form-display-errors
+ * @copyright  2016 Damyon Wiese <damyon@moodle.com>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+define(['jquery', 'core/event'], function($, Event) {
+    return {
+        enhance: function(elementid) {
+            var element = document.getElementById(elementid);
+            $(element).on(Event.Events.FORM_FIELD_VALIDATION, function(event, msg) {
+                event.preventDefault();
+                var parent = $(element).closest('.form-group');
+                var feedback = parent.find('.form-control-feedback');
+                if (msg !== '') {
+                    parent.addClass('has-danger');
+                    $(element).addClass('form-control-danger');
+                    $(element).attr('aria-describedby', $(element).attr('id') + '-feedback');
+                    feedback.html(msg);
+                    feedback.show();
+                } else {
+                    parent.removeClass('has-danger');
+                    $(element).removeClass('form-control-danger');
+                    $(element).attr('aria-describedby', '');
+                    feedback.hide();
+                }
+            });
+        }
+    };
+});
\ No newline at end of file
index 15b0c5b..9f1636c 100644 (file)
     {{{error}}}
 </span>
 {{#js}}
-require(['jquery', 'core/event'], function($, Event) {
-    var element = document.getElementById('{{element.id}}');
-    $(element).on(Event.Events.FORM_FIELD_VALIDATION, function(event, msg) {
-        event.preventDefault();
-        var parent = $(element).closest('.form-group');
-        var feedback = parent.find('.form-control-feedback');
-        if (msg !== '') {
-            parent.addClass('has-danger');
-            $(element).attr('aria-describedby', $(element).attr('id') + '-feedback');
-            feedback.html(msg);
-            feedback.show();
-        } else {
-            parent.removeClass('has-danger');
-            $(element).attr('aria-describedby', '');
-            feedback.hide();
-        }
-    });
+require(['theme_noname/form-display-errors'], function(module) {
+    module.enhance({{#quote}}{{element.id}}{{/quote}});
 });
 {{/js}}
\ No newline at end of file
index 78fdef8..dc8b4f9 100644 (file)
     </div>
 </div>
 {{#js}}
-require(['jquery', 'core/event'], function($, Event) {
-    var element = document.getElementById('{{element.id}}');
-    $(element).on(Event.Events.FORM_FIELD_VALIDATION, function(event, msg) {
-        event.preventDefault();
-        var parent = $(element).closest('.form-group');
-        var feedback = parent.find('.form-control-feedback');
-        if (msg !== '') {
-            parent.addClass('has-danger');
-            $(element).attr('aria-describedby', $(element).attr('id') + '-feedback');
-            feedback.html(msg);
-            feedback.show();
-        } else {
-            parent.removeClass('has-danger');
-            $(element).attr('aria-describedby', '');
-            feedback.hide();
-        }
-    });
+require(['theme_noname/form-display-errors'], function(module) {
+    module.enhance({{#quote}}{{element.id}}{{/quote}});
 });
 {{/js}}
\ No newline at end of file
index cc8fd3c..465597a 100644 (file)
     {{{error}}}
 </span>
 {{#js}}
-require(['jquery', 'core/event'], function($, Event) {
-    var element = document.getElementById('{{element.id}}');
-    $(element).on(Event.Events.FORM_FIELD_VALIDATION, function(event, msg) {
-        event.preventDefault();
-        var parent = $(element).closest('.form-group');
-        var feedback = parent.find('.form-control-feedback');
-        if (msg !== '') {
-            parent.addClass('has-danger');
-            $(element).attr('aria-describedby', $(element).attr('id') + '-feedback');
-            feedback.html(msg);
-            feedback.show();
-        } else {
-            parent.removeClass('has-danger');
-            $(element).attr('aria-describedby', '');
-            feedback.hide();
-        }
-    });
+require(['theme_noname/form-display-errors'], function(module) {
+    module.enhance({{#quote}}{{element.id}}{{/quote}});
 });
 {{/js}}
\ No newline at end of file
index 8b74048..b83155f 100644 (file)
     </div>
 </div>
 {{#js}}
-require(['jquery', 'core/event'], function($, Event) {
-    var element = document.getElementById('{{element.id}}');
-    $(element).on(Event.Events.FORM_FIELD_VALIDATION, function(event, msg) {
-        event.preventDefault();
-        var parent = $(element).closest('.form-group');
-        var feedback = parent.find('.form-control-feedback');
-        if (msg !== '') {
-            parent.addClass('has-danger');
-            $(element).attr('aria-describedby', $(element).attr('id') + '-feedback');
-            feedback.html(msg);
-            feedback.show();
-        } else {
-            parent.removeClass('has-danger');
-            $(element).attr('aria-describedby', '');
-            feedback.hide();
-        }
-    });
+require(['theme_noname/form-display-errors'], function(module) {
+    module.enhance({{#quote}}{{element.id}}{{/quote}});
 });
 {{/js}}
\ No newline at end of file
index 5d4d9ac..a9ac4d0 100644 (file)
     </div>
 </div>
 {{#js}}
-require(['jquery', 'core/event'], function($, Event) {
-    var element = document.getElementById('{{element.id}}');
-    $(element).on(Event.Events.FORM_FIELD_VALIDATION, function(event, msg) {
-        event.preventDefault();
-        var parent = $(element).closest('.form-group');
-        var feedback = parent.find('.form-control-feedback');
-        if (msg !== '') {
-            parent.addClass('has-danger');
-            $(element).addClass('form-control-danger');
-            $(element).attr('aria-describedby', $(element).attr('id') + '-feedback');
-            feedback.html(msg);
-            feedback.show();
-        } else {
-            parent.removeClass('has-danger');
-            $(element).removeClass('form-control-danger');
-            $(element).attr('aria-describedby', '');
-            feedback.hide();
-        }
-    });
+require(['theme_noname/form-display-errors'], function(module) {
+    module.enhance({{#quote}}{{element.id}}{{/quote}});
 });
 {{/js}}
\ No newline at end of file
index 34e1308..17dbab2 100644 (file)
     </div>
 </div>
 {{#js}}
-require(['jquery', 'core/event'], function($, Event) {
-    var element = document.getElementById('{{element.id}}');
-    $(element).on(Event.Events.FORM_FIELD_VALIDATION, function(event, msg) {
-        event.preventDefault();
-        var parent = $(element).closest('.form-group');
-        var feedback = parent.find('.form-control-feedback');
-        if (msg !== '') {
-            parent.addClass('has-danger');
-            $(element).addClass('form-control-danger');
-            $(element).attr('aria-describedby', $(element).attr('id') + '-feedback');
-            feedback.html(msg);
-            feedback.show();
-        } else {
-            parent.removeClass('has-danger');
-            $(element).removeClass('form-control-danger');
-            $(element).attr('aria-describedby', '');
-            feedback.hide();
-        }
-    });
+require(['theme_noname/form-display-errors'], function(module) {
+    module.enhance({{#quote}}{{element.id}}{{/quote}});
 });
 {{/js}}
\ No newline at end of file