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

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

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