MDL-37012 some typos, add a new lang string, change tag 'a' for 'link', some code...
[moodle.git] / lib / form / yui / listing / listing.js
blob0e420e0d8d199dea42f1246904a2cb046d2606a9
1 // This file is part of Moodle - http://moodle.org/
2 //
3 // Moodle is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
7 //
8 // Moodle is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 // GNU General Public License for more details.
13 // You should have received a copy of the GNU General Public License
14 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16 /**
17  * Form listing Javascript.
18  *
19  * It mainly handles loading the main content div when cliking on a tab/row.
20  * @copyright 2012 Jerome Mouneyrac
21  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22  */
24 M.form_listing = {};
25 M.form_listing.Y = null;
26 M.form_listing.instances = [];
28 /**
29  * This function is called for each listing form on page.
30  *
31  * @param {Array} params :  {int} hiddeninputid - the id of the hidden input element
32  *                          {int} elementid - the id of the full form element
33  *                          {Array} items - items has for key the value return by the form, and for content an array with two attributs: mainhtml and rowhtml.
34  *                          {string} hideall - button label to hide all tabs(rows).
35  *                          {string} showall - button label to show all tabs(rows).
36  *                          {string} inputname - the name of the input element
37  *                          {string} currentvalue - the currently selected tab(row)
38  */
39 M.form_listing.init = function(Y, params) {
40     if (params && params.hiddeninputid && params.elementid) {
42         // Replace the radio buttons by a hidden input.
43         Y.one('#formlistinginputcontainer_' + params.inputname).setHTML('<input name='+params.inputname+' type=hidden id='+params.hiddeninputid+' value='+params.currentvalue+' />');
45         var caption = Y.one('#'+params.elementid+'_caption');
46         var allitems = Y.one('#'+params.elementid+'_all');
47         var selecteditem = Y.one('#'+params.elementid+'_main');
48         var hiddeninput = Y.one('#'+params.hiddeninputid);
50         // Do not display the listing by default.
51         var show = 0;
52         allitems.hide();
54         // Refresh the main item + set the hidden input to its value.
55         var selectitem = function(e) {
56             var index = this.get('id').replace(params.elementid+'_all_',"");
57             hiddeninput.set('value', items[index]);
58             selecteditem.setHTML(params.items[items[index]].mainhtml);
59         }
61         // Caption Onlick event to display/hide the listing.
62         var onclick = function(e) {
63             if (!show) {
64                 allitems.show(true);
65                 show = 1;
66                 caption.setHTML(params.hideall);
67             } else {
68                 allitems.hide(true);
69                 show = 0;
70                 caption.setHTML(params.showall);
71             }
72         };
74         caption.on('click', onclick);
76         // Fill the item rows with html + add event.
77         // PS: we need to save the items into a temporary "items[]" array because params.items keys could be url.
78         // This temporary items[] avoid not working calls like Y.one('#myitems_http:www.google.com').
79         var items = [];
80         var itemindex = 0;
81         for (itemid in params.items) {
82             items[itemindex] = itemid;
84             // Add the row.
85             allitems.append("<div id="+params.elementid+'_all_'+itemindex+" class='formlistingrow'>" + params.items[itemid].rowhtml + "</div>");
87             // Add click event to the row.
88             Y.one('#'+params.elementid+'_all_'+itemindex).on('click', selectitem);
90             itemindex = itemindex + 1;
91         }
92     }