mirror of
https://github.com/alchemy-fr/Phraseanet.git
synced 2025-10-13 04:53:26 +00:00
Add jquery.Edit.recordField JS object
This commit is contained in:
277
www/include/js/jquery.Edit.js
Normal file
277
www/include/js/jquery.Edit.js
Normal file
@@ -0,0 +1,277 @@
|
|||||||
|
|
||||||
|
(function( window ) {
|
||||||
|
|
||||||
|
|
||||||
|
var recordFieldValue = function(meta_id, value) {
|
||||||
|
|
||||||
|
this.datas = {meta_id:meta_id, value:value};
|
||||||
|
|
||||||
|
var $this = this;
|
||||||
|
}
|
||||||
|
|
||||||
|
recordFieldValue.prototype = {
|
||||||
|
getValue : function() {
|
||||||
|
return this.datas.value;
|
||||||
|
},
|
||||||
|
getMetaId : function() {
|
||||||
|
return this.datas.meta_id;
|
||||||
|
},
|
||||||
|
setValue : function(value) {
|
||||||
|
this.datas.value = value;
|
||||||
|
return this;
|
||||||
|
},
|
||||||
|
remove : function() {
|
||||||
|
this.datas.value = '';
|
||||||
|
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
var recordField = function(name, meta_struct_id, options, arrayValues) {
|
||||||
|
|
||||||
|
var defaults = {
|
||||||
|
name : name,
|
||||||
|
multi : false,
|
||||||
|
required : false,
|
||||||
|
dirty : false
|
||||||
|
},
|
||||||
|
options = (typeof options == 'object') ? options : {};
|
||||||
|
|
||||||
|
if(isNaN(meta_struct_id))
|
||||||
|
{
|
||||||
|
throw 'meta_struct_id should be a number';
|
||||||
|
}
|
||||||
|
|
||||||
|
this.meta_struct_id = meta_struct_id;
|
||||||
|
this.options = jQuery.extend(defaults, options);
|
||||||
|
this.datas = new Array();
|
||||||
|
|
||||||
|
if(typeof arrayValues === 'object')
|
||||||
|
{
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(isNaN(arrayValues[v].getMetaId()))
|
||||||
|
{
|
||||||
|
if(window.console)
|
||||||
|
console.error('Trying to add a recordFieldValue without metaId...');
|
||||||
|
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var $this = this;
|
||||||
|
}
|
||||||
|
recordField.prototype = {
|
||||||
|
getName : function() {
|
||||||
|
return this.options.name;
|
||||||
|
},
|
||||||
|
isMulti : function() {
|
||||||
|
return this.options.multi;
|
||||||
|
},
|
||||||
|
isRequired : function() {
|
||||||
|
return this.options.required;
|
||||||
|
},
|
||||||
|
isDirty : function() {
|
||||||
|
return this.options.dirty;
|
||||||
|
},
|
||||||
|
addValue : function(value, merge) {
|
||||||
|
merge = !!merge;
|
||||||
|
|
||||||
|
if(window.console)
|
||||||
|
{
|
||||||
|
console.log('adding value ',value,' ; merge is ',merge);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(this.isMulti())
|
||||||
|
{
|
||||||
|
if(!this.hasValue(value))
|
||||||
|
{
|
||||||
|
if(window.console)
|
||||||
|
{
|
||||||
|
console.log('adding new multi value ',value);
|
||||||
|
}
|
||||||
|
this.datas.push(new recordFieldValue(null, value));
|
||||||
|
this.options.dirty = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if(merge === true && this.isEmpty() === false)
|
||||||
|
{
|
||||||
|
if(window.console)
|
||||||
|
{
|
||||||
|
console.log('Merging value ',value);
|
||||||
|
}
|
||||||
|
this.datas[0].setValue(this.datas[0].getValue() + ' ' + value);
|
||||||
|
this.options.dirty = true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if(!this.hasValue(value))
|
||||||
|
{
|
||||||
|
if(this.datas.length === 0)
|
||||||
|
{
|
||||||
|
if(window.console)
|
||||||
|
{
|
||||||
|
console.log('Adding new value ',value);
|
||||||
|
}
|
||||||
|
this.datas.push(new recordFieldValue(null, value));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if(window.console)
|
||||||
|
{
|
||||||
|
console.log('Updating value ',value);
|
||||||
|
}
|
||||||
|
this.datas[0].setValue(value);
|
||||||
|
}
|
||||||
|
this.options.dirty = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return this;
|
||||||
|
},
|
||||||
|
hasValue : function(value) {
|
||||||
|
|
||||||
|
if(typeof value === 'undefined')
|
||||||
|
{
|
||||||
|
if(window.console)
|
||||||
|
console.error('Trying to check the presence of an undefined value');
|
||||||
|
}
|
||||||
|
|
||||||
|
for(d in this.datas)
|
||||||
|
{
|
||||||
|
if(this.datas[d].getValue() == value)
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
},
|
||||||
|
removeValue : function(value) {
|
||||||
|
|
||||||
|
for(d in this.datas)
|
||||||
|
{
|
||||||
|
if(this.datas[d].getValue() == value)
|
||||||
|
{
|
||||||
|
this.datas[d].remove();
|
||||||
|
this.options.dirty = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
},
|
||||||
|
isEmpty : function() {
|
||||||
|
var empty = true;
|
||||||
|
|
||||||
|
for(d in this.datas)
|
||||||
|
{
|
||||||
|
if(this.datas[d].getValue() !== '')
|
||||||
|
empty = false;
|
||||||
|
}
|
||||||
|
return empty;
|
||||||
|
},
|
||||||
|
empty : function() {
|
||||||
|
|
||||||
|
for(d in this.datas)
|
||||||
|
{
|
||||||
|
this.datas[d].remove();
|
||||||
|
this.options.dirty = true;
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
},
|
||||||
|
getValue : function() {
|
||||||
|
|
||||||
|
if(this.isMulti())
|
||||||
|
throw 'This field is multi, I can not give you a single value';
|
||||||
|
|
||||||
|
if(this.isEmpty())
|
||||||
|
return null;
|
||||||
|
|
||||||
|
return this.datas[0];
|
||||||
|
},
|
||||||
|
getValues : function() {
|
||||||
|
|
||||||
|
if(!this.isMulti())
|
||||||
|
throw 'This field is not multi, I can not give you multiple values';
|
||||||
|
|
||||||
|
if(this.isEmpty())
|
||||||
|
return new Array();
|
||||||
|
|
||||||
|
var arrayValues = [];
|
||||||
|
|
||||||
|
for(d in this.datas)
|
||||||
|
{
|
||||||
|
if(this.datas[d].getValue() === '')
|
||||||
|
continue;
|
||||||
|
|
||||||
|
arrayValues.push(this.datas[d]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return arrayValues;
|
||||||
|
},
|
||||||
|
getSerializedValues : function() {
|
||||||
|
|
||||||
|
var arrayValues = [];
|
||||||
|
var values = this.getValues();
|
||||||
|
|
||||||
|
for(v in values)
|
||||||
|
{
|
||||||
|
arrayValues.push(values[v].getValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
return arrayValues.join(' ; ');
|
||||||
|
},
|
||||||
|
replaceValue : function(search, replace) {
|
||||||
|
|
||||||
|
for(d in this.datas)
|
||||||
|
{
|
||||||
|
var value = this.datas[d].getValue();
|
||||||
|
var replacedValue = value.replace(search, replace);
|
||||||
|
|
||||||
|
if(value === replacedValue)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
this.removeValue(value);
|
||||||
|
if(!this.hasValue(replacedValue))
|
||||||
|
{
|
||||||
|
this.addValue(replacedValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.options.dirty = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* cleanup and remove duplicates
|
||||||
|
*/
|
||||||
|
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
window.p4 = window.p4 || {};
|
||||||
|
|
||||||
|
window.p4.recordFieldValue = recordFieldValue;
|
||||||
|
window.p4.recordField = recordField;
|
||||||
|
|
||||||
|
})(window);
|
66
www/include/js/tests/jquery.Edit.js.html
Normal file
66
www/include/js/tests/jquery.Edit.js.html
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title></title>
|
||||||
|
<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>
|
||||||
|
<link type="text/css" rel="stylesheet" href="/include/vendor/qunit/qunit/qunit.css"/>
|
||||||
|
<script> $(document).ready(function(){
|
||||||
|
|
||||||
|
|
||||||
|
test("Selection instanciation", function() {
|
||||||
|
var field = new p4.recordField('Champ', 25);
|
||||||
|
equal( field.isMulti(), false, "Field is not multi" );
|
||||||
|
equal( field.isRequired(), false, "Field is not required" );
|
||||||
|
equal( field.isEmpty(), true, "Field is empty" );
|
||||||
|
|
||||||
|
var fieldMulti = new p4.recordField('Champ', 25, {multi:true});
|
||||||
|
equal( fieldMulti.isMulti(), true, "Field is multi" );
|
||||||
|
equal( fieldMulti.isRequired(), false, "Field is not required" );
|
||||||
|
|
||||||
|
var fieldRequired = new p4.recordField('Champ', 25, {required:true});
|
||||||
|
equal( fieldRequired.isMulti(), false, "Field is not multi" );
|
||||||
|
equal( fieldRequired.isRequired(), true, "Field is required" );
|
||||||
|
|
||||||
|
var fieldMultiRequired = new p4.recordField('Champ', 25, {required:true, multi:true});
|
||||||
|
equal( fieldMultiRequired.isMulti(), true, "Field is not multi" );
|
||||||
|
equal( fieldMultiRequired.isRequired(), true, "Field is required" );
|
||||||
|
|
||||||
|
field.addValue('Une valeur');
|
||||||
|
|
||||||
|
equal( field.isEmpty(), false, "Field is not empty" );
|
||||||
|
equal( field.isDirty(), true, "Field is dirty" );
|
||||||
|
equal( field.getValue().getValue(), 'Une valeur', "Field value" );
|
||||||
|
|
||||||
|
field.empty();
|
||||||
|
equal( field.getValue(), null, "Field is empty " );
|
||||||
|
|
||||||
|
|
||||||
|
var arrayValues = [
|
||||||
|
new p4.recordFieldValue(1, 'valeur')
|
||||||
|
];
|
||||||
|
|
||||||
|
var field = new p4.recordField('Champ', 25, {}, arrayValues);
|
||||||
|
equal( field.isEmpty(), false, "Field is not empty" );
|
||||||
|
|
||||||
|
field.empty();
|
||||||
|
equal( field.getValue(), null, "Field is empty " );
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
module("Add datas");
|
||||||
|
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1 id="qunit-header">QUnit example</h1>
|
||||||
|
<h2 id="qunit-banner"></h2>
|
||||||
|
<div id="qunit-testrunner-toolbar"></div>
|
||||||
|
<h2 id="qunit-userAgent"></h2>
|
||||||
|
<ol id="qunit-tests"></ol>
|
||||||
|
<div id="qunit-fixture">test markup, will be hidden</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
Reference in New Issue
Block a user