From 9db15612b16b140bd7958be983d93de0b943cb01 Mon Sep 17 00:00:00 2001 From: Paul Duncan Date: Sat, 30 Jul 2016 03:29:13 -0400 Subject: populate sites api and site list --- data/assets/js/admin/tabs/sites.js | 125 +++++++++++++++++++++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 data/assets/js/admin/tabs/sites.js (limited to 'data/assets/js') diff --git a/data/assets/js/admin/tabs/sites.js b/data/assets/js/admin/tabs/sites.js new file mode 100644 index 0000000..9ef9564 --- /dev/null +++ b/data/assets/js/admin/tabs/sites.js @@ -0,0 +1,125 @@ +jQuery(function($) { + "use strict"; + + var TEMPLATES = new LuigiTemplate.Cache({ + site: [ + "", + "%{name|h}", + + "", + "default", + "", + "", + ], + + loading: [ + "", + "", + " ", + "Loading...", + "", + ], + + error: [ + "", + "", + " ", + "Error: %{error|h}", + "", + ], + }); + + function filter() { + var qs = $('#sites-q').val().replace(/^\s+|\s+$/g, '').toLowerCase().split(/\s+/); + + if (qs.length > 0) { + // hide all sites + $('#sites .list-group-item').addClass('hidden'); + + // show matching sites + $($.grep($('#sites .list-group-item'), function(el) { + var eq = $(el).data('q'); + + return ($.grep(qs, function(q) { + return eq.indexOf(q) !== -1; + }).length == qs.length); + })).removeClass('hidden'); + } else { + // show all sites + $('#sites .list-group-item').removeClass('hidden'); + } + } + + function reload() { + var btn = $('#sites-reload'), + list = $('#sites'); + + // show loading + btn.toggleClass('disabled').find('.loading').toggleClass('hidden'); + list.html(TEMPLATES.run('loading')); + + send('site/get_sites').always(function() { + btn.toggleClass('disabled').find('.loading').toggleClass('hidden'); + list.html(''); + }).fail(function(r) { + var error = r.responseText; + + try { + var data = $.parseJSON(r.responseText); + if (data.error) + error = data.error; + } catch (e) {} + + list.html(TEMPLATES.run('error', { + error: error + })); + }).done(function(r) { + list.html($.map(r, function(row) { + var active = (row.is_active == '1'); + + return TEMPLATES.run('site', $.extend({}, row, { + q: [row.site_id, row.slug, row.name, row.body].join(' ').toLowerCase(), + badge_css: row.is_default ? '' : 'hidden', + })); + }).join('')); + + // refresh filters + filter(); + }); + + // stop event + return false; + } + + $('#sites-q').on('search-update', function() { + filter(); + }); + + $('#sites').on('click', 'a.list-group-item', function() { + var site_id = $(this).data('site_id'); + + // update highlight + $('#sites .active').removeClass('active'); + $(this).addClass('active'); + + // TODO + // $('#site-edit-dialog').data('site_id', site_id).modal('show'); + alert('TODO: edit site ' + site_id); + + // stop event + return false; + }); + + $('#sites-reload').click(reload); + + // load sites + $('#settings-tab-sites').on('show.bs.tab', function() { + reload(); + }); +}); -- cgit v1.2.3