script.js 5.12 KB
Newer Older
Vasyl Bodnaruk's avatar
Vasyl Bodnaruk committed
1 2 3 4 5 6 7 8
// Table page
$(document).ready(function() {
  // ------------------------------
  // Settings
  // ------------------------------
//  var AJAX_URL = 'https://localhost:3002';

  var selectAllChk = '#select-all';
9 10 11 12
  var chkExport = '.checkbox-export';
  var chkDelete = '.checkbox-delete';
  var counter = '.actions-counter';
  var counterExport = '.actions-export-counter';
Vasyl Bodnaruk's avatar
Vasyl Bodnaruk committed
13
  var deleteBtn = '.js-delete-items';
Vasyl Bodnaruk's avatar
Vasyl Bodnaruk committed
14
  var exportBtn = '.js-export-items'
Vasyl Bodnaruk's avatar
Vasyl Bodnaruk committed
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

  // Action block
  var actionView = '.action-block-view';
  var actionEdit = '.action-block-edit';
  var actionDelete = '.action-block-delete';

  function actionData(element) {
    var id = parseInt($(element).closest('tr').find('.checkbox').prop('id'));
    var type = $(element).data().type;
    var elementDetails = {
      id: id,
      type: type
    };
    return elementDetails;
  }

31
  function updateCounter(chkBoxSelector, button) {
Vasyl Bodnaruk's avatar
Vasyl Bodnaruk committed
32
    var count = 0;
33 34
    var presentSelected = $(chkBoxSelector + ':checked').length > 0 ? false : true;
    var selectedLength = $(chkBoxSelector).each(function(index, item) {
Vasyl Bodnaruk's avatar
Vasyl Bodnaruk committed
35 36 37 38
      $(item).prop('checked') === true
        ? count++
        : ''
    });
39 40
    $(button).find(counter).text(count);
    $(button).prop('disabled', presentSelected);
Vasyl Bodnaruk's avatar
Vasyl Bodnaruk committed
41 42 43 44 45 46 47
  }

  function deleteData() {
    var items = {
      id: [],
      type: 'deleteAll'
    };
48
    var selectedLength = $('.checkbox-delete').each(function(index, item) {
Vasyl Bodnaruk's avatar
Vasyl Bodnaruk committed
49
      $(item).prop('checked') === true
50
        ? items.id.push($(item).closest('tr').prop('id'))
Vasyl Bodnaruk's avatar
Vasyl Bodnaruk committed
51 52 53 54
        : ''
    });
    return items;
  }
Vasyl Bodnaruk's avatar
Vasyl Bodnaruk committed
55 56 57 58 59 60

  function exportData() {
    var items = {
      id: [],
      type: 'exportAll'
    };
61
    var selectedLength = $('.checkbox-export').each(function(index, item) {
Vasyl Bodnaruk's avatar
Vasyl Bodnaruk committed
62
      $(item).prop('checked') === true
63
        ? items.id.push($(item).closest('tr').prop('id'))
Vasyl Bodnaruk's avatar
Vasyl Bodnaruk committed
64 65 66 67 68 69
        : ''
    });
    console.log(items);
    return items;
  }

Vasyl Bodnaruk's avatar
Vasyl Bodnaruk committed
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
    function getCookie(cname) {
        var name = cname + "=";
        var ca = document.cookie.split(';');
        for (var i = 0; i < ca.length; i++) {
          var c = ca[i];
          while (c.charAt(0) == ' ') {
            c = c.substring(1);
          }
          if (c.indexOf(name) == 0) {
            return c.substring(name.length, c.length);
          }
        }
        return "";
      }

  function ajaxPost(url, data) {
    $.ajax({
        headers: { 'X-CSRFToken': getCookie('csrftoken') },
        type: "POST", //rest Type
        dataType: 'json', //mispelled
        data: JSON.stringify(data),
        url: url,
Vasyl Bodnaruk's avatar
Vasyl Bodnaruk committed
92
        async: true,
Vasyl Bodnaruk's avatar
Vasyl Bodnaruk committed
93 94
        contentType: "application/json; charset=utf-8",
        success: function (msg) {
95 96 97 98 99 100 101 102 103 104 105 106 107
            var ids = data.id
            console.log(ids)
            if (data.type === 'deleteAll') {
                removeItems(ids)
                updateButtons()
            }
            if (data.type === 'exportAll') {
                removeItems(ids)
                updateButtons()
            }
//            window.location.reload()
//            window.location.replace(window.location.href);

Vasyl Bodnaruk's avatar
Vasyl Bodnaruk committed
108 109 110
        }
    })
  }
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
  function removeItems (ids) {
    ids.forEach(function(item) {
       $('.form').find('tr').each(function(index, itemRow) {
        $(itemRow).attr('id') === item
          ? $(itemRow).remove()
          : ''
       })
    })
  }
  function updateButtons () {
      updateCounter(chkExport, exportBtn);
      updateCounter(chkDelete, deleteBtn);
  }
  updateButtons()
//  $(selectAllChk).click(function () {
//    $(chk).prop('checked', $(this).prop('checked'));
//    updateCounter();
//  });
Vasyl Bodnaruk's avatar
Vasyl Bodnaruk committed
129

130 131
  $(chkDelete).on('click', updateButtons);
  $(chkExport).on('click', updateButtons);
Vasyl Bodnaruk's avatar
Vasyl Bodnaruk committed
132

Vasyl Bodnaruk's avatar
Vasyl Bodnaruk committed
133
  $(deleteBtn).on('click', function(e) {
134

Vasyl Bodnaruk's avatar
Vasyl Bodnaruk committed
135 136 137 138
    var data = deleteData();
    ajaxPost('/news/', data);
  });

Vasyl Bodnaruk's avatar
Vasyl Bodnaruk committed
139
  $(exportBtn).on('click', function(e) {
140

Vasyl Bodnaruk's avatar
Vasyl Bodnaruk committed
141 142 143 144
    var data = exportData();
    ajaxPost('/news/', data);
  });

Vasyl Bodnaruk's avatar
Vasyl Bodnaruk committed
145 146 147 148 149 150 151
  $(actionDelete).on('click', function(e) {
//    e.preventDefault();
    updateCounter();
//    var data = actionData(this);
//    console.log(data);
//    ajaxPost('/news/', data);
  });
Vasyl Bodnaruk's avatar
Vasyl Bodnaruk committed
152 153
});

Vasyl Bodnaruk's avatar
Vasyl Bodnaruk committed
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
function getUrlParameter(name) {
    name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
    var regex = new RegExp('[\\?&]' + name + '=([^&#]*)');
    var results = regex.exec(location.search);
    return results === null ? null : decodeURIComponent(results[1].replace(/\+/g, ' '));
};

$('.text-white').on('click', function(e) {
    e.preventDefault();
    var from = getUrlParameter('from_date');
    var to = getUrlParameter('to_date');
    var media = getUrlParameter('media');
    var word = getUrlParameter('search');
    var order = getUrlParameter('order_by');
    var reverse = getUrlParameter('reverse');
    var res = '?';
    if (from){res += 'from_date=' + from + '&'}
    if (to){res += 'to_date=' + to + '&'}
    if (media){res += 'media=' + media + '&'}
    if (word){res += 'search=' + word + '&'}

    var orderName = $(this).attr('href').replace("?order_by=", "")
    if (order == orderName){res +='order_by=' + orderName + '&' + 'reverse=' + '1&'} else
    {
        res += 'order_by=' + orderName + '&'

    }
    window.location.href = res;
})
Vasyl Bodnaruk's avatar
Vasyl Bodnaruk committed
183 184 185 186 187 188


// Index Page
$(document).ready(function() {
  $('.multiselect').fastselect();
  $('#publish-date').datepicker();
Vasyl Bodnaruk's avatar
Vasyl Bodnaruk committed
189
});