[REF] Migrated createFormFromMap to jQuery
[cds-indico.git] / indico / ext / livesync / htdocs / js / livesync.js
blob164c0c92e07b8403dfc100a9a13264d4731f7c1b
1 /* This file is part of Indico.
2  * Copyright (C) 2002 - 2012 European Organization for Nuclear Research (CERN).
3  *
4  * Indico is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or (at your option) any later version.
8  *
9  * Indico is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with Indico; if not, see <http://www.gnu.org/licenses/>.
16  */
18 function activateAgentText(agentId) {
19     return Html.div(
20         {style: {width: '350px'}},
21         $T('Activation is done in two simple steps.'),
22         $T('The first step requires the remote service to be synchronized with ' +
23            'Indico. In order to do so, you should run the following command:'),
24         Html.div({style: {fontFamily: 'monospace',
25                           marginTop: '10px',
26                           marginBottom: '10px'}},
27                  'indico_livesync agent ' + agentId + ' export'),
28         Html.div({}, $T("When it finishes, press the 'activate' button in the agent list.")),
29         Html.div({}, $T("Press 'Cancel' if you don't want to proceed with the activation.")))
32 function agentRequest(method, agentId) {
33     indicoRequest(
34         method,
35         {'id': agentId},
36         function(result, error) {
37             if (error) {
38                 IndicoUtil.errorReport(error);
39             } else {
40                 window.location.reload();
41             }
42         });
45 type("AgentModificationDialog", ["ExclusivePopupWithButtons"],
46      {
47          _submit: function(info, addMode) {
49              var self = this;
51              if(this.parameterManager.check() && this.pmExtra.check()) {
52                  indicoRequest(
53                      addMode?'livesync.addAgent':'livesync.editAgent',
54                      info,
55                      function(result, error) {
56                          if (error) {
57                              IndicoUtil.errorReport(error);
58                          } else {
59                              window.location.reload();
60                          }
61                      });
62              } else {
63                  new AlertPopup($T("Warning"), $T("Please fill in the missing parameters")).open();
64              }
65          },
67          draw: function() {
68              var self = this;
70              this.parameterManager = new IndicoUtil.parameterManager();
71              this.pmExtra = new IndicoUtil.parameterManager();
73              var selectType = Widget.select(this._availableTypes);
74              var idField = new RealtimeTextBox();
76              if (!this.fullyEditable) {
77                  $(selectType.dom).attr('disabled', 'disabled');
78                  idField.disable();
79              } else {
80                  this.parameterManager.add(selectType, 'text', false);
81                  this.parameterManager.add(idField, 'text', false);
82              }
84              var mainContent = IndicoUtil.createFormFromMap([
85                  [$T('Type'),
86                   $B(selectType,
87                      this.info.accessor('type'))],
88                  [$T('Agent ID'),
89                   $B(idField,
90                      this.info.accessor('id')).draw()],
91                  [$T('Name'),
92                  $B(this.parameterManager.add(new RealtimeTextBox(), 'text', false),
93                     this.info.accessor('name')).draw()],
94                  [$T('Description'),
95                   $B(this.parameterManager.add(new RealtimeTextArea({}), 'text', false),
96                     this.info.accessor('description')).draw()]]
97              );
99              var specificOptions = bind.element(
100                  Html.tbody({}),
101                  this._extraOptions,
102                  function(option) {
103                      return Html.tr(
104                          {},
105                          Html.td("popUpLabel", option.get()),
106                          Html.td({},
107                                  $B(self.pmExtra.add(new RealtimeTextBox(), 'text', false),
108                                     self.info.get('specific').accessor(option.key)).draw()));
109                  });
111              if (this.fullyEditable) {
112                  selectType.observe(function(agentType) {
113                      self.pmExtra.clear();
114                      self.info.set('specific', $O())
115                      self._resetForm(agentType);
116                  });
117              }
119              return this.ExclusivePopupWithButtons.prototype.draw.call(this, $('<div></div>').append(mainContent, $('<table></table>').append($(specificOptions.dom))));
120          },
122          _getButtons: function() {
123              var self = this;
124              return [
125                  [this.fullyEditable ? $T('Add') : $T('Save'), function() {
126                      self._submit(self.info, self.fullyEditable);
127                  }]
128              ];
129          }
131      });
134 type("AddAgentDialog", ["AgentModificationDialog"],
135      {
137          _resetForm: function(agentType) {
138              this._extraOptions.clear()
139              this._extraOptions.update(this._allExtraOptions[agentType])
141              var self = this;
142              each(this.info, function(value, key) {
143                  if (key != 'type' && key != 'specific') {
144                      self.info.set(key, undefined);
145                  }
146              });
148              each(this.info.get('specific'), function(value, key) {
149                  self.info.set(key, undefined);
150              });
152              this.info.set('type', agentType);
153          },
155          postDraw: function() {
156              var defaultType = keys(this._allExtraOptions)[0];
157              this._resetForm(defaultType);
158              this.ExclusivePopupWithButtons.prototype.postDraw.call(this);
159          }
161      },
162      function(availableTypes, allExtraOptions) {
163          var self = this;
165          this.info = $O({'specific': $O()});
166          this.fullyEditable = true;
168          this._availableTypes = availableTypes;
169          this._extraOptions = $O(allExtraOptions[keys(this._allExtraOptions)[0]]);
171          this._allExtraOptions = allExtraOptions;
173          this.ExclusivePopupWithButtons($T("Add New Agent"),
174                                         function() {
175                                             self.close();
176                                         });
177      });
180 type("EditAgentDialog", ["AgentModificationDialog"],
181      {
182      },
183      function(availableTypes, extraOptions, info) {
185          var self = this;
186          this.info = watchize(info)
188          this._availableTypes = availableTypes;
189          this._extraOptions = extraOptions;
191          this.ExclusivePopupWithButtons($T("Edit Agent"),
192                                         function() {
193                                             self.close();
194                                         });
195      });