PageRenderTime 39ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 1ms

/www/js/model.js

https://github.com/NaturalSolutions/sauvage
JavaScript | 408 lines | 279 code | 81 blank | 48 comment | 8 complexity | d7cb0c94e3f30b86be0e4fb2a215ee40 MD5 | raw file
  1. "use strict";
  2. Backbone.Collection.prototype.save = function (options) {
  3. $.each(this.models, function(index, model) {
  4. model.save();
  5. });
  6. };
  7. Backbone.Model.prototype.initialize = Backbone.Collection.prototype.initialize = function () {
  8. var schema = (typeof(this.constructor.schema) === 'undefined') ? this.model.schema : this.constructor.schema;
  9. var subobjects = _.map(schema, function(field, key) {
  10. if (field.type === 'NestedModel' ) return key;
  11. });
  12. subobjects = _.filter(subobjects, function(key){ return (typeof(key) !== 'undefined') ; });
  13. if (typeof(subobjects) !== 'undefined') {
  14. var self = this;
  15. $.each(subobjects, function(index, subPropertyName) {
  16. if (schema[subPropertyName].fetch) {
  17. var subPropertyModel = new app['models'][schema[subPropertyName].className]();
  18. subPropertyModel.filters = new Object();
  19. if ( self.get(schema[subPropertyName].pk)) {
  20. subPropertyModel.filters[schema[subPropertyName].fk] = self.get(schema[subPropertyName].pk);
  21. subPropertyModel.fetch({
  22. success: function(data) {
  23. self.set(subPropertyName,subPropertyModel);
  24. }
  25. });
  26. }
  27. }
  28. });
  29. }
  30. };
  31. // Return models with matching attributes. Useful for simple cases of
  32. // `filter`.
  33. Backbone.Collection.prototype.multiValueWhere = function(attrs, first) {
  34. if (_.isEmpty(attrs)) return first ? void 0 : [];
  35. return this[first ? 'find' : 'filter'](function(model) {
  36. for (var key in attrs) {
  37. for (var ind in attrs[key] ) {
  38. if (attrs[key][ind] === model.get(key)) return true;
  39. }
  40. }
  41. return false;
  42. });
  43. };
  44. // -------------------------------------------------- The Models ---------------------------------------------------- //
  45. app.models.User = Backbone.Model.extend({
  46. defaults: {
  47. },
  48. },{
  49. table : 'Tuser',
  50. schema: {
  51. id: { title:'id',type:'hidden', sqltype:'INTEGER', sqlconstraints:'PRIMARY KEY', autoincrement:true},
  52. email: { title:'Ajouter votre email.', type:'Email', sqltype:'NVARCHAR(50)', required: true},
  53. // pseudo: { title:'commonName', type:'Text', sqltype:'NVARCHAR(50)' },
  54. },
  55. dao: app.dao.UserDAO,
  56. verboseName: 'Utilisateur'
  57. });
  58. // The User Collection Model
  59. app.models.UserCollection = Backbone.Collection.extend({
  60. model: app.models.User,
  61. });
  62. // Application model
  63. app.models.Application = Backbone.Model.extend({
  64. defaults: {
  65. },
  66. },{
  67. table : 'Tapplication',
  68. schema: {
  69. id: { title:'id',type:'hidden', sqltype:'INTEGER', sqlconstraints:'PRIMARY KEY', autoincrement:true},
  70. aide: { title:'aide', type:'hidden', sqltype:'INTEGER', required: true},
  71. },
  72. dao: app.dao.ApplicationDAO,
  73. verboseName: 'Application'
  74. });
  75. // The User Collection Model
  76. app.models.ApplicationCollection = Backbone.Collection.extend({
  77. model: app.models.Application,
  78. });
  79. // The Taxon Model
  80. /// !!!!!!!!!!!!!!! !!!!!!!!!!!!!!! !!!!!!!!!!!!!!! !!!!!!!!!!!!!!! !!!!!!!!!!!!!!!
  81. /// En cas de modification des modèles taxons/Critères et Picture
  82. /// penser à modifier les requêtes écrites et les valeurs récupérées dans le XML
  83. /// FICHIER : database.js loadXmlTaxa()
  84. /// !!!!!!!!!!!!!!! !!!!!!!!!!!!!!! !!!!!!!!!!!!!!! !!!!!!!!!!!!!!! !!!!!!!!!!!!!!!
  85. app.models.Taxon = Backbone.Model.extend({
  86. },{
  87. //@TODO reflechir et implémenter aux actions en cascade
  88. //delete : true/false
  89. table : 'Ttaxon',
  90. schema: {
  91. taxonId: { title:'taxonId',type:'Number', sqltype:'INTEGER', required: true, sqlconstraints:'PRIMARY KEY'},
  92. fk_group: { title:'fk_group', sqltype:'INTEGER'},
  93. commonName: { title:'commonName', type:'Text', sqltype:'NVARCHAR(200)', required: true },
  94. scientificName: { title:'scientificName', type:'Text', sqltype:'NVARCHAR(500)', required: true },
  95. description: { title:'description', type:'TextArea', sqltype:'text'},
  96. picture : { title:'picture', type:'Text', sqltype:'NVARCHAR(500)', required: true},
  97. pictures : {type: 'NestedModel', model: 'app.models.PicturesCollection',className : 'PicturesCollection' , pk : 'taxonId', fk: 'fk_taxon', fetch: true, save:true},
  98. caracValues : {type: 'NestedModel', model: 'app.models.TaxonCaracValuesCollection', className : 'TaxonCaracValuesCollection', pk : 'taxonId', fk: 'fk_taxon', fetch: true, save:true},
  99. regionPaca: { title:'regionPaca', type:'Number',sqltype:'INTEGER'}
  100. },
  101. dao: app.dao.TaxonDAO,
  102. });
  103. // The TaxonCollection Model
  104. app.models.TaxonCollection = Backbone.Collection.extend({
  105. model: app.models.Taxon,
  106. });
  107. // The Taxon Model lite
  108. app.models.TaxonLite = Backbone.Model.extend({
  109. },{
  110. //@TODO reflechir et implémenter aux actions en cascade
  111. //delete : true/false
  112. table : 'Ttaxon',
  113. schema: {
  114. taxonId: { title:'taxonId',type:'Number', sqltype:'INTEGER', sqlconstraints:'PRIMARY KEY', required: true},
  115. fk_group: { title:'fk_group', sqltype:'INTEGER'},
  116. commonName: { title:'commonName', type:'Text', sqltype:'NVARCHAR(200)', required: true },
  117. scientificName: { title:'scientificName', type:'Text', sqltype:'NVARCHAR(500)', required: true },
  118. picture : { title:'picture', type:'Text', sqltype:'NVARCHAR(500)', required: true},
  119. regionPaca: { title:'regionPaca', type:'Number',sqltype:'INTEGER'},
  120. },
  121. dao: app.dao.TaxonDAO,
  122. });
  123. // The TaxonCollection Model
  124. app.models.TaxonLiteCollection = Backbone.Collection.extend({
  125. model: app.models.TaxonLite,
  126. });
  127. // Espece CEL Model id, num_nom, nom_sci, famille, num_taxon, referentiel
  128. app.models.EspeceCel = Backbone.Model.extend({
  129. },{
  130. table : 'TespeceCel',
  131. schema: {
  132. id: { title:'id',type:'Number', sqltype:'INTEGER', sqlconstraints:'PRIMARY KEY',autoincrement:true},
  133. num_nom: { title:'num_nom', type:'Number', sqltype:'NVARCHAR(500)', required: true },
  134. nom_sci: { title:'nom_sci', type:'Text', sqltype:'NVARCHAR(500)', required: true },
  135. famille: { title:'famille', type:'Text', sqltype:'NVARCHAR(500)' },
  136. num_taxon : { title:'num_taxon', sqltype:'INTEGER'},
  137. referentiel: { title:'referentiel', type:'Text', sqltype:'NVARCHAR(50)'},
  138. },
  139. dao: app.dao.EspeceCELDataValueDAO,
  140. });
  141. // Espece CEL Model
  142. app.models.EspeceCelCollection = Backbone.Collection.extend({
  143. model: app.models.EspeceCel,
  144. findByName: function(key) {
  145. var self = this;
  146. new app.dao.EspeceCELDataValueDAO.findByName(key, function(data) {
  147. self.reset(data);
  148. });
  149. }
  150. });
  151. // The Picture Model
  152. app.models.Picture =Backbone.Model.extend({
  153. },{
  154. table : 'Tpicture',
  155. schema: {
  156. id: { title:'id', type:'hidden', sqltype:'INTEGER', sqlconstraints:'PRIMARY KEY', autoincrement:true},
  157. fk_taxon: { title:'fk_taxon', type:'Number', sqltype:'INTEGER', required: true},
  158. path : { title:'path', type:'Text', sqltype:'NVARCHAR(500)',required: true},
  159. description: { title:'description',type:'TextArea', sqltype:'text'},
  160. author: { title:'author', type:'Text', sqltype:'NVARCHAR(500)',},
  161. },
  162. dao: app.dao.PictureDAO,
  163. });
  164. // The Picture Collections
  165. app.models.PicturesCollection =Backbone.Collection.extend({
  166. model : app.models.Picture,
  167. });
  168. // The Taxon carac value Model
  169. app.models.TaxonCaracValue = Backbone.Model.extend({
  170. },{
  171. table : 'TvalTaxon_Criteria_values',
  172. schema: {
  173. id: { title:'id', type:'hidden', sqltype:'INTEGER', sqlconstraints:'PRIMARY KEY', autoincrement:true},
  174. fk_taxon: { title:'fk_taxon', type:'Number', sqltype:'INTEGER', required: true},
  175. fk_carac_value : { title:'fk_carac_value', type:'Text', sqltype:'NVARCHAR(20)', required: true},
  176. },
  177. dao: app.dao.TaxonCaracValueDAO,
  178. });
  179. // The Taxon carac value Collection
  180. app.models.TaxonCaracValuesCollection =Backbone.Collection.extend({
  181. model : app.models.TaxonCaracValue,
  182. });
  183. // The Groupe Model
  184. app.models.Groupe = Backbone.Model.extend({
  185. },{
  186. table : 'Ttaxa_group',
  187. schema: {
  188. groupId: { title:'groupId', type:'Number', sqltype:'INTEGER' , sqlconstraints:'PRIMARY KEY'},
  189. name: { title:'name',type:'Text'},
  190. description: { title:'description', type:'TextArea'},
  191. picture : { title:'picture',type:'Text', sqltype:'NVARCHAR(500)', required: true},
  192. },
  193. dao: app.dao.GroupeDAO,
  194. });
  195. // The CaracteristiqueDef Model
  196. /// !!!!!!!!!!!!!!! !!!!!!!!!!!!!!! !!!!!!!!!!!!!!! !!!!!!!!!!!!!!! !!!!!!!!!!!!!!!
  197. /// En cas de modification des modèles définition Groupes Critères et Valeurs
  198. /// penser à modifier les données récupérées dans le XML
  199. /// FICHIER : database.js loadXmlCriteria()
  200. /// !!!!!!!!!!!!!!! !!!!!!!!!!!!!!! !!!!!!!!!!!!!!! !!!!!!!!!!!!!!! !!!!!!!!!!!!!!!
  201. app.models.CaracteristiqueDef = Backbone.Model.extend({
  202. },{
  203. /* subobjects:{
  204. defCaracValues : {type: 'app.models.CaracteristiqueDefValuesCollection',className : 'CaracteristiqueDefValuesCollection' , pk : 'criteraId', fk: 'fk_criteria', fetch: true, save:true},
  205. },*/
  206. table : 'TdefCriteria',
  207. schema: {
  208. criteraId: { title:'criteraId', type:'Text', sqltype:'NVARCHAR(20)', sqlconstraints:'PRIMARY KEY'},
  209. name: { title:'name',type:'Text'},
  210. description: { title:'description', type:'TextArea'},
  211. fk_group: { title:'fk_group', type:'Number', sqltype:'INTEGER'},
  212. defCaracValues : {type: 'NestedModel', model: 'app.models.CaracteristiqueDefValuesCollection',className : 'CaracteristiqueDefValuesCollection' , pk : 'criteraId', fk: 'fk_criteria', fetch: true, save:true},
  213. },
  214. dao: app.dao.CaracteristiqueDefDAO,
  215. });
  216. // The CaracteristiqueDef Collection
  217. app.models.CaracteristiqueDefsCollection =Backbone.Collection.extend({
  218. model : app.models.CaracteristiqueDef,
  219. });
  220. // The CaracteristiqueDefValue Model
  221. app.models.CaracteristiqueDefValue = Backbone.Model.extend({
  222. },{
  223. table : 'TdefCriteria_values',
  224. schema: {
  225. criteraValueId: { title:'criteraValueId', type:'Text',sqltype:'NVARCHAR(20)', sqlconstraints:'PRIMARY KEY'},
  226. fk_criteria: { title:'fk_criteria', type:'Text',sqltype:'NVARCHAR(20)', required: true},
  227. name: { title:'name', type:'Text'},
  228. picture : { title:'picture',type:'Text', sqltype:'NVARCHAR(500)', required: true},
  229. },
  230. dao: app.dao.CaracteristiqueDefValueDAO,
  231. });
  232. // The CaracteristiqueDefValue Collection
  233. app.models.CaracteristiqueDefValuesCollection =Backbone.Collection.extend({
  234. model : app.models.CaracteristiqueDefValue,
  235. });
  236. // The Context Model
  237. app.models.Context = Backbone.Model.extend({
  238. },{
  239. table : 'Tcontext',
  240. schema: {
  241. id: { title:'id', type:'hidden', sqltype:'INTEGER', sqlconstraints:'PRIMARY KEY', autoincrement:true},
  242. fk_context: { title:'fk_context', type:'Text',sqltype:'NVARCHAR(500)', required: true},
  243. fk_object : { title:'fk_object',type:'Text', sqltype:'NVARCHAR(500)', required: true},
  244. context_type: { title:'context_type', type:'Text',sqltype:'NVARCHAR(500)', required: true},
  245. object_type : { title:'object_type',type:'Text', sqltype:'NVARCHAR(500)', required: true},
  246. },
  247. dao: app.dao.ContextDAO,
  248. });
  249. // The Context collection Collection
  250. app.models.ContextCollection =Backbone.Collection.extend({
  251. model : app.models.Context,
  252. });
  253. // -------------------------------------------------- The Data observation Models ---------------------------------------------------- //
  254. // The OccurenceDataValue Model
  255. app.models.OccurenceDataValue = Backbone.Model.extend({
  256. defaults: {
  257. milieu:'Mur',
  258. sended:0
  259. },
  260. },{
  261. table : 'TdataObs_occurences',
  262. schema: {
  263. id: { title:'id', type:'hidden', sqltype:'INTEGER', sqlconstraints:'PRIMARY KEY', autoincrement:true},
  264. latitude:{ type: 'hidden', title:'Latitude',sqltype:'REAL', required: true},
  265. longitude: { type: 'hidden', title:'Longitude',sqltype:'REAL', required: true} ,
  266. fk_taxon: { title:'fk_taxon', type:'Number', sqltype:'INTEGER' ,required: true},
  267. fk_rue: { title:'fk_rue', type:'hidden', sqltype:'INTEGER' ,required: true},
  268. sended: { title:'sended', type:'hidden', sqltype:'INTEGER' ,required: true},
  269. name_taxon: { title:'Espèce', type:'Text',sqltype:'NVARCHAR(500)', required: true},
  270. scientificName: { title:'Nom scientifique', type: 'Text',sqltype:'NVARCHAR(500)', required: true},
  271. milieu: { title:'Type de milieu', type: 'Select', sqltype:'NVARCHAR(500)', options: [{val:'Pelouse', label:'Pelouse'},{val:'Mur', label:'Mur'},{val:'Plate bande', label:'Plate bande'},{val:'Pied d\'arbre', label:'Pied d\'arbre'} ,{val:'Fissure', label:'Fissure'}, {val:'Haie', label:'Haie'}, {val: 'Chemin', label:'Chemin'}],required: true },
  272. datetime : { type: 'hidden', sqltype:'DATETIME' ,title:'datetime', required: true},
  273. photo: {
  274. title:'Photo', type:'Picture',sqltype:'NVARCHAR(500)',
  275. optCamera:{'quality': 50,'correctOrientation': false,'encodingType': 'navigator.camera.EncodingType.JPEG', 'source': 'navigator.camera.PictureSourceType.CAMERA', 'targetWidth': 200,'destinationType': 'navigator.camera.DestinationType.DATA_URL'}
  276. },
  277. note: { title:'Note', type:'Textarea',sqltype:'NVARCHAR(500)'},
  278. regionPaca: { title:'regionPaca', type:'hidden',sqltype:'INTEGER'}
  279. },
  280. dao: app.dao.OccurenceDataValueDAO,
  281. verboseName: 'Occurence'
  282. });
  283. // The OccurenceDataValues Collection
  284. app.models.OccurenceDataValuesCollection =Backbone.Collection.extend({
  285. model : app.models.OccurenceDataValue,
  286. getObsByRueId : function(fkrue) {
  287. return this.where({'fk_rue': fkrue});
  288. }
  289. });
  290. // The ParcoursDataValues Model
  291. app.models.ParcoursDataValue = Backbone.Model.extend({
  292. defaults: {
  293. cote:'Pair',
  294. state:6
  295. },
  296. },{
  297. table : 'TdataObs_parcours',
  298. schema: {
  299. //INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT
  300. id: { title:'id', type:'hidden', sqltype:'INTEGER', sqlconstraints:'PRIMARY KEY', autoincrement:true},
  301. name: { title:'Nom de la rue', type:'Text',required: true},
  302. cote: { title:'Le côté de la rue', type: 'Select', sqltype:'NVARCHAR(500)', options: [{val:'Pair', label:'Pair'}, {val:'Impair', label:'Impair'}, {val:'Les deux', label:'Les deux'}] },
  303. begin_latitude:{ type: 'hidden', sqltype:'REAL',title:'begin_latitude'},
  304. begin_longitude: { type: 'hidden',sqltype:'REAL', title:'begin_longitude'},
  305. begin_datetime : { type: 'hidden', sqltype:'DATETIME' ,title:'begin_datetime'},
  306. end_latitude:{ type: 'hidden', sqltype:'REAL',title:'end_latitude'},
  307. end_longitude: { type: 'hidden',sqltype:'REAL', title:'end_longitude'},
  308. end_datetime : { type: 'hidden', sqltype:'DATETIME' ,title:'end_datetime'},
  309. state: { title:'state', type:'hidden', sqltype:'INTEGER' ,required: true},
  310. },
  311. dao: app.dao.ParcoursDataValueDAO,
  312. verboseName: 'Parcours'
  313. });
  314. // The ParcoursDataValues Collection
  315. app.models.ParcoursDataValuesCollection =Backbone.Collection.extend({
  316. model : app.models.ParcoursDataValue,
  317. dao: app.dao.ParcoursDataValueDAO,
  318. initialize: function() {
  319. },
  320. });
  321. //position
  322. app.models.Position = Backbone.Model.extend({
  323. },{
  324. schema: {
  325. coords : { title:'object_type',type:'Text', sqltype:'NVARCHAR(500)'},
  326. },
  327. });