Start splitting core.js

New files :
* classic_poll.js -> loaded on page choix_autre.php
* date_poll.js -> loaded on page choix_date.ph
* framadatepicker.js -> loaded on pages that need the date picker
This commit is contained in:
Olivier PEREZ 2015-03-05 14:53:42 +01:00
parent 778a03f802
commit 6bbe03758c
7 changed files with 366 additions and 285 deletions

View File

@ -296,7 +296,11 @@ if (empty($_SESSION['form']->title) || empty($_SESSION['form']->admin_name) || (
</div> </div>
</div> </div>
</div> </div>
</form>' . "\n"; </form>
<script type="text/javascript" src="js/app/framadatepicker.js"></script>
<script type="text/javascript" src="js/app/classic_poll.js"></script>
' . "\n";
bandeau_pied(); bandeau_pied();

View File

@ -288,7 +288,11 @@ if (!isset($_SESSION['form']->title) || !isset($_SESSION['form']->admin_name) ||
</div> </div>
</div> </div>
</div> </div>
</form>'."\n"; </form>
<script type="text/javascript" src="js/app/framadatepicker.js"></script>
<script type="text/javascript" src="js/app/date_poll.js"></script>
'."\n";
bandeau_pied(); bandeau_pied();

106
js/app/classic_poll.js Normal file
View File

@ -0,0 +1,106 @@
/**
* This software is governed by the CeCILL-B license. If a copy of this license
* is not distributed with this file, you can obtain one at
* http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.txt
*
* Authors of STUdS (initial project): Guilhem BORGHESI (borghesi@unistra.fr) and Raphaël DROZ
* Authors of Framadate/OpenSondate: Framasoft (https://github.com/framasoft)
*
* =============================
*
* Ce logiciel est régi par la licence CeCILL-B. Si une copie de cette licence
* ne se trouve pas avec ce fichier vous pouvez l'obtenir sur
* http://www.cecill.info/licences/Licence_CeCILL-B_V1-fr.txt
*
* Auteurs de STUdS (projet initial) : Guilhem BORGHESI (borghesi@unistra.fr) et Raphaël DROZ
* Auteurs de Framadate/OpenSondage : Framasoft (https://github.com/framasoft)
*/
(function () {
// 2 choices filled and you can submit
var submitChoicesAvalaible = function () {
var nb_filled_choices = 0;
$('.choice-field input').each(function () {
if ($(this).val() != '') {
nb_filled_choices++;
}
});
if (nb_filled_choices >= 1) {
$('button[name="fin_sondage_autre"]').removeClass('disabled');
} else {
$('button[name="fin_sondage_autre"]').addClass('disabled');
}
};
// Button "Add a choice"
$('#add-a-choice').on('click', function () {
var nb_choices = $('.choice-field').length;
var last_choice = $('.choice-field:last');
var new_choice = last_choice.html();
// label
var last_choice_label = last_choice.children('label').text();
var choice_text = last_choice_label.substring(0, last_choice_label.indexOf(' '));
// for and id
var re_id_choice = new RegExp('"choice' + (nb_choices - 1) + '"', 'g');
var new_choice_html = new_choice.replace(re_id_choice, '"choice' + nb_choices + '"')
.replace(last_choice_label, choice_text + ' ' + (nb_choices + 1))
.replace(/value="(.*?)"/g, 'value=""');
last_choice.after('<div class="form-group choice-field">' + new_choice_html + '</div>');
$('#choice' + nb_choices).focus();
$('#remove-a-choice').removeClass('disabled');
});
// Button "Remove a choice"
$('#remove-a-choice').on('click', function () {
$('.choice-field:last').remove();
var nb_choices = $('.choice-field').length;
$('#choice' + (nb_choices - 1)).focus();
if (nb_choices == 1) {
$('#remove-a-choice').addClass('disabled');
}
submitChoicesAvalaible();
});
$(document).on('keyup, change', '.choice-field input', function () {
submitChoicesAvalaible();
});
submitChoicesAvalaible();
// Button to build markdown from: link + image-url + text
var md_a_imgModal = $('#md-a-imgModal');
var md_text = $('#md-text');
var md_img = $('#md-img');
var md_val = $('#md-a');
$(document).on('click', '.md-a-img', function () {
md_a_imgModal.modal('show');
md_a_imgModal.find('.btn-primary').attr('value', $(this).prev().attr('id'));
$('#md-a-imgModalLabel').text($(this).attr('title'));
});
md_a_imgModal.find('.btn-primary').on('click', function () {
if (md_img.val() != '' && md_val.val() != '') {
$('#' + $(this).val()).val('[![' + md_text.val() + '](' + md_img.val() + ')](' + md_val.val() + ')');
} else if (md_img.val() != '') {
$('#' + $(this).val()).val('![' + md_text.val() + '](' + md_img.val() + ')');
} else if (md_val.val() != '') {
$('#' + $(this).val()).val('[' + md_text.val() + '](' + md_val.val() + ')');
} else {
$('#' + $(this).val()).val(md_text.val());
}
md_a_imgModal.modal('hide');
md_img.val('');
md_val.val('');
md_text.val('');
submitChoicesAvalaible();
});
})();

201
js/app/date_poll.js Normal file
View File

@ -0,0 +1,201 @@
/**
* This software is governed by the CeCILL-B license. If a copy of this license
* is not distributed with this file, you can obtain one at
* http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.txt
*
* Authors of STUdS (initial project): Guilhem BORGHESI (borghesi@unistra.fr) and Raphaël DROZ
* Authors of Framadate/OpenSondate: Framasoft (https://github.com/framasoft)
*
* =============================
*
* Ce logiciel est régi par la licence CeCILL-B. Si une copie de cette licence
* ne se trouve pas avec ce fichier vous pouvez l'obtenir sur
* http://www.cecill.info/licences/Licence_CeCILL-B_V1-fr.txt
*
* Auteurs de STUdS (projet initial) : Guilhem BORGHESI (borghesi@unistra.fr) et Raphaël DROZ
* Auteurs de Framadate/OpenSondage : Framasoft (https://github.com/framasoft)
*/
(function () {
// Global variables
var $selected_days = $('#selected-days');
var $removeaday_and_copyhours = $('#remove-a-day, #copyhours');
// at least 1 day and 1 hour filled and you can submit
var submitDaysAvalaible = function () {
var nb_filled_days = 0;
var nb_filled_hours = 0;
$selected_days.find('fieldset legend input').each(function () {
if ($(this).val() != '') {
nb_filled_days++;
}
});
$selected_days.find('.hours').each(function () {
if ($(this).val() != '') {
nb_filled_hours++;
}
});
if (nb_filled_days >= 1 && nb_filled_hours >= 1) {
$('button[name="choixheures"]').removeClass('disabled');
} else {
$('button[name="choixheures"]').addClass('disabled');
}
};
// Button "Remove all hours"
$(document).on('click', '#resethours', function () {
$selected_days.find('fieldset').each(function () {
$(this).find('.hours:gt(2)').parent().remove();
});
$('#d0-h0').focus();
$selected_days.find('fieldset .hours').attr('value', '');
});
// Button "Remove all days"
$('#resetdays').on('click', function () {
$selected_days.find('fieldset:gt(0)').remove();
$('#day0').focus();
$removeaday_and_copyhours.addClass('disabled');
});
// Button "Copy hours of the first day"
$('#copyhours').on('click', function () {
var first_day_hours = $selected_days.find('fieldset:eq(0) .hours').map(function () {
return $(this).val();
});
$selected_days.find('fieldset:gt(0)').each(function () {
for (var i = 0; i < first_day_hours.length; i++) {
$(this).find('.hours:eq(' + i + ')').val(first_day_hours[i]); // fill hours
}
});
$('#d0-h0').focus();
});
// Buttons "Add an hour"
$(document).on('click', '.add-an-hour', function () {
var last_hour = $(this).parent('div').parent('div').prev();
// for and id
var di_hj = last_hour.children('.hours').attr('id').split('-');
var di = parseInt(di_hj[0].replace('d', ''));
var hj = parseInt(di_hj[1].replace('h', ''));
// label, title and placeholder
var last_hour_label = last_hour.children('.hours').attr('placeholder');
var hour_text = last_hour_label.substring(0, last_hour_label.indexOf(' '));
// RegEx for multiple replace
var re_label = new RegExp(last_hour_label, 'g');
var re_id = new RegExp('"d' + di + '-h' + hj + '"', 'g');
// HTML code of the new hour
var new_hour_html =
'<div class="col-sm-2">' +
last_hour.html().replace(re_label, hour_text + ' ' + (hj + 2))
.replace(re_id, '"d' + di + '-h' + (hj + 1) + '"')
.replace(/value="(.*?)"/g, 'value=""') +
'</div>';
// After 11 + button is disable
if (hj < 10) {
last_hour.after(new_hour_html);
$('#d' + di + '-h' + (hj + 1)).focus();
$(this).prev('.remove-an-hour').removeClass('disabled');
if (hj == 9) {
$(this).addClass('disabled');
}
}
});
// Buttons "Remove an hour"
$(document).on('click', '.remove-an-hour', function () {
var last_hour = $(this).parent('div').parent('div').prev();
// for and id
var di_hj = last_hour.children('.hours').attr('id').split('-');
var di = parseInt(di_hj[0].replace('d', ''));
var hj = parseInt(di_hj[1].replace('h', ''));
// The first hour must not be removed
if (hj > 0) {
last_hour.remove();
$('#d' + di + '-h' + (hj - 1)).focus();
$(this).next('.add-an-hour').removeClass('disabled');
if (hj == 1) {
$(this).addClass('disabled');
}
}
submitDaysAvalaible();
});
// Button "Add a day"
$('#add-a-day').on('click', function () {
var nb_days = $selected_days.find('fieldset').length;
var last_day = $selected_days.find('fieldset:last');
var last_day_title = last_day.find('legend input').attr('title');
var re_id_hours = new RegExp('"d' + (nb_days - 1) + '-h', 'g');
var re_name_hours = new RegExp('name="horaires' + (nb_days - 1), 'g');
var new_day_html = last_day.html().replace(re_id_hours, '"d' + nb_days + '-h')
.replace('id="day' + (nb_days - 1) + '"', 'id="day' + nb_days + '"')
.replace('for="day' + (nb_days - 1) + '"', 'for="day' + nb_days + '"')
.replace(re_name_hours, 'name="horaires' + nb_days)
.replace(/value="(.*?)"/g, 'value=""')
.replace(/hours" title="(.*?)"/g, 'hours" title="" p')
.replace('title="' + last_day_title + '"', 'title="' + last_day_title.substring(0, last_day_title.indexOf(' ')) + ' ' + (nb_days + 1) + '"');
last_day.after('<fieldset>' + new_day_html + '</fieldset>');
$('#day' + (nb_days)).focus();
$removeaday_and_copyhours.removeClass('disabled');
});
// Button "Remove a day"
$('#remove-a-day').on('click', function () {
$selected_days.find('fieldset:last').remove();
var nb_days = $selected_days.find('fieldset').length;
$('#day' + (nb_days - 1)).focus();
if (nb_days == 1) {
$removeaday_and_copyhours.addClass('disabled');
}
submitDaysAvalaible();
});
// Title update on hours and buttons -/+ hours
$(document).on('change', '#selected-days legend input', function () {
$selected_days.find('.hours').each(function () {
$(this).attr('title', $(this).parents('fieldset').find('legend input').val() + ' - ' + $(this).attr('placeholder'));
});
$('#selected-days .add-an-hour, #selected-days .remove-an-hour').each(function () {
var old_title = $(this).attr('title');
if (old_title.indexOf('-') > 0) {
old_title = old_title.substring(old_title.indexOf('-') + 2, old_title.length);
}
$(this).attr('title', $(this).parents('fieldset').find('legend input').val() + ' - ' + old_title);
});
});
$(document).on('keyup, change', '.hours, #selected-days fieldset legend input', function () {
submitDaysAvalaible();
});
submitDaysAvalaible();
// 2 days and you can remove a day or copy hours
if ($selected_days.find('fieldset').length > 1) {
$removeaday_and_copyhours.removeClass('disabled');
}
})();

45
js/app/framadatepicker.js Normal file
View File

@ -0,0 +1,45 @@
/**
* This software is governed by the CeCILL-B license. If a copy of this license
* is not distributed with this file, you can obtain one at
* http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.txt
*
* Authors of STUdS (initial project): Guilhem BORGHESI (borghesi@unistra.fr) and Raphaël DROZ
* Authors of Framadate/OpenSondate: Framasoft (https://github.com/framasoft)
*
* =============================
*
* Ce logiciel est régi par la licence CeCILL-B. Si une copie de cette licence
* ne se trouve pas avec ce fichier vous pouvez l'obtenir sur
* http://www.cecill.info/licences/Licence_CeCILL-B_V1-fr.txt
*
* Auteurs de STUdS (projet initial) : Guilhem BORGHESI (borghesi@unistra.fr) et Raphaël DROZ
* Auteurs de Framadate/OpenSondage : Framasoft (https://github.com/framasoft)
*/
var init_datepicker = function() {
$('.input-group.date').datepicker({
format: "dd/mm/yyyy",
todayBtn: "linked",
orientation: "top left",
autoclose: true,
language: lang,
todayHighlight: true,
beforeShowDay: function (date){
var $selected_days = [];
$('#selected-days').find('input[id^="day"]').each(function() {
if($(this).val()!='') {
$selected_days.push($(this).val());
}
});
for(var i = 0; i < $selected_days.length; i++){
var $selected_date = $selected_days[i].split('/');
if (date.getFullYear() == $selected_date[2] && (date.getMonth()+1) == $selected_date[1] && date.getDate() == $selected_date[0]){
return {
classes: 'disabled selected'
};
}
}
}
});
};

View File

@ -1,42 +1,13 @@
$(document).ready(function() { $(document).ready(function() {
var lang = $('html').attr('lang');
// Datepicker
var framadatepicker = function() {
$('.input-group.date').datepicker({
format: "dd/mm/yyyy",
todayBtn: "linked",
orientation: "top left",
autoclose: true,
language: lang,
todayHighlight: true,
beforeShowDay: function (date){
var $selected_days = new Array();
$('#selected-days input[id^="day"]').each(function() {
if($(this).val()!='') {
$selected_days.push($(this).val());
}
});
for(i = 0; i < $selected_days.length; i++){
var $selected_date = $selected_days[i].split('/');
if (date.getFullYear() == $selected_date[2] && (date.getMonth()+1) == $selected_date[1] && date.getDate() == $selected_date[0]){
return {
classes: 'disabled selected'
};
}
}
}
});
};
window.lang = $('html').attr('lang');
var datepickerfocus = false; // a11y : datepicker not display on focus until there is one click on the button var datepickerfocus = false; // a11y : datepicker not display on focus until there is one click on the button
$(document).on('click','.input-group.date .input-group-addon', function() { $(document).on('click','.input-group.date .input-group-addon', function() {
datepickerfocus = true; datepickerfocus = true;
// Re-init datepicker config before displaying // Re-init datepicker config before displaying
$(this).parent().datepicker(framadatepicker()); $(this).parent().datepicker(init_datepicker());
$(this).parent().datepicker('show'); $(this).parent().datepicker('show');
// Trick to refresh calendar // Trick to refresh calendar
@ -48,261 +19,10 @@ $(document).ready(function() {
$(document).on('focus','.input-group.date input', function() { $(document).on('focus','.input-group.date input', function() {
if(datepickerfocus) { if(datepickerfocus) {
$(this).parent('.input-group.date').datepicker(framadatepicker()); $(this).parent('.input-group.date').datepicker(init_datepicker());
$(this).parent('.input-group.date').datepicker('show'); $(this).parent('.input-group.date').datepicker('show');
} }
}); });
/**
* choix_date.php
**/
// Button "Remove all hours"
$(document).on('click','#resethours', function() {
$('#selected-days fieldset').each(function() {
$(this).find('.hours:gt(2)').parent().remove();
});
$('#d0-h0').focus();
$('#selected-days fieldset .hours').attr('value','');
});
// Button "Remove all days"
$('#resetdays').on('click', function() {
$('#selected-days fieldset:gt(0)').remove();
$('#day0').focus();
$('#remove-a-day, #copyhours').addClass('disabled');
});
// Button "Copy hours of the first day"
$('#copyhours').on('click', function() {
var first_day_hours = $('#selected-days fieldset:eq(0) .hours').map(function() {
return $(this).val();
});
$('#selected-days fieldset:gt(0)').each(function() {
for ($i = 0; $i < first_day_hours.length; $i++) {
$(this).find('.hours:eq('+$i+')').val(first_day_hours[$i]); // fill hours
}
});
$('#d0-h0').focus();
});
// Buttons "Add an hour"
$(document).on('click','.add-an-hour', function() {
var last_hour = $(this).parent('div').parent('div').prev();
// for and id
var di_hj = last_hour.children('.hours').attr('id').split('-');
var di = parseInt(di_hj[0].replace('d','')); var hj = parseInt(di_hj[1].replace('h',''));
// label, title and placeholder
var last_hour_label = last_hour.children('.hours').attr('placeholder');
var hour_text = last_hour_label.substring(0, last_hour_label.indexOf(' '));
// RegEx for multiple replace
var re_label = new RegExp(last_hour_label, 'g');
var re_id = new RegExp('"d'+di+'-h'+hj+'"', 'g');
// HTML code of the new hour
var new_hour_html =
'<div class="col-sm-2">'+
last_hour.html().replace(re_label, hour_text+' '+(hj+2))
.replace(re_id,'"d'+di+'-h'+(hj+1)+'"')
.replace(/value="(.*?)"/g, 'value=""')+
'</div>';
// After 11 + button is disable
if (hj<10) {
last_hour.after(new_hour_html);
$('#d'+di+'-h'+(hj+1)).focus();
$(this).prev('.remove-an-hour').removeClass('disabled');
if (hj==9) {
$(this).addClass('disabled');
}
};
});
// Buttons "Remove an hour"
$(document).on('click', '.remove-an-hour', function() {
var last_hour = $(this).parent('div').parent('div').prev();
// for and id
var di_hj = last_hour.children('.hours').attr('id').split('-');
var di = parseInt(di_hj[0].replace('d','')); var hj = parseInt(di_hj[1].replace('h',''));
// The first hour must not be removed
if (hj>0) {
last_hour.remove();
$('#d'+di+'-h'+(hj-1)).focus();
$(this).next('.add-an-hour').removeClass('disabled');
if (hj==1) {
$(this).addClass('disabled');
}
};
SubmitDaysAvalaible();
});
// Button "Add a day"
$('#add-a-day').on('click', function() {
var nb_days = $('#selected-days fieldset').length;
var last_day = $('#selected-days fieldset:last');
var last_day_title = last_day.find('legend input').attr('title');
var re_id_hours = new RegExp('"d'+(nb_days-1)+'-h', 'g');
var re_name_hours = new RegExp('name="horaires'+(nb_days-1), 'g');
var new_day_html = last_day.html().replace(re_id_hours, '"d'+nb_days+'-h')
.replace('id="day'+(nb_days-1)+'"', 'id="day'+nb_days+'"')
.replace('for="day'+(nb_days-1)+'"', 'for="day'+nb_days+'"')
.replace(re_name_hours, 'name="horaires'+nb_days)
.replace(/value="(.*?)"/g, 'value=""')
.replace(/hours" title="(.*?)"/g, 'hours" title="" p')
.replace('title="'+last_day_title+'"', 'title="'+last_day_title.substring(0, last_day_title.indexOf(' '))+' '+(nb_days+1)+'"');
last_day.after('<fieldset>'+new_day_html+'</fieldset>');
$('#day'+(nb_days)).focus();
$('#remove-a-day, #copyhours').removeClass('disabled');
});
// Button "Remove a day"
$('#remove-a-day').on('click', function() {
$('#selected-days fieldset:last').remove();
var nb_days = $('#selected-days fieldset').length;
$('#day'+(nb_days-1)).focus();
if ( nb_days == 1) {
$('#remove-a-day, #copyhours').addClass('disabled');
};
SubmitDaysAvalaible();
});
// Title update on hours and buttons -/+ hours
$(document).on('change','#selected-days legend input', function() {
$('#selected-days .hours').each(function () {
$(this).attr('title', $(this).parents('fieldset').find('legend input').val()+' - '+$(this).attr('placeholder'));
});
$('#selected-days .add-an-hour, #selected-days .remove-an-hour').each(function () {
var old_title = $(this).attr('title');
if(old_title.indexOf('-')>0) {
old_title = old_title.substring(old_title.indexOf('-')+2,old_title.length);
}
$(this).attr('title', $(this).parents('fieldset').find('legend input').val()+' - '+old_title);
});
});
// 1 day and 2 hours or 2 days and you can submit
function SubmitDaysAvalaible() {
var nb_filled_days = 0;
var nb_filled_hours = 0;
$('#selected-days fieldset legend input').each(function() {
if($(this).val()!='') {
nb_filled_days++;
}
});
$('#selected-days .hours').each(function() {
if($(this).val()!='') {
nb_filled_hours++;
}
});
if (nb_filled_days>1) {
$('button[name="choixheures"]').removeClass('disabled');
} else if (nb_filled_hours>1 && nb_filled_days==1) {
$('button[name="choixheures"]').removeClass('disabled');
} else {
$('button[name="choixheures"]').addClass('disabled');
}
}
$(document).on('keyup, change','.hours, #selected-days fieldset legend input', function() {
SubmitDaysAvalaible();
});
SubmitDaysAvalaible();
// 2 days and you can remove a day or copy hours
if($('#selected-days fieldset').length>1) {
$('#remove-a-day, #copyhours').removeClass('disabled');
}
/**
* choix_autre.php
**/
// Button "Add a choice"
$('#add-a-choice').on('click', function() {
var nb_choices = $('.choice-field').length;
var last_choice = $('.choice-field:last');
var new_choice = last_choice.html();
// label
var last_choice_label = last_choice.children('label').text();
var choice_text = last_choice_label.substring(0, last_choice_label.indexOf(' '));
// for and id
var re_id_choice = new RegExp('"choice'+(nb_choices-1)+'"', 'g');
var last_choice_label = last_choice.children('label').text();
var new_choice_html = new_choice.replace(re_id_choice, '"choice'+nb_choices+'"')
.replace(last_choice_label, choice_text+' '+(nb_choices+1))
.replace(/value="(.*?)"/g, 'value=""');
last_choice.after('<div class="form-group choice-field">'+new_choice_html+'</div>');
$('#choice'+nb_choices).focus();
$('#remove-a-choice').removeClass('disabled');
});
// Button "Remove a choice"
$('#remove-a-choice').on('click', function() {
$('.choice-field:last').remove();
var nb_choices = $('.choice-field').length;
$('#choice'+(nb_choices-1)).focus();
if (nb_choices == 2) {
$('#remove-a-choice').addClass('disabled');
};
SubmitChoicesAvalaible();
});
// 2 choices filled and you can submit
function SubmitChoicesAvalaible() {
var nb_filled_choices = 0;
$('.choice-field input').each(function() {
if($(this).val()!='') {
nb_filled_choices++;
}
});
if(nb_filled_choices>1) {
$('button[name="fin_sondage_autre"]').removeClass('disabled');
} else {
$('button[name="fin_sondage_autre"]').addClass('disabled');
}
}
$(document).on('keyup, change','.choice-field input', function() {
SubmitChoicesAvalaible();
});
SubmitChoicesAvalaible();
$(document).on('click', '.md-a-img', function() {
$('#md-a-imgModal').modal('show');
$('#md-a-imgModal .btn-primary').attr('value',$(this).prev().attr('id'));
$('#md-a-imgModalLabel').text($(this).attr('title'));
});
$('#md-a-imgModal .btn-primary').on('click', function() {
if($('#md-img').val()!='' && $('#md-a').val()!='') {
$('#'+$(this).val()).val('[!['+$('#md-text').val()+']('+$('#md-img').val()+')]('+$('#md-a').val()+')');
} else if ($('#md-img').val()!='') {
$('#'+$(this).val()).val('!['+$('#md-text').val()+']('+$('#md-img').val()+')');
} else if ($('#md-a').val()!='') {
$('#'+$(this).val()).val('['+$('#md-text').val()+']('+$('#md-a').val()+')');
} else {
$('#'+$(this).val()).val($('#md-text').val());
}
$('#md-a-imgModal').modal('hide');
$('#md-img').val(''); $('#md-a').val('');$('#md-text').val('');
SubmitChoicesAvalaible();
});
/** /**
* adminstuds.php * adminstuds.php

View File

@ -36,4 +36,5 @@
</div> </div>
</div> </div>
</form> </form>
<script type="text/javascript" src="js/app/framadatepicker.js"></script>
{/block} {/block}