Merge branch 'MDL-67818-check-api-fixes' of https://github.com/brendanheywood/moodle
[moodle.git] / course / format / topics / format.js
CommitLineData
9f3015ec 1// Javascript functions for Topics course format
d95b77bd
RK
2
3M.course = M.course || {};
4
5M.course.format = M.course.format || {};
6
7/**
405eaac2 8 * Get sections config for this format
d95b77bd 9 *
405eaac2
RK
10 * The section structure is:
11 * <ul class="topics">
12 * <li class="section">...</li>
13 * <li class="section">...</li>
14 * ...
15 * </ul>
16 *
17 * @return {object} section list configuration
d95b77bd 18 */
405eaac2
RK
19M.course.format.get_config = function() {
20 return {
21 container_node : 'ul',
22 container_class : 'topics',
23 section_node : 'li',
24 section_class : 'section'
25 };
d95b77bd
RK
26}
27
28/**
29 * Swap section
30 *
31 * @param {YUI} Y YUI3 instance
32 * @param {string} node1 node to swap to
33 * @param {string} node2 node to swap with
34 * @return {NodeList} section list
35 */
36M.course.format.swap_sections = function(Y, node1, node2) {
37 var CSS = {
38 COURSECONTENT : 'course-content',
a7d0e1fd 39 SECTIONADDMENUS : 'section_add_menus'
d95b77bd
RK
40 };
41
42 var sectionlist = Y.Node.all('.'+CSS.COURSECONTENT+' '+M.course.format.get_section_selector(Y));
b3f19d35
JD
43 // Swap the non-ajax menus, noting these are not always present (depends on theme and user prefs).
44 if (sectionlist.item(node1).one('.'+CSS.SECTIONADDMENUS)) {
45 sectionlist.item(node1).one('.'+CSS.SECTIONADDMENUS).swap(sectionlist.item(node2).one('.'+CSS.SECTIONADDMENUS));
46 }
d95b77bd 47}
9f3015ec
RK
48
49/**
50 * Process sections after ajax response
51 *
52 * @param {YUI} Y YUI3 instance
dc489dff 53 * @param {NodeList} sectionlist of sections
9f3015ec
RK
54 * @param {array} response ajax response
55 * @param {string} sectionfrom first affected section
56 * @param {string} sectionto last affected section
57 * @return void
58 */
59M.course.format.process_sections = function(Y, sectionlist, response, sectionfrom, sectionto) {
60 var CSS = {
61 SECTIONNAME : 'sectionname'
dafa9127
GB
62 },
63 SELECTORS = {
2c77be65 64 SECTIONLEFTSIDE : '.left .section-handle .icon'
9f3015ec
RK
65 };
66
67 if (response.action == 'move') {
cbddba18
GB
68 // If moving up swap around 'sectionfrom' and 'sectionto' so the that loop operates.
69 if (sectionfrom > sectionto) {
70 var temp = sectionto;
71 sectionto = sectionfrom;
72 sectionfrom = temp;
73 }
dafa9127
GB
74
75 // Update titles and move icons in all affected sections.
76 var ele, str, stridx, newstr;
77
9f3015ec 78 for (var i = sectionfrom; i <= sectionto; i++) {
dafa9127 79 // Update section title.
5b971892
DM
80 var content = Y.Node.create('<span>' + response.sectiontitles[i] + '</span>');
81 sectionlist.item(i).all('.'+CSS.SECTIONNAME).setHTML(content);
dc489dff
NM
82 // Update the drag handle.
83 ele = sectionlist.item(i).one(SELECTORS.SECTIONLEFTSIDE).ancestor('.section-handle');
84 str = ele.getAttribute('title');
dafa9127
GB
85 stridx = str.lastIndexOf(' ');
86 newstr = str.substr(0, stridx +1) + i;
dc489dff
NM
87 ele.setAttribute('title', newstr);
88 // Update the aria-label for the section.
89 sectionlist.item(i).setAttribute('aria-label', content.get('innerText').trim());
9f3015ec
RK
90 }
91 }
92}