web/data/cubicweb.preferences.js
changeset 5658 7b9553a9db65
parent 4280 15d541321a8c
child 6549 be6d49623207
equal deleted inserted replaced
5655:ef903fff826d 5658:7b9553a9db65
     1 /* toggle visibility of an element by its id
     1 /**
       
     2  * toggle visibility of an element by its id
     2  * & set current visibility status in a cookie
     3  * & set current visibility status in a cookie
     3  * XXX whenever used outside of preferences, don't forget to
     4  * XXX whenever used outside of preferences, don't forget to
     4  *     move me in a more appropriate place
     5  *     move me in a more appropriate place
     5  */
     6  */
     6 
     7 
     9 function togglePrefVisibility(elemId) {
    10 function togglePrefVisibility(elemId) {
    10     clearPreviousMessages();
    11     clearPreviousMessages();
    11     jQuery('#' + elemId).toggleClass('hidden');
    12     jQuery('#' + elemId).toggleClass('hidden');
    12 }
    13 }
    13 
    14 
    14 function closeFieldset(fieldsetid){
    15 function closeFieldset(fieldsetid) {
    15     var linklabel = _('open all');
    16     var linklabel = _('open all');
    16     var linkhref = 'javascript:openFieldset("' +fieldsetid + '")';
    17     var linkhref = 'javascript:openFieldset("' + fieldsetid + '")';
    17     _toggleFieldset(fieldsetid, 1, linklabel, linkhref);
    18     _toggleFieldset(fieldsetid, 1, linklabel, linkhref);
    18 }
    19 }
    19 
    20 
    20 function openFieldset(fieldsetid){
    21 function openFieldset(fieldsetid) {
    21     var linklabel = _('close all');
    22     var linklabel = _('close all');
    22     var linkhref = 'javascript:closeFieldset("'+ fieldsetid + '")';
    23     var linkhref = 'javascript:closeFieldset("' + fieldsetid + '")';
    23     _toggleFieldset(fieldsetid, 0, linklabel, linkhref);
    24     _toggleFieldset(fieldsetid, 0, linklabel, linkhref);
    24 }
    25 }
    25 
    26 
    26 function _toggleFieldset(fieldsetid, closeaction, linklabel, linkhref){
    27 function _toggleFieldset(fieldsetid, closeaction, linklabel, linkhref) {
    27     jQuery('#'+fieldsetid).find('div.openlink').each(function(){
    28     jQuery('#' + fieldsetid).find('div.openlink').each(function() {
    28 	    var link = A({'href' : "javascript:noop();",
    29         var link = A({
    29 			  'onclick' : linkhref},
    30             'href': "javascript:noop();",
    30 			  linklabel);
    31             'onclick': linkhref
    31 	    jQuery(this).empty().append(link);
    32         },
    32 	});
    33         linklabel);
    33     jQuery('#'+fieldsetid).find('fieldset[id]').each(function(){
    34         jQuery(this).empty().append(link);
    34 	    var fieldset = jQuery(this);
    35     });
    35 	    if(closeaction){
    36     jQuery('#' + fieldsetid).find('fieldset[id]').each(function() {
    36 		fieldset.addClass('hidden');
    37         var fieldset = jQuery(this);
    37 	    }else{
    38         if (closeaction) {
    38 		fieldset.removeClass('hidden');
    39             fieldset.addClass('hidden');
    39 		linkLabel = (_('open all'));
    40         } else {
    40 	    }
    41             fieldset.removeClass('hidden');
    41 	});
    42             linkLabel = (_('open all'));
       
    43         }
       
    44     });
    42 }
    45 }
    43 
    46 
    44 function validatePrefsForm(formid){
    47 function validatePrefsForm(formid) {
    45     clearPreviousMessages();
    48     clearPreviousMessages();
    46     clearPreviousErrors(formid);
    49     clearPreviousErrors(formid);
    47     return validateForm(formid, null,  submitSucces, submitFailure);
    50     return validateForm(formid, null, submitSucces, submitFailure);
    48 }
    51 }
    49 
    52 
    50 function submitFailure(formid){
    53 function submitFailure(formid) {
    51     var form = jQuery('#'+formid);
    54     var form = jQuery('#' + formid);
    52     var dom = DIV({'class':'critical'},
    55     var dom = DIV({
    53 		  _("please correct errors below"));
    56         'class': 'critical'
       
    57     },
       
    58     _("please correct errors below"));
    54     jQuery(form).find('div.formsg').empty().append(dom);
    59     jQuery(form).find('div.formsg').empty().append(dom);
    55     // clearPreviousMessages()
    60     // clearPreviousMessages()
    56     jQuery(form).find('span.error').next().focus();
    61     jQuery(form).find('span.error').next().focus();
    57 }
    62 }
    58 
    63 
    59 function submitSucces(url, formid){
    64 function submitSucces(url, formid) {
    60     var form = jQuery('#'+formid);
    65     var form = jQuery('#' + formid);
    61     setCurrentValues(form);
    66     setCurrentValues(form);
    62     var dom = DIV({'class':'msg'},
    67     var dom = DIV({
    63 		  _("changes applied"));
    68         'class': 'msg'
       
    69     },
       
    70     _("changes applied"));
    64     jQuery(form).find('div.formsg').empty().append(dom);
    71     jQuery(form).find('div.formsg').empty().append(dom);
    65     jQuery(form).find('input').removeClass('changed');
    72     jQuery(form).find('input').removeClass('changed');
    66     checkValues(form, true);
    73     checkValues(form, true);
    67     return;
    74     return;
    68 }
    75 }
    74 
    81 
    75 function clearPreviousErrors(formid) {
    82 function clearPreviousErrors(formid) {
    76     jQuery('#err-value:' + formid).remove();
    83     jQuery('#err-value:' + formid).remove();
    77 }
    84 }
    78 
    85 
    79 function checkValues(form, success){
    86 function checkValues(form, success) {
    80     var unfreezeButtons = false;
    87     var unfreezeButtons = false;
    81     jQuery(form).find('select').each(function () {
    88     jQuery(form).find('select').each(function() {
    82 	    unfreezeButtons = _checkValue(jQuery(this), unfreezeButtons);
    89         unfreezeButtons = _checkValue(jQuery(this), unfreezeButtons);
    83 	});
    90     });
    84     jQuery(form).find('[type=text]').each(function () {
    91     jQuery(form).find('[type=text]').each(function() {
    85 	    unfreezeButtons = _checkValue(jQuery(this), unfreezeButtons);
    92         unfreezeButtons = _checkValue(jQuery(this), unfreezeButtons);
    86 	});
    93     });
    87     jQuery(form).find('input[type=radio]:checked').each(function () {
    94     jQuery(form).find('input[type=radio]:checked').each(function() {
    88             unfreezeButtons = _checkValue(jQuery(this), unfreezeButtons);
    95         unfreezeButtons = _checkValue(jQuery(this), unfreezeButtons);
    89      });
    96     });
    90 
    97 
    91     if (unfreezeButtons){
    98     if (unfreezeButtons) {
    92 	unfreezeFormButtons(form.attr('id'));
    99         unfreezeFormButtons(form.attr('id'));
    93     }else{
   100     } else {
    94 	if (!success){
   101         if (!success) {
    95 	    clearPreviousMessages();
   102             clearPreviousMessages();
    96 	}
   103         }
    97 	clearPreviousErrors(form.attr('id'));
   104         clearPreviousErrors(form.attr('id'));
    98 	freezeFormButtons(form.attr('id'));
   105         freezeFormButtons(form.attr('id'));
    99     }
   106     }
   100 }
   107 }
   101 
   108 
   102 function _checkValue(input, unfreezeButtons){
   109 function _checkValue(input, unfreezeButtons) {
   103     var currentValue = prefsValues[input.attr('name')];
   110     var currentValue = prefsValues[input.attr('name')];
   104      if (currentValue != input.val()){
   111     if (currentValue != input.val()) {
   105 	 input.addClass('changed');
   112         input.addClass('changed');
   106 	 unfreezeButtons = true;
   113         unfreezeButtons = true;
   107      }else{
   114     } else {
   108 	 input.removeClass('changed');
   115         input.removeClass('changed');
   109 	 jQuery("span[id=err-" + input.attr('id') + "]").remove();
   116         jQuery("span[id=err-" + input.attr('id') + "]").remove();
   110      }
   117     }
   111      input.removeClass('error');
   118     input.removeClass('error');
   112      return unfreezeButtons;
   119     return unfreezeButtons;
   113 }
   120 }
   114 
   121 
   115 function setCurrentValues(form){
   122 function setCurrentValues(form) {
   116     jQuery(form).find('[name^=value]').each(function () {
   123     jQuery(form).find('[name^=value]').each(function() {
   117 	var input = jQuery(this);
   124         var input = jQuery(this);
   118 	var name = input.attr('name');
   125         var name = input.attr('name');
   119 	if(input.attr('type') == 'radio'){
   126         if (input.attr('type') == 'radio') {
   120 	    // NOTE: there seems to be a bug with jQuery(input).attr('checked')
   127             // NOTE: there seems to be a bug with jQuery(input).attr('checked')
   121 	    //       in our case, we can't rely on its value, we use
   128             //       in our case, we can't rely on its value, we use
   122 	    //       the DOM API instead.
   129             //       the DOM API instead.
   123 	    if(input[0].checked){
   130             if (input[0].checked) {
   124 		prefsValues[name] = input.val();
   131                 prefsValues[name] = input.val();
   125 	    }
   132             }
   126 	}else{
   133         } else {
   127 	    prefsValues[name] = input.val();
   134             prefsValues[name] = input.val();
   128 	}
   135         }
   129 	jQuery(form).find('input[name=edits-'+ name + ']').val(prefsValues[name]);
   136         jQuery(form).find('input[name=edits-' + name + ']').val(prefsValues[name]);
   130     });
   137     });
   131 }
   138 }
   132 
   139 
   133 function initEvents(){
   140 function initEvents() {
   134     jQuery('form').each(function() {
   141     jQuery('form').each(function() {
   135 	var form = jQuery(this);
   142         var form = jQuery(this);
   136 	//freezeFormButtons(form.attr('id'));
   143         //freezeFormButtons(form.attr('id'));
   137 	form.find('.validateButton').attr('disabled', 'disabled');
   144         form.find('.validateButton').attr('disabled', 'disabled');
   138 	form.find('input[type=text]').keyup(function(){
   145         form.find('input[type=text]').keyup(function() {
   139 	    checkValues(form);
   146             checkValues(form);
   140 	});
   147         });
   141 	form.find('input[type=radio]').change(function(){
   148         form.find('input[type=radio]').change(function() {
   142 	    checkValues(form);
   149             checkValues(form);
   143 	});
   150         });
   144 	form.find('select').change(function(){
   151         form.find('select').change(function() {
   145 	    checkValues(form);
   152             checkValues(form);
   146 	});
   153         });
   147 	setCurrentValues(form);
   154         setCurrentValues(form);
   148     });
   155     });
   149 }
   156 }
   150 
   157 
   151 $(document).ready(function() {
   158 $(document).ready(function() {
   152 	initEvents();
   159     initEvents();
   153 });
   160 });
       
   161