f87a5ec85c5d53d6fbbcf29ef4e96ac6f41518f2
[moodle.git] / course / yui / modchooser / modchooser.js
1 YUI.add('moodle-course-modchooser', function(Y) {
2     var CSS = {
3         PAGECONTENT : 'div#page-content',
4         SECTION : 'li.section',
5         SECTIONMODCHOOSER : 'span.section-modchooser-link',
6         SITEMENU : 'div.block_site_main_menu',
7         SITETOPIC : 'div.sitetopic'
8     };
10     var MODCHOOSERNAME = 'course-modchooser';
12     var MODCHOOSER = function() {
13         MODCHOOSER.superclass.constructor.apply(this, arguments);
14     }
16     Y.extend(MODCHOOSER, M.core.chooserdialogue, {
17         // The current section ID
18         sectionid : null,
20         // The hidden element holding the jump param
21         jumplink : null,
23         initializer : function(config) {
24             var dialogue = Y.one('.chooserdialoguebody');
25             var header = Y.one('.choosertitle');
26             var params = {
27                 width: '540px'
28             };
29             this.setup_chooser_dialogue(dialogue, header, params);
31             this.jumplink = this.container.one('#jump');
33             // Initialize existing sections and register for dynamically created sections
34             this.setup_for_section();
35             M.course.coursebase.register_module(this);
37             // Catch the page toggle
38             Y.all('.block_settings #settingsnav .type_course .modchoosertoggle a').on('click', this.toggle_mod_chooser, this);
39         },
40         /**
41          * Update any section areas within the scope of the specified
42          * selector with AJAX equivalents
43          *
44          * @param baseselector The selector to limit scope to
45          * @return void
46          */
47         setup_for_section : function(baseselector) {
48             if (!baseselector) {
49                 var baseselector = CSS.PAGECONTENT;
50             }
52             // Setup for site topics
53             Y.one(baseselector).all(CSS.SITETOPIC).each(function(section) {
54                 this._setup_for_section(section);
55             }, this);
57             // Setup for standard course topics
58             Y.one(baseselector).all(CSS.SECTION).each(function(section) {
59                 this._setup_for_section(section);
60             }, this);
62             // Setup for the block site menu
63             Y.one(baseselector).all(CSS.SITEMENU).each(function(section) {
64                 this._setup_for_section(section);
65             }, this);
66         },
67         _setup_for_section : function(section, sectionid) {
68             var chooserspan = section.one(CSS.SECTIONMODCHOOSER);
69             if (!chooserspan) {
70                 return;
71             }
72             var chooserlink = Y.Node.create("<a href='#' />");
73             chooserspan.get('children').each(function(node) {
74                 chooserlink.appendChild(node);
75             });
76             chooserspan.insertBefore(chooserlink);
77             chooserlink.on('click', this.display_mod_chooser, this);
78         },
79         /**
80          * Display the module chooser
81          *
82          * @param e Event Triggering Event
83          * @param secitonid integer The ID of the section triggering the dialogue
84          * @return void
85          */
86         display_mod_chooser : function (e) {
87             // Set the section for this version of the dialogue
88             if (e.target.ancestor(CSS.SITETOPIC)) {
89                 // The site topic has a sectionid of 1
90                 this.sectionid = 1;
91             } else if (e.target.ancestor(CSS.SECTION)) {
92                 var section = e.target.ancestor(CSS.SECTION);
93                 this.sectionid = section.get('id').replace('section-', '');
94             } else if (e.target.ancestor(CSS.SITEMENU)) {
95                 // The block site menu has a sectionid of 0
96                 this.sectionid = 0;
97             }
98             this.display_chooser(e);
99         },
100         toggle_mod_chooser : function(e) {
101             // Get the add section link
102             var modchooserlinks = Y.all('div.addresourcemodchooser');
104             // Get the dropdowns
105             var dropdowns = Y.all('div.addresourcedropdown');
107             if (modchooserlinks.size() == 0) {
108                 // Continue with non-js action if there are no modchoosers to add
109                 return;
110             }
112             // We need to update the text and link
113             var togglelink = Y.one('.block_settings #settingsnav .type_course .modchoosertoggle a');
115             // The actual text is in the last child
116             var toggletext = togglelink.get('lastChild');
118             var usemodchooser;
119             // Determine whether they're currently hidden
120             if (modchooserlinks.item(0).hasClass('visibleifjs')) {
121                 // The modchooser is currently visible, hide it
122                 usemodchooser = 0;
123                 modchooserlinks
124                     .removeClass('visibleifjs')
125                     .addClass('hiddenifjs');
126                 dropdowns
127                     .addClass('visibleifjs')
128                     .removeClass('hiddenifjs');
129                 toggletext.set('data', M.util.get_string('modchooserenable', 'moodle'));
130                 togglelink.set('href', togglelink.get('href').replace('off', 'on'));
131             } else {
132                 // The modchooser is currently not visible, show it
133                 usemodchooser = 1;
134                 modchooserlinks
135                     .addClass('visibleifjs')
136                     .removeClass('hiddenifjs');
137                 dropdowns
138                     .removeClass('visibleifjs')
139                     .addClass('hiddenifjs');
140                 toggletext.set('data', M.util.get_string('modchooserdisable', 'moodle'));
141                 togglelink.set('href', togglelink.get('href').replace('on', 'off'));
142             }
144             M.util.set_user_preference('usemodchooser', usemodchooser);
146             // Prevent the page from reloading
147             e.preventDefault();
148         },
149         option_selected : function(thisoption) {
150             // Add the sectionid to the URL
151             this.jumplink.set('value', thisoption.get('value') + '&section=' + this.sectionid);
152         }
153     },
154     {
155         NAME : MODCHOOSERNAME,
156         ATTRS : {
157         }
158     });
159     M.course = M.course || {};
160     M.course.init_chooser = function(config) {
161         return new MODCHOOSER(config);
162     }
163 },
164 '@VERSION@', {
165     requires:['base', 'overlay', 'moodle-core-chooserdialogue', 'transition']
167 );