/Backup/Src/Bowerbird.Website/js/bowerbird/views/sightingIdentificationFormView.js

https://github.com/Bowerbird/bowerbird-web · JavaScript · 170 lines · 126 code · 32 blank · 12 comment · 11 complexity · c1b249782dc362fcd63de6080e99ba9c MD5 · raw file

  1. /// <reference path="../../libs/log.js" />
  2. /// <reference path="../../libs/require/require.js" />
  3. /// <reference path="../../libs/jquery/jquery-1.7.2.js" />
  4. /// <reference path="../../libs/underscore/underscore.js" />
  5. /// <reference path="../../libs/backbone/backbone.js" />
  6. /// <reference path="../../libs/backbone.marionette/backbone.marionette.js" />
  7. // SightingIdentificationFormView
  8. // ------------------------------
  9. define(['jquery', 'underscore', 'backbone', 'app', 'ich', 'views/sightingdetailsview', 'views/identificationformview', 'views/sightingidentificationsubformview', 'moment', 'datepicker', 'multiselect', 'jqueryui/dialog', 'tipsy', 'tagging'],
  10. function ($, _, Backbone, app, ich, SightingDetailsView, IdentificationFormView, SightingIdentificationSubFormView, moment) {
  11. var SightingIdentificationFormView = Backbone.Marionette.Layout.extend({
  12. viewType: 'form',
  13. className: 'form single sighting-identification-form',
  14. template: 'SightingIdentificationForm',
  15. regions: {
  16. sightingSection: '.sighting',
  17. sightingIdentificationSection: '.sighting-identification-fieldset'
  18. },
  19. events: {
  20. 'click #cancel': '_cancel',
  21. 'click #save': '_save'
  22. },
  23. initialize: function (options) {
  24. this.categorySelectList = options.categorySelectList;
  25. this.categories = options.categories;
  26. this.sighting = options.sighting;
  27. this.model.on('validated', this.onValidation, this);
  28. },
  29. serializeData: function () {
  30. return {
  31. Model: {
  32. Sighting: this.sighting.toJSON(),
  33. Identification: this.model.toJSON()
  34. }
  35. };
  36. },
  37. onShow: function () {
  38. var sightingView = new SightingDetailsView({ model: this.sighting, className: 'observation-details', template: 'SightingFullDetails' });
  39. this.sightingView = sightingView;
  40. this.sightingSection.show(sightingView);
  41. var sightingIdentificationSubFormView = new SightingIdentificationSubFormView({ el: this.$el.find('.sighting-identification-fieldset'), model: this.model, categorySelectList: this.categorySelectList, categories: this.categories });
  42. this.sightingIdentificationSection.attachView(sightingIdentificationSubFormView);
  43. sightingIdentificationSubFormView.showBootstrappedDetails();
  44. this._showDetails();
  45. },
  46. showBootstrappedDetails: function () {
  47. this.initializeRegions();
  48. var sightingView = new SightingDetailsView({ el: this.$el.find('.observation-details'), model: this.sighting, template: 'SightingFullDetails' });
  49. this.sightingView = sightingView;
  50. this.sightingSection.attachView(sightingView);
  51. sightingView.showBootstrappedDetails();
  52. var sightingIdentificationSubFormView = new SightingIdentificationSubFormView({ el: this.$el.find('.sighting-identification-fieldset'), model: this.model, categorySelectList: this.categorySelectList, categories: this.categories });
  53. this.sightingIdentificationSection.attachView(sightingIdentificationSubFormView);
  54. sightingIdentificationSubFormView.showBootstrappedDetails();
  55. this._showDetails();
  56. },
  57. _showDetails: function () {
  58. app.vent.on('view:render:complete', function () {
  59. this.sightingView.refresh();
  60. }, this);
  61. },
  62. onValidation: function (obs, errors) {
  63. if (errors.length == 0) {
  64. this.$el.find('.validation-summary').slideUp(function () { $(this).remove(); });
  65. }
  66. if (errors.length > 0) {
  67. if (this.$el.find('.validation-summary').length == 0) {
  68. this.$el.find('form').prepend(ich.ValidationSummary({
  69. SummaryMessage: 'Please correct the following before continuing:',
  70. Errors: errors,
  71. // Due to a bug in mustache.js where you can't reference a parent element in a string array loop, we have to build the HTML here
  72. Error: function () {
  73. return _.map(this.Messages, function (message) {
  74. return '<li class="validation-field-' + this.Field + '">' + message + '</li>';
  75. }, this).join('\n');
  76. }
  77. }));
  78. this.$el.find('.validation-summary').slideDown();
  79. } else {
  80. var that = this;
  81. // Remove items that are now valid
  82. this.$el.find('.validation-summary li').each(function () {
  83. var $li = that.$el.find(this);
  84. var found = _.find(errors, function (err) {
  85. return _.find(err.Messages, function (message) {
  86. return 'validation-field-' + err.Field === $li.attr('class') && message === $li.text();
  87. });
  88. });
  89. if (!found) {
  90. $li.slideUp(function () { $(this).remove(); });
  91. }
  92. });
  93. // Add items
  94. var lis = this.$el.find('.validation-summary li');
  95. _.each(errors, function (err) {
  96. _.each(err.Messages, function (message) {
  97. // Only add if the class and text is not found in li list
  98. var found = _.find(lis, function (li) {
  99. var $li = $(li);
  100. return $li.attr('class') === 'validation-field-' + err.Field && $li.text() === message;
  101. });
  102. if (!found) {
  103. var linew = $('<li class="validation-field-' + err.Field + '">' + message + '</li>').css({ display: 'none' });
  104. that.$el.find('.validation-summary ul').append(linew);
  105. linew.slideDown();
  106. }
  107. });
  108. }, this);
  109. }
  110. }
  111. this.$el.find('#Identification').removeClass('input-validation-error');
  112. if (_.any(errors, function (item) { return item.Field === 'IsCustomIdentification'; })) {
  113. this.$el.find('#Identification').addClass('input-validation-error');
  114. }
  115. },
  116. _cancel: function () {
  117. app.showPreviousContentView();
  118. },
  119. _save: function () {
  120. this.$el.find('#save').attr('disabled', 'disabled').val('Saving...');
  121. var that = this;
  122. this.model.save(null, {
  123. success: function (model, response, options) {
  124. that.$el.find('#save').attr('disabled', 'disabled').val('Saved');
  125. that.onValidation(that.model, []);
  126. app.showPreviousContentView();
  127. },
  128. error: function (model, xhr, options) {
  129. that.$el.find('#save').removeAttr('disabled').val('Save');
  130. var data = JSON.parse(xhr.responseText);
  131. that.onValidation(that.model, data.Model.Errors);
  132. $('html, body').animate({ scrollTop: 0 }, 'slow');
  133. }
  134. });
  135. }
  136. });
  137. return SightingIdentificationFormView;
  138. });