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