Fix Js errors

This commit is contained in:
Romain Neutron
2012-01-24 15:47:12 +01:00
parent 9d17412df2
commit a4d3e6388e
4 changed files with 551 additions and 64 deletions

View File

@@ -27,8 +27,12 @@
getVocabularyId : function() {
return this.datas.VocabularyId;
},
setValue : function(value) {
setValue : function(value, VocabularyId) {
if(typeof VocabularyId === 'undefined')
VocabularyId = null;
this.datas.value = value;
this.datas.VocabularyId = VocabularyId;
return this;
},
remove : function() {
@@ -63,6 +67,11 @@
this.meta_struct_id = meta_struct_id;
this.options = jQuery.extend(defaults, options);
if(this.options.multi === true && this.options.separator === null)
{
this.options.separator = ';';
}
};
databoxField.prototype = {
@@ -103,15 +112,21 @@
};
this.datas = new Array();
if(typeof arrayValues === 'object')
if(arrayValues instanceof Array)
{
if(arrayValues.length > 1 && !databoxField.isMulti())
throw 'You can not add multiple values to a non multi field';
var first = true;
for(v in arrayValues)
{
if(typeof arrayValues[v] !== 'object')
{
if(window.console)
{
console.error('Trying to add a non-recordFieldValue to the field...');
}
continue;
}
@@ -119,7 +134,9 @@
if(isNaN(arrayValues[v].getMetaId()))
{
if(window.console)
{
console.error('Trying to add a recordFieldValue without metaId...');
}
continue;
}
@@ -127,11 +144,15 @@
if(!first && this.options.multi === false)
{
if(window.console)
{
console.error('Trying to add multi values in a non-multi field');
}
}
if(window.console)
{
console.log('adding a value : ', arrayValues[v]);
}
this.datas.push(arrayValues[v]);
first = false;
@@ -166,7 +187,10 @@
if(this.databoxField.isReadonly())
{
if(window.console)
{
console.error('Unable to set a value to a readonly field');
}
return;
}
@@ -196,19 +220,29 @@
}
else
{
if(merge === true && this.isEmpty() === false)
if(merge === true && this.isEmpty() === false && VocabularyId === null)
{
if(window.console)
{
console.log('Merging value ',value);
}
this.datas[0].setValue(this.datas[0].getValue() + ' ' + value);
this.datas[0].setVocabularyId(VocabularyId);
this.datas[0].setValue(this.datas[0].getValue() + ' ' + value, VocabularyId);
this.options.dirty = true;
}
else
{
if(merge === true && this.isEmpty() === false && VocabularyId !== null)
{
if(window.console)
{
console.error('Cannot merge vocabularies');
}
this.datas[0].setValue(value, VocabularyId);
}
else
{
if(!this.hasValue(value, VocabularyId))
{
if(this.datas.length === 0)
@@ -225,24 +259,25 @@
{
console.log('Updating value ',value);
}
this.datas[0].setValue(value);
this.datas[0].setVocabularyId(VocabularyId);
this.datas[0].setValue(value, VocabularyId);
}
this.options.dirty = true;
}
}
}
}
return this;
},
hasValue : function(value, VocabularyId) {
console.log('has value ', value, VocabularyId);
if(typeof value === 'undefined')
{
if(window.console)
{
console.error('Trying to check the presence of an undefined value');
}
}
if(typeof VocabularyId === 'undefined')
VocabularyId = null;
@@ -276,7 +311,10 @@
if(this.databoxField.isReadonly())
{
if(window.console)
{
console.error('Unable to set a value to a readonly field');
}
return;
}
@@ -333,7 +371,10 @@
if(this.databoxField.isReadonly())
{
if(window.console)
{
console.error('Unable to set a value to a readonly field');
}
return;
}
@@ -398,17 +439,17 @@
if(this.databoxField.isReadonly())
{
if(window.console)
{
console.error('Unable to set a value to a readonly field');
return;
}
console.log('Search / Replace');
return;
}
for(d in this.datas)
{
if(this.datas[d].getVocabularyId() !== null)
{
console.log('value has vocabId, continue;');
continue;
}
@@ -417,14 +458,12 @@
if(value === replacedValue)
{
console.log('value', value, ' has not change, continue;');
continue;
}
this.removeValue(value);
if(!this.hasValue(replacedValue))
{
console.log('adding value ', replacedValue);
this.addValue(replacedValue);
}

View File

@@ -6,34 +6,38 @@
(function( window ) {
var Selectable = function(options) {
var Selectable = function($container, options) {
var defaults = {
allow_multiple : false,
container : window.document,
selector : '*',
selector : '',
callbackSelection : null,
selectStart : null,
selectStop : null
},
options = (typeof options == 'object') ? options : {};
var $this = this;
if($container.data('selectionnable'))
{
/* this container is already selectionnable */
if(window.console)
{
console.error('Trying to apply new selection to existing one');
}
return $container.data('selectionnable');
}
this.$container = $container;
this.options = jQuery.extend(defaults, options);
this.datas = new Array();
var $container = jQuery(this.options.container),
$this = this;
this.$container.data('selectionnable', this);
this.$container.addClass('selectionnable');
if(jQuery($container).hasClass('selectionnable'))
{
/* this container is already selectionnable */
return;
}
jQuery($container).addClass('selectionnable');
jQuery(this.options.selector, $container)
jQuery(this.options.selector, this.$container)
.live('click', function(event){
if(typeof $this.options.selectStart === 'function')
@@ -45,11 +49,11 @@
var k = get_value($that, $this);
if(is_shift_key(event) && jQuery('.last_selected', $container).filter($this.options.selector).length != 0)
if(is_shift_key(event) && jQuery('.last_selected', this.$container).filter($this.options.selector).length != 0)
{
var lst = jQuery($this.options.selector, $container);
var lst = jQuery($this.options.selector, this.$container);
var index1 = jQuery.inArray( jQuery('.last_selected', $container).filter($this.options.selector)[0], lst );
var index1 = jQuery.inArray( jQuery('.last_selected', this.$container).filter($this.options.selector)[0], lst );
var index2 = jQuery.inArray( $that[0], lst );
if(index2<index1)
@@ -63,7 +67,7 @@
{
var exp = $this.options.selector + ':gt(' + index1 + '):lt(' + (index2-index1) + ')';
$.each(jQuery(exp, $container),function(i,n){
$.each(jQuery(exp, this.$container),function(i,n){
if(!jQuery(n).hasClass('selected'))
{
var k = get_value(jQuery(n), $this);
@@ -84,7 +88,7 @@
if(!is_ctrl_key(event))
{
$this.empty().push(k);
jQuery('.selected', $container).filter($this.options.selector).removeClass('selected');
jQuery('.selected', this.$container).filter($this.options.selector).removeClass('selected');
$that.addClass('selected');
}
else
@@ -102,7 +106,7 @@
}
}
jQuery('.last_selected', $container).removeClass('last_selected');
jQuery('.last_selected', this.$container).removeClass('last_selected');
$that.addClass('last_selected');
@@ -130,13 +134,35 @@
}
}
function is_ctrl_key(event)
{
if(event.altKey)
return true;
if(event.ctrlKey)
return true;
if(event.metaKey) // apple key opera
return true;
if(event.keyCode == '17') // apple key opera
return true;
if(event.keyCode == '224') // apple key mozilla
return true;
if(event.keyCode == '91') // apple key safari
return true;
return false;
}
function is_shift_key(event)
{
if(event.shiftKey)
return true;
return false;
}
Selectable.prototype = {
push : function(element){
if(window.console)
{
window.console.log('pushing ',element);
}
if(this.options.allow_multiple === true || !this.has(element))
{
this.datas.push(element);
@@ -162,6 +188,8 @@
empty : function(){
this.datas = new Array();
jQuery(this.options.selector, this.$container).filter('.selected:visible').removeClass('selected');
return this;
},
length : function(){
@@ -181,7 +209,7 @@
selectAll : function(separator){
var $this = this;
jQuery(this.optionsthis.options.selector, $container).not('.selected').filter(':visible').each(function(){
jQuery(this.options.selector, this.$container).not('.selected').filter(':visible').each(function(){
$this.push(get_value(this, $this));
$(this).addClass('selected');
});

View File

@@ -5,21 +5,74 @@
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script src="/include/vendor/qunit/qunit/qunit.js"></script>
<script src="/include/js/jquery.Edit.js"></script>
<script src="../jquery.Edit.js"></script>
<link type="text/css" rel="stylesheet" href="/include/vendor/qunit/qunit/qunit.css"/>
<script> $(document).ready(function(){
module("Add datas");
test("Record field value", function(){
var FieldValue1 = new p4.recordFieldValue(1, 'valeur');
var FieldValue2 = new p4.recordFieldValue(2, 'valeur bidule', 3);
equal( FieldValue1.getVocabularyId(), null, "VocabularyId is null" );
equal( FieldValue2.getVocabularyId(), 3, "VocabularyId is not null" );
equal( FieldValue1.getValue(), 'valeur', "VocabularyId is null" );
equal( FieldValue2.getValue(), 'valeur bidule', "VocabularyId is not null" );
FieldValue1.setValue('Chouette');
FieldValue2.setValue('Caribosse');
equal( FieldValue1.getValue(), 'Chouette', "VocabularyId is null" );
equal( FieldValue2.getValue(), 'Caribosse', "VocabularyId is not null" );
equal( FieldValue1.getVocabularyId(), null, "VocabularyId is null" );
equal( FieldValue2.getVocabularyId(), null, "VocabularyId is not null" );
FieldValue2.setValue('Trou', 24);
equal( FieldValue2.getVocabularyId(), 24, "VocabularyId is not null" );
equal( FieldValue2.getValue(), 'Trou', "VocabularyId is not null" );
});
test("Selection instanciation", function() {
var DBField = new p4.databoxField('Champ', 25)
var field = new p4.recordField(DBField);
equal( field.getName(), 'Champ', "Field name is correct" );
equal( field.getMetaStructId(), 25, "Field meta_struct_id is correct" );
equal( field.isMulti(), false, "Field is not multi" );
equal( field.isRequired(), false, "Field is not required" );
equal( field.isEmpty(), true, "Field is empty" );
var DBFieldMulti = new p4.databoxField('Champ', 25, {multi:true})
var DBFieldMulti = new p4.databoxField('Champ', 25, {separator:''});
equal( DBFieldMulti.getSeparator(), '', "Empty separator is available" );
var DBFieldMulti = new p4.databoxField('Champ', 25,
{
minLength:2,
maxLength:344,
type:'ChaineEnOr',
separator:'-'
}
);
equal( DBFieldMulti.getMaxLength(), 344, "Default MaxLength is null" );
equal( DBFieldMulti.getMinLength(), 2, "Default MinLength is null" );
equal( DBFieldMulti.getType(), 'ChaineEnOr', "Default Type is string" );
equal( DBFieldMulti.getSeparator(), '-', "Default Separator is ;" );
var DBFieldMulti = new p4.databoxField('Champ', 25, {multi:true});
equal( DBFieldMulti.getMaxLength(), null, "Default MaxLength is null" );
equal( DBFieldMulti.getMinLength(), null, "Default MinLength is null" );
equal( DBFieldMulti.getType(), 'string', "Default Type is string" );
equal( DBFieldMulti.getSeparator(), ';', "Default Separator is ;" );
var fieldMulti = new p4.recordField(DBFieldMulti);
equal( fieldMulti.isMulti(), true, "Field is multi" );
@@ -47,6 +100,20 @@
equal( field.getValue(), null, "Field is empty " );
var arrayValues = new p4.recordFieldValue(1, 'valeur');
var field = new p4.recordField(DBField, arrayValues);
equal( field.isEmpty(), true, "Field empty" );
var arrayValues = 'valeur';
var field = new p4.recordField(DBField, arrayValues);
equal( field.isEmpty(), true, "Field empty" );
var arrayValues = ['valeur'];
var field = new p4.recordField(DBField, arrayValues);
equal( field.isEmpty(), true, "Field empty" );
var arrayValues = [
new p4.recordFieldValue(1, 'valeur')
];
@@ -54,12 +121,260 @@
var field = new p4.recordField(DBField, arrayValues);
equal( field.isEmpty(), false, "Field is not empty" );
field.empty();
equal( field.getValue(), null, "Field is empty " );
});
module("Add datas");
test("Test recordFieldValue non-multi", function() {
var DBField = new p4.databoxField('Champ', 25)
var arrayValues = [
new p4.recordFieldValue(1, 'valeur')
];
var field = new p4.recordField(DBField, arrayValues);
equal( field.isEmpty(), false, "Field is not empty" );
equal( field.getValue().getValue(), 'valeur' );
equal( field.getValue().getVocabularyId(), null );
field.addValue('TADAM', true);
equal( field.getValue().getValue(), 'valeur TADAM' );
equal( field.getValue().getVocabularyId(), null );
field.addValue('Brouille', true, 24);
equal( field.getValue().getValue(), 'Brouille' );
equal( field.getValue().getVocabularyId(), 24 );
field.addValue('Brouille', true);
equal( field.getValue().getValue(), 'Brouille Brouille' );
equal( field.getValue().getVocabularyId(), null );
field.addValue('Brouille Brouille', false);
equal( field.getValue().getValue(), 'Brouille Brouille' );
equal( field.getValue().getVocabularyId(), null );
field.addValue('Grenouille');
equal( field.getValue().getValue(), 'Grenouille' );
equal( field.getValue().getVocabularyId(), null );
field.removeValue('Grenouille');
equal( field.getValue(), null );
field.addValue('Grenouille', false, 23);
field.removeValue('Grenouille');
equal( field.getValue().getValue(), 'Grenouille' );
field.removeValue('Grenouillette', 24);
equal( field.getValue().getValue(), 'Grenouille' );
field.removeValue('Grenouillette', 23);
equal( field.getValue(), null);
field.addValue('Champignons de campagne');
equal( field.getValue().getValue(), 'Champignons de campagne');
field.replaceValue('Champignons', 'Champions');
equal( field.getValue().getValue(), 'Champions de campagne');
field.addValue('Champignons de campagne', false, 51);
field.replaceValue('Champignons', 'Champions');
equal( field.getValue().getValue(), 'Champignons de campagne');
});
test("Test recordFieldValue Multi", function() {
var DBField = new p4.databoxField('Champ', 25,{multi:true})
var arrayValues = [
new p4.recordFieldValue(1, 'valeur')
];
var field = new p4.recordField(DBField, arrayValues);
equal( field.isEmpty(), false, "Field is not empty" );
equal( field.getSerializedValues(), 'valeur' );
for(v in field.getValues())
{
equal( field.getValues()[v].getValue(), 'valeur' );
equal( field.getValues()[v].getVocabularyId(), null );
}
field.addValue('TADAM', true);
equal( field.getValues().length, 2 );
field.addValue('BADOUM', true, 54);
equal( field.getValues().length, 3 );
field.addValue('BADOUM', true);
equal( field.getValues().length, 4 );
field.addValue('BADOUM', true);
equal( field.getValues().length, 4 );
field.addValue('TROLOLO', true, 54);
equal( field.getValues().length, 4 );
field.removeValue('chameau', 54);
equal( field.getValues().length, 3 );
field.removeValue('TADAM');
equal( field.getValues().length, 2 );
field.empty();
equal( field.getValues().length, 0 );
field.addValue('Champignons de campagne');
equal( field.hasValue('Champignons de campagne'), true);
field.replaceValue('Champignons', 'Champions');
equal( field.hasValue('Champignons de campagne'), false);
equal( field.hasValue('Champions de campagne'), true);
field.empty();
equal( field.getValues().length, 0);
field.addValue('Champignons de campagne', false, 51);
field.replaceValue('Champignons', 'Champions');
equal( field.hasValue('Champignons de campagne', 51), true);
for(v in field.getValues())
{
equal( field.getValues()[v].getValue() , 'Champignons de campagne');
equal( field.getValues()[v].getVocabularyId() , 51);
}
});
});
test("Test recordFieldValue Readonly", function() {
var DBField = new p4.databoxField('Champ', 25,{readonly:true})
var arrayValues = [
new p4.recordFieldValue(1, 'valeur')
];
var field = new p4.recordField(DBField, arrayValues);
equal( field.isEmpty(), false, "Field is not empty" );
equal( field.getValue().getValue(), 'valeur' );
equal( field.getValue().getVocabularyId(), null );
field.empty();
equal( field.getValue().getValue(), 'valeur' );
equal( field.getValue().getVocabularyId(), null );
field.addValue('TADAM', true);
equal( field.getValue().getValue(), 'valeur' );
equal( field.getValue().getVocabularyId(), null );
field.addValue('Brouille', true, 24);
equal( field.getValue().getValue(), 'valeur' );
equal( field.getValue().getVocabularyId(), null );
field.addValue('Brouille', true);
equal( field.getValue().getValue(), 'valeur' );
equal( field.getValue().getVocabularyId(), null );
field.addValue('Brouille Brouille', false);
equal( field.getValue().getValue(), 'valeur' );
equal( field.getValue().getVocabularyId(), null );
field.addValue('Grenouille');
equal( field.getValue().getValue(), 'valeur' );
equal( field.getValue().getVocabularyId(), null );
field.removeValue('Grenouille');
equal( field.getValue().getValue(), 'valeur' );
equal( field.getValue().getVocabularyId(), null );
field.addValue('Grenouille', false, 23);
field.removeValue('Grenouille');
equal( field.getValue().getValue(), 'valeur' );
equal( field.getValue().getVocabularyId(), null );
field.removeValue('Grenouillette', 24);
equal( field.getValue().getValue(), 'valeur' );
equal( field.getValue().getVocabularyId(), null );
field.removeValue('Grenouillette', 23);
equal( field.getValue().getValue(), 'valeur' );
equal( field.getValue().getVocabularyId(), null );
field.addValue('Champignons de campagne');
equal( field.getValue().getValue(), 'valeur' );
equal( field.getValue().getVocabularyId(), null );
field.replaceValue('Champignons', 'Champions');
equal( field.getValue().getValue(), 'valeur' );
equal( field.getValue().getVocabularyId(), null );
field.addValue('Champignons de campagne', false, 51);
field.replaceValue('Champignons', 'Champions');
equal( field.getValue().getValue(), 'valeur' );
equal( field.getValue().getVocabularyId(), null );
});
</script>

View File

@@ -5,13 +5,13 @@
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script src="/include/vendor/qunit/qunit/qunit.js"></script>
<script src="/include/js/jquery.Selection.js"></script>
<script src="../jquery.Selection.js"></script>
<link type="text/css" rel="stylesheet" href="/include/vendor/qunit/qunit/qunit.css"/>
<script> $(document).ready(function(){
test("Selection instanciation", function() {
var sel = new Selection();
var sel = new Selectable($('#test_box2'));
var array = new Array();
equal( typeof array, typeof sel.get(), "We expect the selection to be an Array" );
equal( array.length, sel.get().length, "We expect the selection to be empty" );
@@ -20,7 +20,7 @@
module("Add datas");
test("first test within module", function() {
var sel = new Selection();
var sel = new Selectable($('#test_box3'));
sel.push('a');
equal( 1, sel.get().length, "We expect the selection to be empty" );
equal( 'a', sel.get().pop(), "We expect the selection to be empty" );
@@ -29,8 +29,8 @@
module("Multiple selections");
test("first test within module", function() {
var sel1 = new Selection();
var sel2 = new Selection();
var sel1 = new Selectable($('#test_box4'));
var sel2 = new Selectable($('#test_box5'));
sel1.push('a');
sel2.push('b');
@@ -42,7 +42,90 @@
equal( 'b', sel2.get().pop(), "We expect the selection to be empty" );
});
module("DOM selection");
test("DOM selection", function() {
var sel1 = new Selectable($('#test_box1'),
{
selector : 'li'
, allow_multiple : true
, callbackSelection : function(elem){
var lst = jQuery('li', $('#test_box1'));
var index = jQuery.inArray( elem[0], lst );
return 'item' + index;
}
}
);
equal(true, $('#test_box1').hasClass('selectionnable'));
equal(0, sel1.length(), 'Nothing is selected');
equal(0, sel1.size(), 'Nothing is selected');
sel1.selectAll();
equal(0, $('#test_box li:not(.selected)').length);
equal(10, sel1.length(), 'Everything is selected');
equal(10, sel1.size(), 'Everything is selected');
sel1.empty();
equal(sel1.length(), 0);
equal(sel1.size(), 0);
$('#test_box1 li:not(.selected):first').trigger('click');
equal(1, sel1.size(), 'First element selection');
var SKevent = jQuery.Event('click', { shiftKey : true });
$('#test_box1 li:not(.selected)').filter(':last').trigger(SKevent);
equal(sel1.length(), 10, 'Shift click selection');
equal(sel1.size(), 10);
sel1.empty();
equal(0, $('#test_box li:not(.selected)').length);
$('#test_box1 li:not(.selected):last').trigger('click');
equal(1, sel1.size(), 'Last element selection');
var SKevent = jQuery.Event('click', { shiftKey : true });
$('#test_box1 li:not(.selected)').filter(':first').trigger(SKevent);
equal(sel1.length(), 10, 'Shift click selection reversed');
equal(sel1.size(), 10);
$('#test_box1 li:first').trigger('click');
equal(sel1.length(), 1, 'Start new selection');
equal($('#test_box1 li:last').hasClass('selected'), false, 'last item does not have selected class');
var CKevent = jQuery.Event('click', { ctrlKey : true });
$('#test_box1 li:last').trigger(CKevent);
equal(sel1.length(), 2, 'Ctrl click');
var CKevent = jQuery.Event('click', { ctrlKey : true });
$('#test_box1 li:last').trigger(CKevent);
equal(sel1.length(), 1, 'Ctrl click');
var CKevent = jQuery.Event('click', { ctrlKey : true });
$('#test_box1 li:last').trigger(CKevent);
equal(sel1.length(), 2, 'Ctrl click');
equal(sel1.serialize(), 'item0;item9');
equal(sel1.serialize('!u'), 'item0!uitem9');
});
});
</script>
@@ -54,5 +137,27 @@
<h2 id="qunit-userAgent"></h2>
<ol id="qunit-tests"></ol>
<div id="qunit-fixture">test markup, will be hidden</div>
<div id="test_box1">
<ul>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
</div>
<div id="test_box2">
</div>
<div id="test_box3">
</div>
<div id="test_box4">
</div>
<div id="test_box5">
</div>
</body>
</html>