welco/welco/static/js/welco.js

193 lines
7.7 KiB
JavaScript

$(function() {
$('[data-pdf-href]').on('click', function() {
var current_pdf = $('#pdf-viewer').data('current-pdf-href');
var new_pdf = $(this).data('pdf-href');
if (current_pdf !== new_pdf) {
$('#pdf-viewer').data('current-pdf-href', new_pdf);
$('#pdf-viewer')[0].contentWindow.postMessage(new_pdf,
window.location.protocol + '//' + window.location.host);
$(this).trigger('welco:mail-selected');
}
});
$('.mails ul li[data-pdf-href]').on('click', function() {
$(this).parent().find('li').removeClass('active');
$(this).addClass('active');
var source_type = $('div.source div[data-source-type]').data('source-type');
var source_pk = $('div.source .active[data-source-pk]').data('source-pk');
$('.cell[data-zone-url]').each(function(idx, zone) {
$.ajax({url: $(zone).data('zone-url'),
data: {source_type: source_type,
source_pk: source_pk},
async: true,
dataType: 'html',
success: function(data) {
$(zone).find('> div').replaceWith(data);
$(zone).find('select').select2();
$(zone).removeClass('has-page-displayed');
$(zone).removeClass('has-contact-displayed');
},
error: function(error) { console.log(':(', error); }
});
});
});
$('.contacts').delegate('button.save', 'click', function() {
var source_type = $('div.source div[data-source-type]').data('source-type');
var source_pk = $('div.source .active[data-source-pk]').data('source-pk');
var selected_user_id = $('#current-selected-user').val();
$.ajax({url: $('.cell.qualif').data('zone-url'),
data: {user_id: selected_user_id,
source_type: source_type,
source_pk: source_pk},
method: 'POST',
dataType: 'html',
success: function(data) {
$('div.contacts h3').effect('highlight');
$('.cell.qualif > div').replaceWith(data);
$('.qualif').find('select').select2();
},
error: function(error) { console.log(':/', error); }
});
return false;
});
$('.qualif').delegate('button.add', 'click', function() {
var formdef_reference = $('#id_formdef_reference').val();
var source_type = $('div.source div[data-source-type]').data('source-type');
var source_pk = $('div.source .active[data-source-pk]').data('source-pk');
$.ajax({url: $('.cell.qualif').data('zone-url'),
data: {formdef_reference: formdef_reference,
source_type: source_type,
source_pk: source_pk},
method: 'POST',
dataType: 'html',
success: function(data) { $('.cell.qualif > div').replaceWith(data); },
error: function(error) { console.log(':(', error); }
});
return false;
});
$('.qualif').delegate('button.done', 'click', function() {
var formdef_reference = $('#id_formdef_reference').val();
var source_type = $('div.source div[data-source-type]').data('source-type');
var source_pk = $('div.source .active[data-source-pk]').data('source-pk');
$.ajax({url: $(this).data('action-url'),
data: {formdef_reference: formdef_reference,
source_type: source_type,
source_pk: source_pk},
method: 'POST',
dataType: 'json',
success: function(data) {
if (data.result == 'ok') {
window.location.reload();
} else {
/* TODO: display error notification */
}
},
error: function(error) { console.log(':(', error); }
});
return false;
});
$('.qualif').delegate('button.save', 'click', function() {
var post_date = $('#id_post_date').val();
var registered_mail = $('#id_registered_mail').prop('checked');
var source_type = $('div.source div[data-source-type]').data('source-type');
var source_pk = $('div.source .active[data-source-pk]').data('source-pk');
$.ajax({url: $(this).data('action-url'),
data: {post_date: post_date,
registered_mail: registered_mail,
source_type: source_type,
source_pk: source_pk},
method: 'POST',
dataType: 'html',
success: function(data) {
$('.cell.qualif > div').replaceWith(data);
$('div.qualif-source').effect('highlight');
$('.qualif').find('select').select2();
},
error: function(error) { console.log(':(', error); }
});
return false;
});
$('.qualif').delegate('a.plus', 'click', function() {
$(this).parents('li').hide();
$('.qualif .add-formdef-reference').show();
});
$('.kb, .contacts').delegate('a.reset', 'click', function() {
var source_type = $('div.source div[data-source-type]').data('source-type');
var source_pk = $('div.source .active[data-source-pk]').data('source-pk');
$(this).parents('.cell').each(function(idx, zone) {
$.ajax({url: $(zone).data('zone-url'),
data: {source_type: source_type,
source_pk: source_pk},
async: true,
dataType: 'html',
success: function(data) {
$(zone).find('> div').replaceWith(data);
$(zone).find('select').select2();
$(zone).removeClass('has-contact-displayed');
$(zone).removeClass('has-page-displayed');
},
error: function(error) { console.log(':(', error); }
});
});
});
$('.kb, .contacts').delegate('input', 'keyup', function() {
var q = $(this).val();
var search_result_ul = $(this).parent().find('ul.result');
search_result_ul.empty();
$.getJSON($(this).data('autocomplete-json'),
{'q': q},
function (response) {
search_result_ul.empty();
$(response.data).each(function(idx, elem) {
var new_elem = '<li><a href="#" data-page-slug="' + elem.slug + '">' + elem.title;
if (elem.more) {
new_elem = new_elem + ' <span class="more">' + elem.more + '</span>';
}
new_elem = new_elem + '</a></li>'
$(new_elem).appendTo(search_result_ul);
});
}
);
return false;
});
$('.kb').delegate('ul.result a', 'click', function() {
var page_slug = $(this).data('page-slug');
var fragment_url = $(this).parents('[data-page-fragment-url]').data('page-fragment-url') + page_slug + '/';
$.ajax({url: fragment_url,
method: 'GET',
dataType: 'html',
success: function(data) {
$('.kb .page').replaceWith(data);
$('.kb').addClass('has-page-displayed');
},
error: function(error) { console.log(':(', error); }
});
return false;
});
$('.contacts').delegate('ul.result a', 'click', function() {
var user_slug = $(this).data('page-slug');
var fragment_url = $(this).parents('[data-contact-fragment-url]').data('contact-fragment-url') + user_slug + '/';
$.ajax({url: fragment_url,
method: 'GET',
dataType: 'html',
success: function(data) {
$('.contacts .contact').replaceWith(data);
$('.contacts').addClass('has-contact-displayed');
},
error: function(error) { console.log(':(', error); }
});
return false;
});
$('#pdf-viewer').on('load', function() {
$('.mails ul li[data-pdf-href]:first-child').trigger('click');
});
});