MDL-67264 core_course: Begin set up for Activity chooser
[moodle.git] / course / yui / build / moodle-course-modchooser / moodle-course-modchooser.js
CommitLineData
43736b7d
AN
1YUI.add('moodle-course-modchooser', function (Y, NAME) {
2
3/**
4 * The activity chooser dialogue for courses.
5 *
1f777e5c 6 * @module moodle-course-modchooser
43736b7d
AN
7 */
8
9var CSS = {
3a0bc0fd 10 PAGECONTENT: 'body',
b791ee3d 11 SECTION: null,
cd2efd12 12 SECTIONMODCHOOSER: 'button.section-modchooser-link',
63e4df60 13 SITEMENU: '.block_site_main_menu',
3a0bc0fd 14 SITETOPIC: 'div.sitetopic'
43736b7d
AN
15};
16
17var MODCHOOSERNAME = 'course-modchooser';
18
19/**
20 * The activity chooser dialogue for courses.
21 *
22 * @constructor
23 * @class M.course.modchooser
24 * @extends M.core.chooserdialogue
25 */
26var MODCHOOSER = function() {
27 MODCHOOSER.superclass.constructor.apply(this, arguments);
28};
29
30Y.extend(MODCHOOSER, M.core.chooserdialogue, {
1f777e5c
AN
31 /**
32 * The current section ID.
33 *
34 * @property sectionid
35 * @private
36 * @type Number
37 * @default null
38 */
3a0bc0fd 39 sectionid: null,
43736b7d 40
1f777e5c
AN
41 /**
42 * Set up the activity chooser.
43 *
44 * @method initializer
45 */
3a0bc0fd 46 initializer: function() {
b791ee3d
AN
47 var sectionclass = M.course.format.get_sectionwrapperclass();
48 if (sectionclass) {
49 CSS.SECTION = '.' + sectionclass;
50 }
43736b7d
AN
51 var dialogue = Y.one('.chooserdialoguebody');
52 var header = Y.one('.choosertitle');
53 var params = {};
54 this.setup_chooser_dialogue(dialogue, header, params);
55
56 // Initialize existing sections and register for dynamically created sections
57 this.setup_for_section();
58 M.course.coursebase.register_module(this);
43736b7d 59 },
1f777e5c 60
43736b7d
AN
61 /**
62 * Update any section areas within the scope of the specified
63 * selector with AJAX equivalents
64 *
1f777e5c 65 * @method setup_for_section
43736b7d 66 * @param baseselector The selector to limit scope to
43736b7d 67 */
3a0bc0fd 68 setup_for_section: function(baseselector) {
43736b7d
AN
69 if (!baseselector) {
70 baseselector = CSS.PAGECONTENT;
71 }
72
73 // Setup for site topics
74 Y.one(baseselector).all(CSS.SITETOPIC).each(function(section) {
75 this._setup_for_section(section);
76 }, this);
77
78 // Setup for standard course topics
b791ee3d
AN
79 if (CSS.SECTION) {
80 Y.one(baseselector).all(CSS.SECTION).each(function(section) {
81 this._setup_for_section(section);
82 }, this);
83 }
43736b7d
AN
84
85 // Setup for the block site menu
86 Y.one(baseselector).all(CSS.SITEMENU).each(function(section) {
87 this._setup_for_section(section);
88 }, this);
89 },
1f777e5c
AN
90
91 /**
92 * Update any section areas within the scope of the specified
93 * selector with AJAX equivalents
94 *
95 * @method _setup_for_section
96 * @private
97 * @param baseselector The selector to limit scope to
98 */
3a0bc0fd 99 _setup_for_section: function(section) {
43736b7d
AN
100 var chooserspan = section.one(CSS.SECTIONMODCHOOSER);
101 if (!chooserspan) {
102 return;
103 }
104 var chooserlink = Y.Node.create("<a href='#' />");
105 chooserspan.get('children').each(function(node) {
106 chooserlink.appendChild(node);
107 });
108 chooserspan.insertBefore(chooserlink);
109 chooserlink.on('click', this.display_mod_chooser, this);
110 },
111 /**
1f777e5c
AN
112 * Display the module chooser
113 *
114 * @method display_mod_chooser
115 * @param {EventFacade} e Triggering Event
116 */
3a0bc0fd 117 display_mod_chooser: function(e) {
43736b7d
AN
118 // Set the section for this version of the dialogue
119 if (e.target.ancestor(CSS.SITETOPIC)) {
120 // The site topic has a sectionid of 1
121 this.sectionid = 1;
122 } else if (e.target.ancestor(CSS.SECTION)) {
123 var section = e.target.ancestor(CSS.SECTION);
124 this.sectionid = section.get('id').replace('section-', '');
125 } else if (e.target.ancestor(CSS.SITEMENU)) {
126 // The block site menu has a sectionid of 0
127 this.sectionid = 0;
128 }
129 this.display_chooser(e);
130 },
1f777e5c 131
1f777e5c
AN
132 /**
133 * Helper function to set the value of a hidden radio button when a
134 * selection is made.
135 *
136 * @method option_selected
137 * @param {String} thisoption The selected option value
138 * @private
139 */
3a0bc0fd 140 option_selected: function(thisoption) {
80cd5086
AN
141 // Add the sectionid to the URL.
142 this.hiddenRadioValue.setAttrs({
143 name: 'jump',
144 value: thisoption.get('value') + '&section=' + this.sectionid
145 });
43736b7d
AN
146 }
147},
148{
3a0bc0fd
DP
149 NAME: MODCHOOSERNAME,
150 ATTRS: {
1f777e5c
AN
151 /**
152 * The maximum height (in pixels) of the activity chooser.
153 *
154 * @attribute maxheight
155 * @type Number
156 * @default 800
157 */
3a0bc0fd
DP
158 maxheight: {
159 value: 800
43736b7d
AN
160 }
161 }
162});
163M.course = M.course || {};
164M.course.init_chooser = function(config) {
165 return new MODCHOOSER(config);
166};
167
168
169}, '@VERSION@', {"requires": ["moodle-core-chooserdialogue", "moodle-course-coursebase"]});