summaryrefslogtreecommitdiff
path: root/modules/gallery/js/l10n_client.js
blob: 58032a48b7aa15919c8e7bc34d3a3671d3cbf76d (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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
// Fork from Drupal's l10n_client module, originally written by:
// G‡bor Hojtsy http://drupal.org/user/4166 (original author)
// Young Hahn / Development Seed - http://developmentseed.org/ (friendly user interface)

var Gallery = Gallery || { 'behaviors': {} };

Gallery.attachBehaviors = function(context) {
  context = context || document;
  // Execute all of them.
  jQuery.each(Gallery.behaviors,
              function() {
                  this(context);
              });
};

$(document).ready(function() {
  Gallery.attachBehaviors(this);
});

// Store all l10n_client related data + methods in its own object
jQuery.extend(Gallery, {
  l10nClient: new (function() {
    // Set "selected" string to unselected, i.e. -1
    this.selected = -1;
    // Keybindings
    this.keys = {'toggle':'ctrl+shift+s', 'clear': 'esc'}; // Keybindings
    // Keybinding functions
    this.key = function(pressed) {
      switch(pressed) {
        case 'toggle':
          // Grab user-hilighted text & send it into the search filter
          userSelection = window.getSelection ? window.getSelection() : document.getSelection ? document.getSelection() : document.selection.createRange().text;
          userSelection = String(userSelection);
          if(userSelection.length > 0) {
            Gallery.l10nClient.filter(userSelection);
            Gallery.l10nClient.toggle(1);
            $('#l10n-client #g-l10n-search').focus();
          } else {
            if($('#l10n-client').is('.hidden')) {
              Gallery.l10nClient.toggle(1);
              if(!$.browser.safari) {
                $('#l10n-client #g-l10n-search').focus();
              }
            } else {
              Gallery.l10nClient.toggle(0);
            }
          }
        break;
        case 'clear':
          this.filter(false);
        break;
      }
    };

    // Toggle the l10nclient
    this.toggle = function(state) {
      switch(state) {
        case 1:
          $('#l10n-client-string-select, #l10n-client-string-editor, #l10n-client .labels .label').show();
          $('#l10n-client').height('22em').removeClass('hidden');
					//$('#l10n-client').slideUp();
					$('#g-minimize-l10n').text("_");
        /*
         * This CSS clashes with Gallery's CSS, probably due to
         * YUI's grid / floats.
          if(!$.browser.msie) {
              $('body').css('border-bottom', '22em solid #fff');
          }
        */
          $.cookie('Gallery_l10n_client', '1', {expires: 7, path: '/'});
        break;
        case 0:
          $('#l10n-client-string-select, #l10n-client-string-editor, #l10n-client .labels .label').hide();
          $('#l10n-client').height('2em').addClass('hidden');
          // TODO: Localize this message
					$('#g-minimize-l10n').text(MSG_TRANSLATE_TEXT);
        /*
          if(!$.browser.msie) {
            $('body').css('border-bottom', '0px');
          }
        */
          $.cookie('Gallery_l10n_client', '0', {expires: 7, path: '/'});
        break;
      }
    };

    // Get a string from the DOM tree
    this.getString = function(index, type) {
      return l10n_client_data[index][type];
    };

    // Set a string in the DOM tree
    this.setString = function(index, data) {
      l10n_client_data[index]['translation'] = data;
    };

    // Display the source message
    this.showSourceMessage = function(source, is_plural) {
      if (is_plural) {
        var pretty_source = $('#source-text-tmp-space').text('[one] - ' + source['one']).html();
        pretty_source += '<br/>';
        pretty_source += $('#source-text-tmp-space').text('[other] - ' + source['other']).html();
      } else {
        var pretty_source = $('#source-text-tmp-space').text(source).html();
      }
      $('#l10n-client-string-editor .source-text').html(pretty_source);
    };
    this.isPluralMessage = function(message) {
      return typeof(message) == 'object';
    };

    this.updateTranslationForm = function(translation, is_plural) {
      $('.translationField').addClass('hidden');
      if (is_plural) {
        if (typeof(translation) != 'object') {
          translation = {};
        }
        var num_plural_forms = plural_forms.length;
        for (var i = 0; i < num_plural_forms; i++) {
          var form = plural_forms[i];
          if (translation[form] == undefined) {
            translation[form] = '';
          }
          $('#l10n-edit-plural-translation-' + form)
              .attr('value', translation[form]);
          $('#plural-' + form).removeClass('hidden');
        }
      } else {
        $('#l10n-edit-translation').attr('value', translation);
        $('#l10n-edit-translation').removeClass('hidden');
      }
    };

    // Filter the string list by a search string
    this.filter = function(search) {
      if(search == false || search == '') {
        $('#l10n-client #l10n-search-filter-clear').focus();
        $('#l10n-client-string-select li').show();
        $('#l10n-client #g-l10n-search').val('');
        $('#l10n-client #g-l10n-search').focus();
      } else {
        if(search.length > 0) {
          $('#l10n-client-string-select li').hide();
          $('#l10n-client-string-select li:contains('+search+')').show();
          $('#l10n-client #g-l10n-search').val(search);
        }
      }
    };

    this.copySourceText = function() {
      var index  = Gallery.l10nClient.selected;
      if (index >= 0) {
        var source = Gallery.l10nClient.getString(index, 'source');
        var is_plural = Gallery.l10nClient.isPluralMessage(source);
        if (is_plural) {
          if (typeof(translation) != 'object') {
            translation = {};
          }
          var num_plural_forms = plural_forms.length;
          for (var i = 0; i < num_plural_forms; i++) {
            var form = plural_forms[i];
            var text = source['other'];
            if (form == 'one') {
              text = source['one'];
            }
            $('#l10n-edit-plural-translation-' + form)
                .attr('value', text);
          }
        } else {
          $('#l10n-edit-translation').attr('value', source);
        }

      }
    };
  })
});

// Attaches the localization editor behavior to all required fields.
Gallery.behaviors.l10nClient = function(context) {

  switch($.cookie('Gallery_l10n_client')) {
    case '1':
      Gallery.l10nClient.toggle(1);
    break;
    default:
      Gallery.l10nClient.toggle(0);
    break;
  }

  // If the selection changes, copy string values to the source and target fields.
  // Add class to indicate selected string in list widget.
  $('#l10n-client-string-select li').click(function() {
    $('#l10n-client-string-select li').removeClass('active');
    $(this).addClass('active');
    var index = $('#l10n-client-string-select li').index(this);
    var source = Gallery.l10nClient.getString(index, 'source');
    var key = Gallery.l10nClient.getString(index, 'key');
    var is_plural = Gallery.l10nClient.isPluralMessage(source);
    Gallery.l10nClient.showSourceMessage(source, is_plural);
    Gallery.l10nClient.updateTranslationForm(Gallery.l10nClient.getString(index, 'translation'), is_plural);
    $("#g-l10n-client-save-form input[name='l10n-message-key']").val(key);
    Gallery.l10nClient.selected = index;
  });

  // When l10n_client window is clicked, toggle based on current state.
  $('#g-minimize-l10n').click(function() {
    if($('#l10n-client').is('.hidden')) {
      Gallery.l10nClient.toggle(1);
    } else {
      Gallery.l10nClient.toggle(0);
    }
  });

  // Close the l10n client using an AJAX call and refreshing the page
  $('#g-close-l10n').click(function(event) {
		$.ajax({
      type: "GET",
      url: toggle_l10n_mode_url,
      data: "csrf=" + csrf,
      success: function() {
        window.location.reload(true);
      }
    });
		event.preventDefault();
  });

  // Register keybindings using jQuery hotkeys
  // TODO: Either remove hotkeys code or add query.hotkeys.js.
  if($.hotkeys) {
    $.hotkeys.add(Gallery.l10nClient.keys['toggle'], function(){Gallery.l10nClient.key('toggle');});
    $.hotkeys.add(Gallery.l10nClient.keys['clear'], {target:'#l10n-client #g-l10n-search', type:'keyup'}, function(){Gallery.l10nClient.key('clear');});
  }

  // Custom listener for l10n_client livesearch
  $('#l10n-client #g-l10n-search').keyup(function(key) {
    Gallery.l10nClient.filter($('#l10n-client #g-l10n-search').val());
  });

  // Clear search
  $('#l10n-client #l10n-search-filter-clear').click(function() {
    Gallery.l10nClient.filter(false);
    return false;
  });

  // Send AJAX POST data on form submit.
  $('#g-l10n-client-save-form').ajaxForm({
      dataType: "json",
      success: function(data) {
              var source = Gallery.l10nClient.getString(Gallery.l10nClient.selected, 'source');
              var is_plural = Gallery.l10nClient.isPluralMessage(source);
              var num_plural_forms = plural_forms.length;

              // Store translation in local js
              var translation = {};
              if (is_plural) {
                   for (var i = 0; i < num_plural_forms; i++) {
                      var form = plural_forms[i];
                      translation[form] = $('#g-l10n-client-save-form #l10n-edit-plural-translation-' + form).attr('value');
                  }
              } else {
                  translation = $('#l10n-edit-translation').attr('value');
              }
              Gallery.l10nClient.setString(Gallery.l10nClient.selected, translation);

              // Mark message as translated.
              $('#l10n-client-string-select li').eq(Gallery.l10nClient.selected).removeClass('untranslated').removeClass('active').addClass('translated');

              // Clear the translation form fields
              Gallery.l10nClient.showSourceMessage('', false);
              $('#g-l10n-client-save-form #l10n-edit-translation').val('');

              for (var i = 0; i < num_plural_forms; i++) {
                  var form = plural_forms[i];
                  $('#g-l10n-client-save-form #l10n-edit-plural-translation-' + form).val('');
              }
              $("#g-l10n-client-save-form input[name='l10n-message-key']").val('');
          },
              error: function(xmlhttp) {
              // TODO: Localize this message
              alert('An HTTP error @status occured (or empty response).'.replace('@status', xmlhttp.status));
          }
      });

  // TODO: Add copy/clear buttons (without ajax behavior)
  /*         <input type="submit" name="l10n-edit-copy" value="<?= t("Copy source") ?>"/>
        <input type="submit" name="l10n-edit-clear" value="<?= t("Clear") ?>"/>
  */
  // TODO: Handle plurals in copy button

  // Copy source text to translation field on button click.
  $('#g-l10n-client-save-form #l10n-edit-copy').click(function() {
    $('#g-l10n-client-save-form #l10n-edit-target').val($('#l10n-client-string-editor .source-text').text());
  });

  // Clear translation field on button click.
  $('#g-l10n-client-save-form #l10n-edit-clear').click(function() {
    $('#g-l10n-client-save-form #l10n-edit-target').val('');
  });
};