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

  var p = '#site-add-';

  var FIELDS = {
    text: ['name', 'slug', 'body', 'lang', 'theme_id'],
    list: ['domains', 'styles', 'scripts'],
  }

  var DEFAULTS = {
    lang:     DATA.default_lang,
    theme_id: DATA.default_theme_id,
  };

  $(p + 'dialog').on('show.bs.modal', function() {
    $.each(FIELDS.text, function(_, id) {
      $(p + id).val(DEFAULTS[id] || '');
    });

    // clear list textareas
    $.each(FIELDS.list, function(_, id) {
      $(p + id).val('');
    });
  });

  $(p + 'dialog').on('shown.bs.modal', function() {
    // show content tab
    $(p + 'tab-content').click();

    // focus name field
    $(p + 'name').focus();
  });

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

      // stop event
      return false;
    }
  });

  $(p + 'name').keydown(function(ev) {
    var me = $(this);

    setTimeout(function() {
      $(p + 'slug').val(slugify(me.val()));
    }, 10);
  });

  $(p + 'confirm').click(function() {
    var me = $(this);

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

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

    send('site/add', FIELDS.text.reduce(function(r, id) {
      r[id] = $(p + id).val();
      return r;
    }, FIELDS.list.reduce(function(r, id) {
      r[id] = listify(p + id);
      return r;
    }, {
      // hard-code this for now
      is_full_feed: 't',
    }))).always(function() {
      // toggle loading
      me.toggleClass('disabled').find('.loading').toggleClass('hidden');
    }).fail(function(r) {
      gripe(r, 'add site');
    }).done(function(r) {
      $('#sites-reload').click();
      $(p + 'dialog').modal('hide');
    });

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