dataset-simple-create.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /** Component for creating a new dataset with a few simple options */
  2. define(
  3. function( require ) {
  4. var Backbone = require( "backbone" ),
  5. _ = require( "underscore" ),
  6. fui = require( "app/fui" ),
  7. DatasetSimpleCreateTpl = require( "plugins/text!app/templates/dataset-simple-create.tpl" );
  8. var DatasetSimpleCreate = Backbone.Marionette.ItemView.extend( {
  9. initialize: function() {
  10. _.bindAll( this, "onCommitSimple", "clearWarnings" );
  11. },
  12. template: _.template( DatasetSimpleCreateTpl ),
  13. ui: {
  14. },
  15. el: "#dataset-simple-create",
  16. events: {
  17. "click a.action.commit.simple": "onCommitSimple",
  18. "submit form": "onCommitSimple",
  19. "keydown input[name=dbName]": "clearWarnings"
  20. },
  21. templateHelpers: {
  22. },
  23. serializeData: function() {
  24. return this.model;
  25. },
  26. // event handlers
  27. onCommitSimple: function( e ) {
  28. e.preventDefault();
  29. if (this.validateSimpleForm()) {
  30. var datasetName = $("input[name=dbName]").val().trim();
  31. $("input[name=dbName]").val(datasetName);
  32. var options = $("#simple-edit form").serializeArray();
  33. fui.models.fusekiServer.updateOrCreateDataset( null, options )
  34. .done( this.showDataManagementPage )
  35. .fail( this.showFailureMessage );
  36. }
  37. },
  38. // onCommitUpload: function( e ) {
  39. // e.preventDefault();
  40. //
  41. // if (this.validateUploadForm()) {
  42. // $("#uploadForm").ajaxSubmit( {
  43. // success: this.showDataManagementPage,
  44. // error: this.showFailureMessage
  45. // });
  46. // }
  47. // },
  48. //
  49. showDataManagementPage: function( e ) {
  50. location = "?tab=datasets";
  51. },
  52. /** Todo: need to do a better job of responding to errors */
  53. showFailureMessage: function( jqXHR, textStatus, errorThrown ) {
  54. $(".errorOutput").html( sprintf( "<p class='has-error'>Sorry, that didn't work because:</p><pre>%s</pre>", errorThrown || textStatus ) );
  55. },
  56. /** Clear current warning states */
  57. clearWarnings: function() {
  58. this.clearValidation();
  59. $(".errorOutput").empty();
  60. },
  61. // validation
  62. validateSimpleForm: function() {
  63. this.clearValidation();
  64. if (! $("input[name=dbName]").val() || 0 === $("input[name=dbName]").val().trim().length) {
  65. $(".dbNameValidation").removeClass("hidden")
  66. .parents(".form-group" )
  67. .addClass( "has-error" );
  68. return false;
  69. }
  70. return true;
  71. },
  72. clearValidation: function() {
  73. $(".has-error").removeClass( "has-error" );
  74. $(".has-warning").removeClass( "has-warning" );
  75. }
  76. });
  77. return DatasetSimpleCreate;
  78. }
  79. );