welco/welco/static/js/welco.js

476 lines
19 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

$(function() {
function refresh_bottom_cells(callback) {
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');
$('#create-new-contact').off('click').on('click', window.displayPopup);
if (typeof(callback) === 'function') callback();
},
error: function(error) { console.log(':(', error); }
});
});
}
$('[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');
$('#id_post_date').val($(this).data('post-date'));
$('#id_registered_mail_number').val($(this).data('registered-mail-number'));
$('#id_mail_number').val($(this).data('mail-number'));
var source_pk = $('div.source .active[data-source-pk]').data('source-pk');
$('#postit').data('url', $('#postit').data('base-url') + '?mail=' + source_pk);
$('#postit').trigger('welco:load-mail-note');
$('#postit').show();
refresh_bottom_cells();
});
$('.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.contacts').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');
$('div.contacts button.save').addClass('pinned');
},
error: function(error) { console.log(':/', error); }
});
return false;
});
$('.qualif').delegate('a.remove', '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');
$.ajax({url: $(this).attr('href'),
method: 'GET',
success: function(data) {
$.ajax({url: $('.cell.qualif').data('zone-url'),
data: {source_type: source_type, source_pk: source_pk},
async: true,
dataType: 'html',
success: function(data) {
var zone = $('.cell.qualif');
$(zone).find('> div').replaceWith(data);
$(zone).find('select').select2();
}
});
}
});
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');
if ($('div.contacts button.save').length &&
$('div.contacts button.save.pinned').length == 0) {
if (confirm("Transmettre alors que le contact n'est pas sélectionné ?") != true) {
return false;
}
}
$.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 {
alert(data.msg);
}
},
error: function(error) { console.log(':(', error); }
});
return false;
});
$('.document').delegate('button.reject', 'click', function() {
if (confirm("Confirmer la suppression de ce courrier") != true) {
return false;
}
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: {source_type: source_type,
source_pk: source_pk},
method: 'POST',
dataType: 'html',
success: function(data) {
window.location.reload();
},
error: function(error) { console.log(':(', error); }
});
return false;
});
$('.document').delegate('button.save', 'click', function() {
var post_date = $('#id_post_date').val();
var registered_mail_number = $('#id_registered_mail_number').val();
var mail_number = $('#id_mail_number').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: {post_date: post_date,
registered_mail_number: registered_mail_number,
mail_number: mail_number,
source_type: source_type,
source_pk: source_pk},
method: 'POST',
dataType: 'html',
success: function(data) {
$('div.source .active').data('post-date', post_date);
$('div.source .active').data('registered-mail-number', registered_mail_number);
$('div.source .active').data('mail-number', mail_number);
$('#source-mainarea form').effect('highlight');
},
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) {
var new_zone = $(data);
/* when going back to contact search make sure we are not
* jumped back to the selected contact */
new_zone.find('div.contact').empty();
$(zone).find('> div').replaceWith(new_zone);
$(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 + '/';
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: fragment_url,
method: 'GET',
data: {source_type: source_type, source_pk: source_pk},
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');
});
$(document).on('gadjo:dialog-done welco:load-mail-note', function(ev) {
if (ev.target && ev.target.id == 'create-new-contact') return;
var source_pk = $('div.source .active[data-source-pk]').data('source-pk');
$('#postit').load('/ajax/mail/note/' + source_pk);
});
$(document).on('gadjo:dialog-done welco:load-copies', function(ev) {
if (ev.target && ev.target.id == 'create-new-contact') return;
var source_pk = $('div.source .active[data-source-pk]').data('source-pk');
$.getJSON(
'/ajax/copies/' + source_pk + '/',
function(data) {
$('.info span').text(data.info);
$('.avis span').text(data.avis);
$('.mandatory_avis span').text(data.mandatory_avis);
}
);
});
$(document).on('gadjo:dialog-done', function(ev, data) {
if (ev.target && ev.target.id != 'create-new-contact') return;
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 = data.data.user_id;
$.ajax({url: $('.cell.contacts').data('zone-url'),
data: {user_id: selected_user_id,
source_type: source_type,
source_pk: source_pk},
method: 'POST',
dataType: 'html',
success: function(data) {
var fragment_url = $('.cell.contacts [data-contact-fragment-url]'
).data('contact-fragment-url') + selected_user_id + '/';
$.ajax({url: fragment_url,
method: 'GET',
data: {source_type: source_type, source_pk: source_pk},
dataType: 'html',
success: function(data) {
$('.contacts .contact').replaceWith(data);
$('.contacts').addClass('has-contact-displayed');
},
error: function(error) { console.log(':(', error); }
});
},
error: function(error) { console.log(':/', error); }
});
});
$('#postit').on('click', window.displayPopup);
$('#id_post_date').datepicker($.datepicker.regional["fr"]);
if ($('#formdef-filter').length) {
var known_formdefs = Object();
$('[data-formdef-reference]').each(function(index, elem) {
known_formdefs[$(elem).data('formdef-reference')] = $(elem).text();
});
var options = Array();
$(Object.keys(known_formdefs)).each(function(index, elem) {
options.push({'key': elem, 'value': known_formdefs[elem]});
});
options.sort(function(x, y) {
if (x.value > y.value) return 1;
if (x.value < y.value) return -1;
return 0;
});
$('#formdef-filter').empty();
/* TODO: should use javascript i18n */
$('<option value="">Tout</option>').appendTo($('#formdef-filter'));
$(options).each(function(index, elem) {
$('<option value="' + elem.key + '">' + elem.value + '</option>').appendTo($('#formdef-filter'));
});
$('#formdef-filter').on('change', function() {
var selected_formdef = $(this).val();
if (selected_formdef == '') {
$('#source-sidebar li').removeClass('li-visible');
$('#source-sidebar li').show();
} else {
$('#source-sidebar li').each(function(index, elem) {
var formdef_references = $(elem).find('[data-formdef-reference]').map(function(idx, elem) {
return $(elem).data('formdef-reference');
}).toArray();
if (formdef_references.indexOf(selected_formdef) == -1) {
$(elem).removeClass('li-visible');
$(elem).hide();
} else {
$(elem).addClass('li-visible');
$(elem).show();
}
});
if ($('#source-sidebar li.active.li-visible').length == 0) {
$($('#source-sidebar li.li-visible')[0]).trigger('click');
}
}
});
}
$('.document').delegate('button#take-phoneline', 'click', function() {
var zone = $(this).parents('[data-zone-url]');
var callee = $('input[name=phoneline]').val();
if (callee == '') return false;
$.ajax({url: $(this).data('action-url'),
data: JSON.stringify({callee: callee}),
method: 'POST',
contentType: 'application/json',
async: true,
dataType: 'json',
success: function(data) {
$.ajax({url: zone.data('zone-url'),
dataType: 'html',
success: function(data) {
$(zone).parent('div').html(data);
}
});
},
error: function(error) { console.log(':(', error); }
});
return false;
});
$('.document').delegate('a.release-line', 'click', function() {
var zone = $(this).parents('[data-zone-url]');
var callee = $(this).data('callee');
$.ajax({url: $(this).data('action-url'),
data: JSON.stringify({callee: callee}),
method: 'POST',
contentType: 'application/json',
async: true,
dataType: 'json',
success: function(data) {
$.ajax({url: zone.data('zone-url'),
dataType: 'html',
success: function(data) {
$(zone).parent('div').html(data);
}
});
},
error: function(error) { console.log(':(', error); }
});
return false;
});
if ($('.source-phone').length) {
function check_active_call() {
var active_call_url = $('[data-active-call-url]').data('active-call-url');
$.ajax({url: active_call_url,
dataType: 'json',
success: function(response) {
if (response.active === false) {
$('<span class="done"> (terminé)</span>').appendTo($('div.phonecall.active h1'));
window.setTimeout(check_calls, 1000);
} else {
window.setTimeout(check_active_call, 5000);
}
}
});
}
function check_calls() {
if ($('.phonecall').length && $('.phonecall .done').length == 0) { /* active phone call */
window.setTimeout(check_active_call, 1000);
} else {
var current_calls_url = $('[data-current-calls]').data('current-calls');
$.ajax({url: current_calls_url,
dataType: 'json',
success: function(response) {
if (response.data.calls.length != 0) {
/* got a call */
var caller = response.data.calls[0].caller;
var zone = $('.source-phone .source [data-zone-url]');
$.ajax({url: zone.data('zone-url'),
dataType: 'html',
success: function(data) {
$(zone).parent('div').html(data);
refresh_bottom_cells(function() {
$('.contacts input[name=q]').val(caller).trigger('keyup');
});
}
});
} else {
window.setTimeout(check_calls, 1000);
}
},
error: function() {
window.setTimeout(check_calls, 1000);
}
});
}
}
window.setTimeout(check_calls, 1000);
}
$('.qualif').delegate('a.create-formdata', 'click', function() {
$.ajax({url: $(this).data('action-url'),
data: {},
method: 'POST',
dataType: 'json',
success: function(data) {
if (data.result == 'ok') {
window.open(data.url, '_blank');
refresh_bottom_cells();
} else {
alert(data.msg);
}
},
error: function(error) { console.log(':(', error); }
});
return false;
});
});