MDL-40811 JavaScript: Create a course-util module to reduce code duplication
authorAndrew Nicols <andrew@nicols.co.uk>
Sun, 21 Jul 2013 21:52:35 +0000 (22:52 +0100)
committerAndrew Nicols <andrew@nicols.co.uk>
Mon, 16 Sep 2013 19:44:19 +0000 (20:44 +0100)
14 files changed:
course/yui/build/moodle-course-util-base/moodle-course-util-base-debug.js [new file with mode: 0644]
course/yui/build/moodle-course-util-base/moodle-course-util-base-min.js [new file with mode: 0644]
course/yui/build/moodle-course-util-base/moodle-course-util-base.js [new file with mode: 0644]
course/yui/build/moodle-course-util-cm/moodle-course-util-cm-debug.js [new file with mode: 0644]
course/yui/build/moodle-course-util-cm/moodle-course-util-cm-min.js [new file with mode: 0644]
course/yui/build/moodle-course-util-cm/moodle-course-util-cm.js [new file with mode: 0644]
course/yui/build/moodle-course-util-section/moodle-course-util-section-debug.js [new file with mode: 0644]
course/yui/build/moodle-course-util-section/moodle-course-util-section-min.js [new file with mode: 0644]
course/yui/build/moodle-course-util-section/moodle-course-util-section.js [new file with mode: 0644]
course/yui/src/util/build.json [new file with mode: 0644]
course/yui/src/util/js/base.js [new file with mode: 0644]
course/yui/src/util/js/cm.js [new file with mode: 0644]
course/yui/src/util/js/section.js [new file with mode: 0644]
course/yui/src/util/meta/util.json [new file with mode: 0644]

diff --git a/course/yui/build/moodle-course-util-base/moodle-course-util-base-debug.js b/course/yui/build/moodle-course-util-base/moodle-course-util-base-debug.js
new file mode 100644 (file)
index 0000000..cbeb1ba
Binary files /dev/null and b/course/yui/build/moodle-course-util-base/moodle-course-util-base-debug.js differ
diff --git a/course/yui/build/moodle-course-util-base/moodle-course-util-base-min.js b/course/yui/build/moodle-course-util-base/moodle-course-util-base-min.js
new file mode 100644 (file)
index 0000000..2d1cba0
Binary files /dev/null and b/course/yui/build/moodle-course-util-base/moodle-course-util-base-min.js differ
diff --git a/course/yui/build/moodle-course-util-base/moodle-course-util-base.js b/course/yui/build/moodle-course-util-base/moodle-course-util-base.js
new file mode 100644 (file)
index 0000000..cbeb1ba
Binary files /dev/null and b/course/yui/build/moodle-course-util-base/moodle-course-util-base.js differ
diff --git a/course/yui/build/moodle-course-util-cm/moodle-course-util-cm-debug.js b/course/yui/build/moodle-course-util-cm/moodle-course-util-cm-debug.js
new file mode 100644 (file)
index 0000000..aef40c5
Binary files /dev/null and b/course/yui/build/moodle-course-util-cm/moodle-course-util-cm-debug.js differ
diff --git a/course/yui/build/moodle-course-util-cm/moodle-course-util-cm-min.js b/course/yui/build/moodle-course-util-cm/moodle-course-util-cm-min.js
new file mode 100644 (file)
index 0000000..aa48612
Binary files /dev/null and b/course/yui/build/moodle-course-util-cm/moodle-course-util-cm-min.js differ
diff --git a/course/yui/build/moodle-course-util-cm/moodle-course-util-cm.js b/course/yui/build/moodle-course-util-cm/moodle-course-util-cm.js
new file mode 100644 (file)
index 0000000..aef40c5
Binary files /dev/null and b/course/yui/build/moodle-course-util-cm/moodle-course-util-cm.js differ
diff --git a/course/yui/build/moodle-course-util-section/moodle-course-util-section-debug.js b/course/yui/build/moodle-course-util-section/moodle-course-util-section-debug.js
new file mode 100644 (file)
index 0000000..9e38dcb
Binary files /dev/null and b/course/yui/build/moodle-course-util-section/moodle-course-util-section-debug.js differ
diff --git a/course/yui/build/moodle-course-util-section/moodle-course-util-section-min.js b/course/yui/build/moodle-course-util-section/moodle-course-util-section-min.js
new file mode 100644 (file)
index 0000000..fe8775f
Binary files /dev/null and b/course/yui/build/moodle-course-util-section/moodle-course-util-section-min.js differ
diff --git a/course/yui/build/moodle-course-util-section/moodle-course-util-section.js b/course/yui/build/moodle-course-util-section/moodle-course-util-section.js
new file mode 100644 (file)
index 0000000..9e38dcb
Binary files /dev/null and b/course/yui/build/moodle-course-util-section/moodle-course-util-section.js differ
diff --git a/course/yui/src/util/build.json b/course/yui/src/util/build.json
new file mode 100644 (file)
index 0000000..4ecd0ab
--- /dev/null
@@ -0,0 +1,20 @@
+{
+  "name": "moodle-course-util",
+  "builds": {
+    "moodle-course-util-base": {
+      "jsfiles": [
+        "base.js"
+      ]
+    },
+    "moodle-course-util-section": {
+      "jsfiles": [
+        "section.js"
+      ]
+    },
+    "moodle-course-util-cm": {
+      "jsfiles": [
+        "cm.js"
+      ]
+    }
+  }
+}
diff --git a/course/yui/src/util/js/base.js b/course/yui/src/util/js/base.js
new file mode 100644 (file)
index 0000000..4c3240d
--- /dev/null
@@ -0,0 +1,16 @@
+/**
+ * The Moodle.core_course.util classes provide course-related utility functions.
+ *
+ * @module moodle-course-util
+ * @main
+ */
+
+Y.namespace('Moodle.core_course.util');
+
+/**
+ * A collection of general utility functions for use in course.
+ *
+ * @class Moodle.core_course.util
+ * @static
+ */
+
diff --git a/course/yui/src/util/js/cm.js b/course/yui/src/util/js/cm.js
new file mode 100644 (file)
index 0000000..a6884fa
--- /dev/null
@@ -0,0 +1,70 @@
+/**
+ * A collection of utility classes for use with course modules.
+ *
+ * @module moodle-course-util
+ * @submodule moodle-course-util-cm
+ */
+
+Y.namespace('Moodle.core_course.util.cm');
+
+/**
+ * A collection of utility classes for use with course modules.
+ *
+ * @class Moodle.core_course.util.cm
+ * @static
+ */
+Y.Moodle.core_course.util.cm = {
+    CONSTANTS: {
+        MODULEIDPREFIX : 'module-'
+    },
+    SELECTORS: {
+        COURSEMODULE: '.activity',
+        INSTANCENAME: '.instancename'
+    },
+
+    /**
+     * Retrieve the course module item from one of it's child Nodes.
+     *
+     * @method getCourseModuleNodeFromComponent
+     * @param coursemodulecomponent {Node} The component Node.
+     * @return {Node|null} The Course Module Node.
+     */
+    getCourseModuleFromComponent: function(coursemodulecomponent) {
+        return Y.one(coursemodulecomponent).ancestor(this.SELECTORS.COURSEMODULE, true);
+    },
+
+    /**
+     * Determines the section ID for the provided section.
+     *
+     * @method getId
+     * @param coursemodule {Node} The course module to find an ID for.
+     * @return {Number|false} The ID of the course module in question or false if no ID was found.
+     */
+    getId: function(coursemodule) {
+        // We perform a simple substitution operation to get the ID.
+        var id = coursemodule.get('id').replace(
+                this.CONSTANTS.MODULEIDPREFIX, '');
+
+        // Attempt to validate the ID.
+        id = parseInt(id, 10);
+        if (typeof id === 'number' && isFinite(id)) {
+            return id;
+        }
+        return false;
+    },
+
+    /**
+     * Determines the section ID for the provided section.
+     *
+     * @method getName
+     * @param coursemodule {Node} The course module to find an ID for.
+     * @return {Number|false} The ID of the course module in question or false if no ID was found.
+     */
+    getName: function(coursemodule) {
+        var instance = coursemodule.one(this.SELECTORS.INSTANCENAME);
+        if (instance) {
+            return instance.get('firstChild').get('data');
+        }
+        return null;
+    }
+};
diff --git a/course/yui/src/util/js/section.js b/course/yui/src/util/js/section.js
new file mode 100644 (file)
index 0000000..1c02f75
--- /dev/null
@@ -0,0 +1,40 @@
+/**
+ * A collection of utility classes for use with course sections.
+ *
+ * @module moodle-course-util
+ * @submodule moodle-course-util-section
+ */
+
+Y.namespace('Moodle.core_course.util.section');
+
+/**
+ * A collection of utility classes for use with course sections.
+ *
+ * @class Moodle.core_course.util.section
+ * @static
+ */
+Y.Moodle.core_course.util.section = {
+    CONSTANTS: {
+        SECTIONIDPREFIX : 'section-'
+    },
+
+    /**
+     * Determines the section ID for the provided section.
+     *
+     * @method getId
+     * @param section {Node} The section to find an ID for.
+     * @return {Number|false} The ID of the section in question or false if no ID was found.
+     */
+    getId: function(section) {
+        // We perform a simple substitution operation to get the ID.
+        var id = section.get('id').replace(
+                this.CONSTANTS.SECTIONIDPREFIX, '');
+
+        // Attempt to validate the ID.
+        id = parseInt(id, 10);
+        if (typeof id === 'number' && isFinite(id)) {
+            return id;
+        }
+        return false;
+    }
+};
diff --git a/course/yui/src/util/meta/util.json b/course/yui/src/util/meta/util.json
new file mode 100644 (file)
index 0000000..5a35ea2
--- /dev/null
@@ -0,0 +1,26 @@
+{
+  "moodle-course-util": {
+    "requires": [
+        "node"
+    ],
+    "use": [
+        "moodle-course-util-base"
+    ],
+    "submodules": {
+        "moodle-course-util-base": {
+        },
+        "moodle-course-util-section": {
+            "requires": [
+                "node",
+                "moodle-course-util-base"
+            ]
+        },
+        "moodle-course-util-cm": {
+            "requires": [
+                "node",
+                "moodle-course-util-base"
+            ]
+        }
+    }
+  }
+}