aboutsummaryrefslogtreecommitdiff
path: root/data/assets/js/admin/tabs/posts.js
blob: 6fc0afd73082bed65fe038bddc44d30bfe93bc30 (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
jQuery(function($) {
  "use strict";

  var TEMPLATES = new LuigiTemplate.Cache({
    post: [
      "<a ",
        "href='#' ",
        "class='list-group-item' ",
        "data-post_id='%{post_id|h}' ",
        "data-post_type='%{post_type|h}' ",
        "title='Edit %{post_type} %{post_id|h}.' ",
      ">",
        "%{name|h}",
        " ",
        "(slug = %{slug|h}, state = %{state|h}, type = %{post_type|h})",
      "</a>",
    ],

    error: [
      "<span class='list-group-item list-group-item-danger disabled'>",
        "<i class='fa fa-exclamation-triangle'></i>",
        " ",
        "Error: %{error|h}",
      "</span>",
    ],
  });

  function reload() {
    $('#posts-reload').addClass('disabled')
      .find('.loading').toggleClass('hidden');

    send('post/get_posts', {
      post_type:  $('#posts-filter-type li.active a').data('id'),
      q:          $('#posts-q').data('q'),
      page:       1,
    }).always(function() {
      $('#posts-reload').removeClass('disabled')
        .find('.loading').toggleClass('hidden');
    }).fail(function(r) {
      var error = r.responseText;

      try {
        var data = $.parseJSON(r.responseText);
        if (data.error)
          error = data.error;
      } catch (e) {}

      $('#posts').html(TEMPLATES.run('error', {
        error: error,
      }));
    }).done(function(r) {
      console.log(r);

      $('#posts').html($.map(r.rows, function(row) {
        return TEMPLATES.run('post', row);
      }).join(''));
    });
  }

  $('#admin-tab-posts').on('show.bs.tab', function() {
    $('#posts-reload').click();
  });

  $('.add-post').click(function() {
    var type = $(this).data('type');

    // dismiss dropdown
    $('body').trigger('click');

    send(type + '/add').fail(function(r) {
      var msg = r.responseText;

      try {
        var data = $.parseJSON(r);
        if (data.error)
          msg = data.error;
      } catch (e) {}

      alert('Error: ' + msg);
    }).done(function(r) {
      console.log(r);

      // show edit dialog
      $('#' + type + '-edit-dialog').data('post_id', r.post_id).modal('show')
    });

    // stop event
    return false;
  });

  $('#posts-reload').click(function() {
    reload();

    // stop event
    return false;
  });

  $('#posts-q').on('search-update', function() {
    reload();
  });

  $('.posts-filter-menu').on('click', 'a', function() {
    var data = $(this).data(),
        ul = $(this).parents('ul'),
        is_default = (data.id == ul.data('default'));

    ul.find('li').removeClass('active');
    $(this).parents('li').addClass('active');
    ul.prev('a')
      .toggleClass('btn-default', is_default)
      .toggleClass('btn-primary', !is_default)
      .find('span').text(data.text);

    // hide dropdown
    $('body').trigger('click');

    // reload posts
    reload();

    // stop event
    return false;
  });

  $('#posts').on('click', 'a', function() {
    var data = $(this).data();

    $('#' + data.post_type + '-edit-dialog')
      .data('post_id', data.post_id)
      .modal('show');

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