-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.js
127 lines (98 loc) · 2.89 KB
/
data.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
* This file is part of the QuidPHP package <https://quidphp.com>
* Author: Pierre-Philippe Emond <emondpph@gmail.com>
* License: https://github.com/quidphp/browser/blob/master/LICENSE
*/
// dataTarget
// script for storing datas within targets
// import
import { Arr, Pojo, Str } from '../index.js';
// export
export default {
// hasData
// retourne vrai si la node a les données
hasData: function(node,value)
{
return this.getData(node,value) !== undefined;
},
// getDataKey
// retourne la clé de date
getDataKey: function()
{
return '_quid_';
},
// allData
// retourne toutes les données liés à la node
// peut créer l'objet si inexistant
allData: function(node,create)
{
let r = undefined;
this.typecheck(node);
const dataKey = this.getDataKey();
let isPojo = Pojo.is(node[dataKey]);
if(!isPojo && create === true)
{
isPojo = true;
node[dataKey] = {};
}
if(isPojo === true)
r = node[dataKey];
return r;
},
// getData
// permet de retourner une data d'une node
// envoie une exception si plus d'une node
getData: function(node,key)
{
Str.typecheck(key);
const data = this.allData(node);
return (data != null)? Pojo.get(key,data):undefined;
},
// flashData
// retourne la data et efface l'entrée de la node
flashData: function(node,key)
{
const r = this.getData(node,key);
this.removeData(node,key);
return r;
},
// setData
// change de la data sur une ou plusieurs nodes
// ceci n'affecte pas le dom, seulement stocké dans l'objet
// si undefined efface la data
setData: function(nodes,key,value)
{
nodes = this.toArray(nodes);
Str.typecheck(key,true);
const $inst = this;
Arr.each(nodes,function(ele) {
const data = $inst.allData(ele,true);
if(value === undefined)
Pojo.unsetRef(key,data);
else
Pojo.setRef(key,value,data);
});
},
// removeData
// enlève une donnée de la ou les nodes
removeData: function(nodes,key)
{
return this.setData(nodes,key,undefined);
},
// getOrSetData
// crée une data dans une node si la valeur est présenement inexistante
// sinon retourne la data de la node
getOrSetData: function(node,key,value)
{
let r = undefined;
const current = this.getData(node,key);
if(current == null && value != null)
{
this.setData(node,key,value);
r = value;
}
else
r = current;
return r;
}
}