/app/public/js/collections/photos_bla.js

https://bitbucket.org/dbezrukov/photoload · JavaScript · 142 lines · 112 code · 28 blank · 2 comment · 9 complexity · 400f753e259d6256567e7368afa998a1 MD5 · raw file

  1. define([
  2. "underscore",
  3. "underscore.string",
  4. 'backbone',
  5. 'models/photo'
  6. ], function(_, _str, Backbone, Photo) {
  7. var lastQuery;
  8. var offset = 0;
  9. var somePhotosLoaded = false;
  10. return Backbone.Collection.extend({
  11. model: Photo,
  12. url: '/photo/bla',
  13. initialize: function(models, options) {
  14. this.users = options.users;
  15. },
  16. doFetch: function(query){
  17. var self = this;
  18. query.offset = offset;
  19. query.count = somePhotosLoaded ? 20 : 75;
  20. var query_string = $.param(query);
  21. $.ajax({
  22. type: "GET",
  23. url: self.url + '/query',
  24. data: query_string,
  25. dataType: 'JSON',
  26. success: function(data){
  27. somePhotosLoaded = true;
  28. offset += query.count;
  29. self.trigger(data.length < query.count ? 'nomore' : 'hasmore');
  30. data.forEach(function(photo) {
  31. photo.src = self.url + '/' + photo.album + '/' + photo.id,
  32. photo.src_preview = self.url + '/' + photo.album + '/' + photo.id + '/preview',
  33. photo.time_string = formatDate(new Date(photo.utc * 1000));
  34. var user = self.users.get(photo.userId);
  35. photo.user_name = user ? user.get('name') : 'недоступно';
  36. var coordinates = app.account.get('usingPlaneCoordinates')
  37. ? _str.vsprintf('X: %s<br>Y: %s<br>', [ photo.x.toFixed(), photo.y.toFixed() ])
  38. : _str.vsprintf('Широта: %f<br>Долгота: %f<br>', [ photo.lat, photo.lon ]);
  39. var info = _str.vsprintf(
  40. 'Борт: %s<br>' +
  41. 'Загрузил: %s<br>' +
  42. 'Дата: %s<br><br>' +
  43. '%s' +
  44. 'Курс: %f°<br>' +
  45. 'Скорость: %f км/ч<br>' +
  46. 'Высота: %f м', [
  47. photo.album,
  48. photo.user_name,
  49. photo.time_string,
  50. coordinates,
  51. photo.hdg,
  52. photo.speed,
  53. photo.hgt
  54. ]
  55. )
  56. if (photo.roll && photo.pch && photo.yaw) {
  57. info += _str.vsprintf(
  58. '<br><br>' +
  59. 'Крен: %f°<br>' +
  60. 'Тангаж: %f°<br>' +
  61. 'Рыскание: %f°', [
  62. photo.roll,
  63. photo.pch,
  64. photo.yaw
  65. ])
  66. }
  67. if (photo.cameraF && photo.cameraWidth && photo.cameraHeight) {
  68. info += _str.vsprintf(
  69. '<br><br>' +
  70. 'Фокусное расстояние: %f мм<br>' +
  71. 'Матрица: %s мм', [
  72. photo.cameraF,
  73. round(photo.cameraWidth, 1) + 'x' + round(photo.cameraHeight, 1)
  74. ])
  75. }
  76. // plain text
  77. photo.info = info;
  78. // html-formatted text
  79. photo.title = photo.info.replace(/<br>/g, '\n');
  80. self.add(photo);
  81. });
  82. },
  83. error: function(xhr){
  84. }
  85. });
  86. function formatDate(date) {
  87. return (date.toLocaleDateString() + ' ' + date.toLocaleTimeString());
  88. }
  89. function round(value, places) {
  90. if (!value) {
  91. return 0;
  92. }
  93. var newVal = Math.round(value*Math.pow(10, places)) / Math.pow(10, places);
  94. return parseFloat(newVal);
  95. }
  96. },
  97. fetch: function(query_object){
  98. var self = this;
  99. offset = 0;
  100. somePhotosLoaded = false;
  101. if (query_object) {
  102. lastQuery = query_object;
  103. }
  104. self.reset();
  105. self.doFetch(lastQuery);
  106. },
  107. fetchMore: function(){
  108. var self = this;
  109. if (lastQuery) {
  110. self.doFetch(lastQuery);
  111. }
  112. }
  113. });
  114. });