PageRenderTime 49ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/static/scripts/mvc/citation/citation-view.js

https://bitbucket.org/hbc/galaxy-central-hbc/
JavaScript | 187 lines | 169 code | 12 blank | 6 comment | 38 complexity | 5ee35f158ed63d2ff07fcfeee6819340 MD5 | raw file
Possible License(s): CC-BY-3.0
  1. define([
  2. "mvc/base-mvc",
  3. "mvc/citation/citation-model",
  4. "utils/localization"
  5. ], function( baseMVC, citationModel, _l ){
  6. var CitationView = Backbone.View.extend({
  7. tagName: 'div',
  8. className: 'citations',
  9. render: function() {
  10. this.$el.append( "<p>" + this.formattedReference() + "</p>" );
  11. return this;
  12. },
  13. formattedReference: function() {
  14. var model = this.model;
  15. var entryType = model.entryType();
  16. var fields = model.fields();
  17. var ref = "";
  18. // Code inspired by...
  19. // https://github.com/vkaravir/bib-publication-list/blob/master/src/bib-publication-list.js
  20. var authorsAndYear = this._asSentence( (fields.author ? fields.author : "") + (fields.year ? (" (" + fields.year + ")") : "") ) + " ";
  21. var title = fields.title || "";
  22. var pages = fields.pages ? ("pp. " + fields.pages) : "";
  23. var address = fields.address;
  24. if( entryType == "article" ) {
  25. ref = authorsAndYear + this._asSentence(title) +
  26. (fields.journal ? ("In <em>" + fields.journal + ", ") : "") +
  27. (fields.volume ? fields.volume : "") +
  28. (fields.number ? ( "(" + fields.number + "), " ) : ", " ) +
  29. this._asSentence(pages) +
  30. this._asSentence(fields.address) +
  31. "<\/em>";
  32. } else if( entryType == "inproceedings" || entryType == "proceedings" ) {
  33. ref = authorsAndYear +
  34. this._asSentence(title) +
  35. (fields.booktitle ? ("In <em>" + fields.booktitle + ", ") : "") +
  36. (pages ? pages : "") +
  37. (address ? ", " + address : "") +
  38. ".<\/em>";
  39. } else if( entryType == "mastersthesis" || entryType == "phdthesis" ) {
  40. ref = authorsAndYear + this._asSentence(title) +
  41. (fields.howpublished ? fields.howpublished + ". " : "") +
  42. (fields.note ? fields.note + "." : "");
  43. } else if( entryType == "techreport" ) {
  44. ref = authorsAndYear + ". " + this._asSentence(title) +
  45. this._asSentence(fields.institution) +
  46. this._asSentence(fields.number) +
  47. this._asSentence(fields.type);
  48. } else if( entryType == "book" || entryType == "inbook" || entryType == "incollection" ) {
  49. ref = this._asSentence(authorsAndYear) + " " + this._formatBookInfo(fields);
  50. } else {
  51. ref = this._asSentence(authorsAndYear) + " " + this._asSentence(title) +
  52. this._asSentence(fields.howpublished) +
  53. this._asSentence(fields.note);
  54. }
  55. var doiUrl = "";
  56. if( fields.doi ) {
  57. doiUrl = 'http://dx.doi.org/' + fields.doi;
  58. ref += '[<a href="' + doiUrl + '">doi:' + fields.doi + "</a>]";
  59. }
  60. var url = fields.url || doiUrl;
  61. if( url ) {
  62. ref += '[<a href="' + url + '">Link</a>]';
  63. }
  64. return ref;
  65. },
  66. _formatBookInfo: function(fields) {
  67. var info = "";
  68. if( fields.chapter ) {
  69. info += fields.chapter + " in ";
  70. }
  71. if( fields.title ) {
  72. info += "<em>" + fields.title + "<\/em>";
  73. }
  74. if( fields.editor ) {
  75. info += ", Edited by " + fields.editor + ", ";
  76. }
  77. if( fields.publisher) {
  78. info += ", " + fields.publisher;
  79. }
  80. if( fields.pages ) {
  81. info += ", pp. " + fields.pages + "";
  82. }
  83. if( fields.series ) {
  84. info += ", <em>" + fields.series + "<\/em>";
  85. }
  86. if( fields.volume ) {
  87. info += ", Vol." + fields.volume;
  88. }
  89. if( fields.issn ) {
  90. info += ", ISBN: " + fields.issn;
  91. }
  92. return info + ".";
  93. },
  94. _asSentence: function(str) {
  95. return (str && str.trim()) ? str + ". " : "";
  96. }
  97. });
  98. var CitationListView = Backbone.View.extend({
  99. el: '#citations',
  100. /**
  101. * Set up view.
  102. */
  103. initialize: function() {
  104. this.listenTo( this.collection, 'add', this.renderCitation );
  105. },
  106. events: {
  107. 'click .citations-to-bibtex': 'showBibtex',
  108. 'click .citations-to-formatted': 'showFormatted'
  109. },
  110. renderCitation: function( citation ) {
  111. var citationView = new CitationView( { model: citation } );
  112. this.$(".citations-formatted").append( citationView.render().el );
  113. var rawTextarea = this.$(".citations-bibtex-text");
  114. rawTextarea.val( rawTextarea.val() + "\n\r" + citation.attributes.content );
  115. },
  116. render: function() {
  117. this.$el.html(this.citationsElement());
  118. this.collection.each(function( item ){
  119. this.renderCitation( item );
  120. }, this);
  121. this.showFormatted();
  122. },
  123. showBibtex: function() {
  124. this.$(".citations-to-formatted").show();
  125. this.$(".citations-to-bibtex").hide();
  126. this.$(".citations-bibtex").show();
  127. this.$(".citations-formatted").hide();
  128. this.$(".citations-bibtex-text").select();
  129. },
  130. showFormatted: function() {
  131. this.$(".citations-to-formatted").hide();
  132. this.$(".citations-to-bibtex").show();
  133. this.$(".citations-bibtex").hide();
  134. this.$(".citations-formatted").show();
  135. },
  136. partialWarningElement: function() {
  137. if( this.collection.partial ) {
  138. return [
  139. '<div style="padding:5px 10px">',
  140. '<b>Warning: This is a experimental feature.</b> Most Galaxy tools will not annotate',
  141. ' citations explicitly at this time. When writing up your analysis, please manually',
  142. ' review your histories and find all references',
  143. ' that should be cited in order to completely describe your work. Also, please remember to',
  144. ' <a href="https://wiki.galaxyproject.org/CitingGalaxy">cite Galaxy</a>.',
  145. '</div>',
  146. ].join('');
  147. } else {
  148. return '';
  149. }
  150. },
  151. citationsElement: function() {
  152. return [
  153. '<div class="toolForm">',
  154. '<div class="toolFormTitle">',
  155. _l("Citations"),
  156. ' <i class="fa fa-pencil-square-o citations-to-bibtex" title="Select all as BibTeX."></i>',
  157. ' <i class="fa fa-times citations-to-formatted" title="Return to formatted citation list."></i>',
  158. '</div>',
  159. '<div class="toolFormBody" style="padding:5px 10px">',
  160. this.partialWarningElement(),
  161. '<span class="citations-formatted"></span>',
  162. '</div>',
  163. '<div class="citations-bibtex toolFormBody" style="padding:5px 10px">',
  164. '<textarea style="width: 100%; height: 500px;" class="citations-bibtex-text"></textarea>',
  165. '</div>',
  166. '</div>'
  167. ].join( '' );
  168. }
  169. });
  170. //==============================================================================
  171. return {
  172. CitationView : CitationView,
  173. CitationListView : CitationListView
  174. };
  175. });