summaryrefslogtreecommitdiffstats
path: root/core/js/oc-dialogs.js
blob: 35d0a0c5c425cce4594b743ad689ad414979b502 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/**
 * ownCloud
 *
 * @author Bartek Przybylski
 * @copyright 2012 Bartek Przybylski bartek@alefzero.eu
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
 *
 * You should have received a copy of the GNU Affero General Public
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
 *
 * todo(bartek): add select option in form
 */

/**
 * this class ease usage of jquery dialogs
 */
OCdialogs = {
  /**
   * displays alert dialog
   * @param text content of dialog
   * @param title dialog title
   * @param callback which will be triggered when user press OK
   */
  alert:function(text, title, callback) {
    var content = '<p><span class="ui-icon ui-icon-alert"></span>'+text+'</p>';
    OCdialogs.message(content, title, OCdialogs.ALERT_DIALOG, OCdialogs.OK_BUTTON, callback);
  },
  /**
   * displays info dialog
   * @param text content of dialog
   * @param title dialog title
   * @param callback which will be triggered when user press OK
   */
  info:function(text, title, callback) {
    var content = '<p><span class="ui-icon ui-icon-info"></span>'+text+'</p>';
    OCdialogs.message(content, title, OCdialogs.ALERT_DIALOG, OCdialogs.OK_BUTTON, callback);
  },
  /**
   * displays confirmation dialog
   * @param text content of dialog
   * @param title dialog title
   * @param callback which will be triggered when user press YES or NO (true or false would be passed to callback respectively)
   */
  confirm:function(text, title, callback) {
    var content = '<p><span class="ui-icon ui-icon-notice"></span>'+text+'</p>';
    OCdialogs.message(content, title, OCdialogs.ALERT_DIALOG, OCdialogs.YES_NO_BUTTONS, callback);
  },
  /**
   * prompt for user input
   * @param text content of dialog
   * @param title dialog title
   * @param callback which will be triggered when user press OK (input text will be passed to callback)
   */
  prompt:function(text, title, default_value, callback) {
    var content = '<p><span class="ui-icon ui-icon-pencil"></span>'+text+':<br/><input type="text" id="oc-dialog-prompt-input" value="'+default_value+'" style="width:90%"></p>';
    OCdialogs.message(content, title, OCdialogs.PROMPT_DIALOG, OCdialogs.OK_CANCEL_BUTTONS, callback);
  },
  /**
   * prompt user for input with custom form
   * fields should be passed in following format: [{text:'prompt text', name:'return name', type:'input type', value: 'dafault value'},...]
   * @param fields to display 
   * @param title dialog title
   * @param callback which will be triggered when user press OK (user answers will be passed to callback in following format: [{name:'return name', value: 'user value'},...])
   */
  form:function(fields, title, callback) {
    var content = '<table>';
    for (var a in fields) {
      content += '<tr><td>'+fields[a].text+'</td><td>';
      var type=fields[a].type;
      if (type == 'text' || type == 'checkbox' || type == 'password') {
          content += '<input type="'+type+'" name="'+fields[a].name+'"';
          if (type == 'checkbox') {
            if (fields[a].value != undefined && fields[a].value == true) {
              content += ' checked="checked">';
            } else content += '>';
          } else if (type == 'text' || type == 'password' && fields[a].value)
            content += ' value="'+fields[a].value+'">';
      } else if (type == 'select') {
        content += '<select name="'+fields[a].name+'"';
        if (fields[a].value != undefined)
          content += ' value="'+fields[a].value+'"';
        content += '>';
        for (var o in fields[a].options)
          content += '<option value="'+fields[a].options[o].value+'">'+fields[a].options[o].text+'</option>';
        content += '</select>';
      }
      content += '</td></tr>';
    }
    content += '</table>';
    OCdialogs.message(content, title, OCdialogs.FORM_DIALOG, OCdialogs.OK_CANCEL_BUTTONS, callback);
  },
  message:function(content, title, dialog_type, buttons, callback) {
    var c_name = 'oc-dialog-'+OCdialogs.dialogs_counter+'-content';
    var c_id = '#'+c_name;
    var d = '<div id="'+c_name+'" title="'+title+'">'+content+'</div>';
    $('body').append(d);
    var b = [];
    switch (buttons) {
      case OCdialogs.YES_NO_BUTTONS:
        b[1] = {text: t('dialogs', 'No'), click: function(){ if (callback != undefined) callback(false); $(c_id).dialog('close'); }};
        b[0] = {text: t('dialogs', 'Yes'), click: function(){ if (callback != undefined) callback(true); $(c_id).dialog('close');}};
      break;
      case OCdialogs.OK_CANCEL_BUTTONS:
        b[1] = {text: t('dialogs', 'Cancel'), click: function(){$(c_id).dialog('close'); }};
      case OCdialogs.OK_BUTTON: // fallthrough
        var f;
        switch(dialog_type) {
          case OCdialogs.ALERT_DIALOG:
            f = function(){$(c_id).dialog('close'); };
          break;
          case OCdialogs.PROMPT_DIALOG:
            f = function(){OCdialogs.prompt_ok_handler(callback, c_id)};
          break;
          case OCdialogs.FORM_DIALOG:
            f = function(){OCdialogs.form_ok_handler(callback, c_id)};
          break;
        }
        b[0] = {text: t('dialogs', 'Ok'), click: f};
      break;
    }
    var possible_height = ($('tr', d).size()+1)*30;
    $(c_id).dialog({width: 4*$(document).width()/9, height: possible_height + 120, modal: false, buttons: b});
    OCdialogs.dialogs_counter++;
  },
  // dialogs buttons types
  YES_NO_BUTTONS: 70,
  OK_BUTTONS: 71,
  OK_CANCEL_BUTTONS: 72,
  // dialogs types
  ALERT_DIALOG: 80,
  INFO_DIALOG: 81,
  PROMPT_DIALOG: 82,
  FORM_DIALOG: 83,
  dialogs_counter: 0,
  determineValue: function(element) {
    switch ($(element).attr('type')) {
      case 'checkbox': return element.checked;
    }
    return $(element).val();
  },
  prompt_ok_handler: function(callback, c_id) { $(c_id).dialog('close'); if (callback != undefined) callback($(c_id + " input#oc-dialog-prompt-input").val()); },
  form_ok_handler: function(callback, c_id) {
    if (callback != undefined) {
      var r = [];
      var c = 0;
      $(c_id + ' input, '+c_id+' select').each(function(i, elem) {
        r[c] = {name: $(elem).attr('name'), value: OCdialogs.determineValue(elem)};
        c++;
      });
      $(c_id).dialog('close');
      callback(r);
    } else {
      $(c_id).dialog('close');
    }
  }
};