aboutsummaryrefslogtreecommitdiff
path: root/data/assets/js/admin/dialogs/site-add.js
blob: 9a3c02f79c0fe6fb12d2e144fa2548b55f165f68 (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
jQuery(function($) {
  "use strict";

  var IDS = ['name', 'slug', 'body', 'lang', 'theme_id'];
  var DEFAULTS = {
    lang:     DATA.default_lang,
    theme_id: DATA.default_theme_id,
  };

  function get_domains() {
    var r = $('#site-add-domains').val().replace(/^\s+|\s+$/m, '');
    return (r.length > 0) ? r.split(/\s+/m) : [];
  }

  $('#site-add-dialog').on('show.bs.modal', function() {
    $.each(IDS, function(_, id) {
      $('#site-add-' + id).val(DEFAULTS[id] || '');
    });

    // clear list of domains
    $('#site-add-domains').val('');
  });

  $('#site-add-dialog').on('shown.bs.modal', function() {
    $('#site-add-name').focus();
  });

  $('#site-add-dialog input[type="text"]').keydown(function(ev) {
    if (ev.which == 13) {
      setTimeout(function() {
        $('#site-add-confirm').click();
      }, 10);

      // stop event
      return false;
    }
  });

  $('#site-add-confirm').click(function() {
    var me = $(this);

    if (me.hasClass('disabled'))
      return false;

    // toggle loading
    me.toggleClass('disabled').find('.loading').toggleClass('hidden');

    send('site/add', IDS.reduce(function(r, id) {
      r[id] = $('#site-add-' + id).val();
      return r;
    }, {
      is_full_feed: 't',
      domains: JSON.stringify(get_domains()),
    })).always(function() {
      // toggle loading
      me.toggleClass('disabled').find('.loading').toggleClass('hidden');
    }).fail(function(r) {
      gripe(r, 'add site');
    }).done(function(r) {
      $('#sites-reload').click();
      $('#site-add-dialog').modal('hide');
    });

    // stop event
    return false;
  });
});