Updates to lti plugin & progress on instructor management of course tools.
[moodle.git] / mod / lti / mod_form.js
CommitLineData
996b0fd9
CS
1M.mod_lti = M.mod_lti || {};
2
3M.mod_lti.editor = {
4 init: function(Y, settings){
5 this.Y = Y;
6 var self = this;
7 this.settings = Y.JSON.parse(settings);
8
9 var typeSelector = Y.one('#id_typeid');
10 typeSelector.on('change', function(e){
11 self.toggleEditButtons();
12 });
13
14 this.createTypeEditorButtons();
15
16 this.toggleEditButtons();
17 },
18
19 getSelectedToolTypeOption: function(){
20 var Y = this.Y;
21 var typeSelector = Y.one('#id_typeid');
22
23 return typeSelector.one('option[value=' + typeSelector.get('value') + ']');
24 },
25
26 /**
27 * Adds buttons for creating, editing, and deleting tool types
28 */
29 createTypeEditorButtons: function(){
30 var Y = this.Y;
31 var self = this;
32
33 var typeSelector = Y.one('#id_typeid');
34
35 var createIcon = function(id, tooltip, iconUrl){
36 return Y.Node.create('<a />')
37 .set('id', id)
38 .set('title', tooltip)
39 .setStyle('margin-left', '.5em')
40 .set('href', 'javascript:void(0);')
41 .append(Y.Node.create('<img src="' + iconUrl + '" />'));
42 }
43
44 var addIcon = createIcon('lti_add_tool_type', 'Add new tool type', this.settings.add_icon_url);
45 var editIcon = createIcon('lti_edit_tool_type', 'Edit new tool type', this.settings.edit_icon_url);
46 var deleteIcon = createIcon('lti_delete_tool_type', 'Delete tool type', this.settings.delete_icon_url);
47
48 editIcon.on('click', function(e){
49 var toolTypeId = typeSelector.get('value');
50
51 if(self.getSelectedToolTypeOption().getAttribute('editable')){
52 window.open(self.settings.instructor_tool_type_edit_url + '&action=edit&typeid=' + toolTypeId, 'edit_tool');
53 }
54 });
55
56 addIcon.on('click', function(e){
57 window.open(self.settings.instructor_tool_type_edit_url + '&action=add', 'add_tool');
58 });
59
60 deleteIcon.on('click', function(e){
61 var toolTypeId = typeSelector.get('value');
62
63 if(self.getSelectedToolTypeOption().getAttribute('editable')){
64 Y.io(self.settings.instructor_tool_type_edit_url + '&action=delete&typeid=' + toolTypeId, {
65 on: {
66 success: function(){
67 getSelectedOption().remove();
68 },
69 failure: function(){
70
71 }
72 }
73 });
74 }
75 });
76
77 typeSelector.insert(addIcon, 'after');
78 addIcon.insert(editIcon, 'after');
79 editIcon.insert(deleteIcon, 'after');
80 },
81
82 toggleEditButtons: function(){
83 var Y = this.Y;
84
85 var lti_edit_tool_type = Y.one('#lti_edit_tool_type');
86 var lti_delete_tool_type = Y.one('#lti_delete_tool_type');
87
88 if(this.getSelectedToolTypeOption().getAttribute('editable')){
89 lti_edit_tool_type.setStyle('opacity', '1');
90 lti_delete_tool_type.setStyle('opacity', '1');
91 } else {
92 lti_edit_tool_type.setStyle('opacity', '.2');
93 lti_delete_tool_type.setStyle('opacity', '.2');
94 }
95 },
96
97 addToolType: function(text, value){
98 var Y = this.Y;
99 var typeSelector = Y.one('#id_typeid');
100
101 var option = Y.Node.create('<option />')
102 .set('text', text)
103 .set('value', value)
104 .set('selected', 'selected');
105
106 typeSelector.append(option);
107 },
108
109 updateToolType: function(text, value){
110 var Y = this.Y;
111 var typeSelector = Y.one('#id_typeid');
112
113 var option = Y.Node.create('<option />')
114 .set('text', text)
115 .set('value', value)
116 .set('selected', 'selected');
117
118 typeSelector.append(option);
119 }
120
121};
122
123
124