PageRenderTime 64ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/static_dev/js/hitchgraph/models.js

https://bitbucket.org/alistril/kamcatka
JavaScript | 370 lines | 323 code | 32 blank | 15 comment | 20 complexity | 4a3c23578007ae6a0469ff4a88143449 MD5 | raw file
  1. define([
  2. 'underscore',
  3. 'backbone',
  4. 'gmaps',
  5. 'moment',
  6. 'hitchgraph/app',
  7. 'hitchgraph/constants',
  8. 'backbone.relational'
  9. ], function(_, Backbone, gmaps, moment, app,constants){
  10. var models = {};
  11. ////////////////////////////////////////////////////////////////////////////////////////////
  12. ////////////////////////////////////////// Models //////////////////////////////////////////
  13. ////////////////////////////////////////////////////////////////////////////////////////////
  14. models.UserProfile = Backbone.RelationalModel.extend({
  15. initialize: function(data){
  16. if(_(data).has("location") && _(data.location).has("coordinates")){
  17. this.set(
  18. "location",
  19. new gmaps.LatLng(
  20. data.location.coordinates[1],
  21. data.location.coordinates[0]
  22. )
  23. );
  24. }
  25. if(_(data).has("date_met")){
  26. this.set("date_met",moment(data.datetime));
  27. }
  28. }
  29. });
  30. models.CoordinateDescription = Backbone.RelationalModel.extend({
  31. urlRoot : "/expeditions/res/coordinate_descriptions",
  32. relations: [
  33. {
  34. type: Backbone.HasOne,
  35. key: 'userprofile',
  36. relatedModel: models.UserProfile,
  37. includeInJSON: true,
  38. reverseRelation: {
  39. key: 'coordinatedescription_set',
  40. includeInJSON: 'id'
  41. },
  42. }
  43. ],
  44. patch : function(){
  45. return this.save({
  46. "comments" : this.get("comments"),
  47. "picture" : this.get("picture")
  48. },{patch:true});
  49. },
  50. initialize: function(){
  51. },
  52. has_details : function(){
  53. return ( this.has("comments") && this.get("comments").length ) || ( this.has("picture") && this.get("picture").length ) ;
  54. }
  55. });
  56. models.CoordinateDescriptionCollection = Backbone.Collection.extend({
  57. model: models.CoordinateDescription,
  58. initialize: function(){
  59. }
  60. });
  61. models.Coordinate = Backbone.RelationalModel.extend({
  62. urlRoot : "/expeditions/res/coordinates",
  63. url : function(){
  64. return this.urlRoot + "/" + this.id + "/";
  65. },
  66. defaults: {
  67. "draggable": false
  68. },
  69. relations: [
  70. {
  71. type: Backbone.HasMany,
  72. key: 'coordinatedescription_set',
  73. relatedModel: models.CoordinateDescription,
  74. includeInJSON: true,
  75. reverseRelation: {
  76. key: 'coordinate',
  77. includeInJSON: 'id'
  78. },
  79. }
  80. ],
  81. parse : function(response, options){
  82. ret = Backbone.RelationalModel.prototype.parse.call(this,response, options);
  83. if(ret.location){
  84. ret.location = new gmaps.LatLng(
  85. ret.location.coordinates[1],
  86. ret.location.coordinates[0]
  87. );
  88. }
  89. if(ret.datetime){
  90. ret.datetime = moment(ret.datetime);
  91. }
  92. return ret;
  93. },
  94. toJSON : function(){
  95. data = Backbone.RelationalModel.prototype.toJSON.call(this);
  96. //convert to geojson instead of default serializition
  97. if (data.location){
  98. data.location = {
  99. "type": "Point",
  100. "coordinates": [
  101. this.get("location").lng(),
  102. this.get("location").lat()
  103. ]
  104. };
  105. }
  106. if(data.datetime){
  107. data.datetime = this.get("datetime").format();
  108. }
  109. return data;
  110. },
  111. initialize: function(data){
  112. if(!data) return;
  113. if(_(data).has("location") && _(data.location).has("coordinates")){
  114. this.set(
  115. "location",
  116. new gmaps.LatLng(
  117. data.location.coordinates[1],
  118. data.location.coordinates[0]
  119. )
  120. );
  121. }
  122. if(_(data).has("datetime")){
  123. this.set("datetime",moment(data.datetime));
  124. }
  125. }/*,
  126. save : function(attrs, options){
  127. options || (options = {});
  128. // Get data
  129. options.data = JSON.stringify(attrs);
  130. // Filter the data to send to the server
  131. delete options.geometry;
  132. delete options.draggable;
  133. Backbone.Model.prototype.save.call(this, attrs, options);
  134. }*/,
  135. has_details : function(){
  136. var ret = false;
  137. if(this.has("coordinatedescription_set")){
  138. this.get("coordinatedescription_set").each(function(description){
  139. ret = ret || description.has_details();
  140. });
  141. }
  142. return ret;
  143. },
  144. select : function(selected){
  145. this.get("hitchgraph").selectCoordinate(this);
  146. },
  147. is_selected : function(){
  148. if(!this.has("hitchgraph"))
  149. return false;
  150. return (this == this.get("hitchgraph").coordinate_selected);
  151. },
  152. is_ownable : function(username){
  153. if(this.has("owners")){
  154. return _(this.get("owners")).indexOf(username)>-1;
  155. }else
  156. return true;
  157. }
  158. });
  159. models.CoordinateCollection = Backbone.Collection.extend({
  160. model: models.Coordinate,
  161. initialize: function(){
  162. }
  163. });
  164. models.Displacement = Backbone.RelationalModel.extend({
  165. relations: [
  166. {
  167. type: Backbone.HasOne,
  168. key: 'source',
  169. relatedModel: models.Coordinate,
  170. reverseRelation: {
  171. type: Backbone.HasOne,
  172. key: 'displacement_source_related',
  173. includeInJSON: 'id'
  174. },
  175. },
  176. {
  177. type: Backbone.HasOne,
  178. key: 'destination',
  179. relatedModel: models.Coordinate,
  180. reverseRelation: {
  181. type: Backbone.HasOne,
  182. key: 'displacement_destination_related',
  183. includeInJSON: 'id'
  184. },
  185. }
  186. ],
  187. initialize: function(){
  188. }
  189. });
  190. models.DisplacementExtreme = Backbone.RelationalModel.extend({
  191. relations: [
  192. {
  193. type: Backbone.HasOne,
  194. key: 'source',
  195. relatedModel: models.Coordinate,
  196. reverseRelation: {
  197. type: Backbone.HasOne,
  198. key: 'extreme_source_related',
  199. includeInJSON: 'id'
  200. },
  201. },
  202. {
  203. type: Backbone.HasOne,
  204. key: 'destination',
  205. relatedModel: models.Coordinate,
  206. reverseRelation: {
  207. type: Backbone.HasOne,
  208. key: 'extreme_destination_related',
  209. includeInJSON: 'id'
  210. },
  211. }
  212. ],
  213. initialize: function(){
  214. }
  215. });
  216. models.DisplacementCollection = Backbone.Collection.extend({
  217. model: models.Displacement,
  218. initialize: function(){
  219. }
  220. });
  221. models.HitchGraph = Backbone.RelationalModel.extend({
  222. urlRoot : "/expeditions/res/hitchgraphs/",
  223. relations: [
  224. {
  225. type: Backbone.HasMany,
  226. key: 'displacement_set',
  227. relatedModel: models.Displacement,
  228. reverseRelation: {
  229. key: 'displacement_of_hitchgraph',
  230. includeInJSON: 'id'
  231. },
  232. },
  233. {
  234. type: Backbone.HasMany,
  235. key: 'coordinate_set',
  236. relatedModel: models.Coordinate,
  237. reverseRelation: {
  238. key: 'hitchgraph',
  239. includeInJSON: 'id'
  240. },
  241. },
  242. {
  243. type: Backbone.HasMany,
  244. key: 'userprofile_set',
  245. relatedModel: models.UserProfile,
  246. reverseRelation: {
  247. key: 'userprofile_of_hitchgraph',
  248. includeInJSON: 'id'
  249. },
  250. },
  251. {
  252. type: Backbone.HasOne,
  253. key: 'start',
  254. relatedModel: models.Coordinate,
  255. reverseRelation: false
  256. },
  257. {
  258. type: Backbone.HasOne,
  259. key: 'finish',
  260. relatedModel: models.Coordinate,
  261. reverseRelation: false
  262. },
  263. {
  264. type: Backbone.HasMany,
  265. key: 'start_set',
  266. relatedModel: models.Coordinate,
  267. reverseRelation: {
  268. key: 'is_start_of',
  269. includeInJSON: 'id'
  270. }
  271. },
  272. {
  273. type: Backbone.HasMany,
  274. key: 'finish_set',
  275. relatedModel: models.Coordinate,
  276. reverseRelation: {
  277. key: 'is_finish_of',
  278. includeInJSON: 'id'
  279. }
  280. },
  281. {
  282. type: Backbone.HasMany,
  283. key: 'coordinate_before_user_set',
  284. relatedModel: models.Coordinate,
  285. reverseRelation: {
  286. key: 'before_start_of',
  287. includeInJSON: 'id'
  288. }
  289. },
  290. {
  291. type: Backbone.HasMany,
  292. key: 'coordinate_after_user_set',
  293. relatedModel: models.Coordinate,
  294. reverseRelation: {
  295. key: 'after_finish_of',
  296. includeInJSON: 'id'
  297. }
  298. },
  299. {
  300. type: Backbone.HasMany,
  301. key: 'displacement_before_user_set',
  302. relatedModel: models.DisplacementExtreme,
  303. reverseRelation: {
  304. key: 'displacement_before_start_of',
  305. includeInJSON: 'id'
  306. }
  307. },
  308. {
  309. type: Backbone.HasMany,
  310. key: 'displacement_after_user_set',
  311. relatedModel: models.DisplacementExtreme,
  312. reverseRelation: {
  313. key: 'displacement_after_finish_of',
  314. includeInJSON: 'id'
  315. }
  316. }/**/
  317. ],
  318. selectCoordinate : function(coordinate){
  319. if(this.coordinate_selected)
  320. this.coordinate_selected.trigger("selected", this, false);
  321. this.coordinate_selected = coordinate;
  322. this.coordinate_selected.trigger("selected", this, true);
  323. this.trigger("coordinate_selected",this.coordinate_selected);
  324. },
  325. initialize: function(){
  326. }
  327. });
  328. models.HitchGraphCollection = Backbone.Collection.extend({
  329. url : "/expeditions/res/hitchgraphs/",
  330. model: models.HitchGraph,
  331. initialize: function(){
  332. }
  333. });
  334. return models;
  335. }); //define