a0ab494c9a1195eb6dd73550c6b69f1d4f262965
[Evergreen.git] / Open-ILS / web / js / dojo / fieldmapper / dojoData.js
1 /*
2 # ---------------------------------------------------------------------------
3 # Copyright (C) 2008  Georgia Public Library Service / Equinox Software, Inc
4 # Mike Rylander <miker@esilibrary.com>
5
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
10
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 # ---------------------------------------------------------------------------
16 */
17
18 if(!dojo._hasResource['fieldmapper.dojoData']){
19
20         dojo._hasResource['fieldmapper.dojoData'] = true;
21         dojo.provide('fieldmapper.dojoData');
22         dojo.require('fieldmapper.Fieldmapper');
23         dojo.require('fieldmapper.hash');
24
25
26         function _fromStoreItem (data) {
27                 this.fromHash(data);
28
29                 for (var i in this._ignore_fields)
30                         this[this._ignore_fields[i]](null);
31
32                 for ( var i=0; i < this._fields.length; i++) {
33                         if (dojo.isArray( this[this._fields[i]]() ))
34                                 this[this._fields[i]]( this[this._fields[i]]()[0] );
35                 }
36                 return this;
37         }
38
39         function _toStoreData (list, label, params) {
40
41                 if (!params) params = {};
42                 if (!list) list = {};
43
44                 // a sane default
45                 if (!params.identifier) params.identifier = this.Identifier;
46                 if (!label) label = params.label;
47                 if (!label) label = params.identifier;
48
49                 var data = { label : label, identifier : params.identifier, items : [] };
50
51                 for (var i in list) data.items.push( list[i].toHash(true) );
52
53                 if (params.children && params.parent) {
54                         var _hash_list = data.items;
55
56                         var _find_root = {};
57                         for (var i in _hash_list) {
58                                 _find_root[_hash_list[i][params.identifier]] = _hash_list[i]; 
59                         }
60
61                         var item_data = [];
62                         for (var i in _hash_list) {
63                                 var obj = _hash_list[i]
64                                 obj[params.children] = [];
65
66                                 for (var j in _hash_list) {
67                                         var kid = _hash_list[j];
68                                         if (kid[params.parent] == obj[params.identifier]) {
69                                                 obj[params.children].push( { _reference : kid[params.identifier] } );
70                                                 kid._iskid = true;
71                                                 if (_find_root[kid[params.identifier]]) delete _find_root[kid[params.identifier]];
72                                         }
73                                 }
74
75                                 item_data.push( obj );
76                         }
77
78                         for (var j in _find_root) {
79                                 _find_root[j]['_top'] = 'true';
80                                 if (!_find_root[j][params.parent])
81                                         _find_root[j]['_trueRoot'] = 'true';
82                         }
83
84                         data.items = item_data;
85                 }
86
87                 return data;
88         }
89
90         for (var i in fmclasses) fieldmapper[i].prototype.fromStoreItem = _fromStoreItem;
91         for (var i in fmclasses) fieldmapper[i].toStoreData = _toStoreData;
92
93         fieldmapper.aou.prototype._ignore_fields = ['children'];
94         fieldmapper.aout.prototype._ignore_fields = ['children'];
95         fieldmapper.pgt.prototype._ignore_fields = ['children'];
96
97         fieldmapper.aou.toStoreData = function (list, label) {
98                 if (!label) label = 'shortname';
99                 return _toStoreData.call(this, list, label, { 'parent' : 'parent_ou', 'children' : 'children' });
100         }
101
102         fieldmapper.aout.toStoreData = function (list, label) {
103                 if (!label) label = 'name';
104                 return _toStoreData.call(this, list, label, { 'parent' : 'parent', 'children' : 'children' });
105         }
106
107         fieldmapper.pgt.toStoreData = function (list, label) {
108                 if (!label) label = 'name';
109                 return _toStoreData.call(this, list, label, { 'parent' : 'parent', 'children' : 'children' });
110         }
111
112         /*
113         ppl.toStoreData = function (list, label) {
114                 if (!label) label = 'code';
115                 return _toStoreData(list, label, {});
116         }
117         */
118
119 }