plugin.jsx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. var Preact = require('preact'),
  2. h = require('preact').h,
  3. createClass = require('preact-compat').createClass,
  4. SupportedPlatforms = require('./supportedplatforms.jsx'),
  5. classNames = require('classnames'),
  6. ZeroClipboard = require('../js/lib/ZeroClipboard.js');
  7. var Plugin = createClass({
  8. shouldComponentUpdate: function(nextProps, nextState) {
  9. return this.props.plugin !== nextProps.plugin;
  10. },
  11. setClipboardText: function() {
  12. if(this.props.plugin && this.props.flashEnabled) {
  13. var client = new ZeroClipboard(document.getElementById("copy-" + this.props.plugin.name));
  14. var copyText = "cordova plugin add " + this.props.plugin.name;
  15. client.off();
  16. client.on("copy", function(event) {
  17. event.clipboardData.setData("text/plain", copyText);
  18. });
  19. }
  20. },
  21. copyTextWithoutFlash: function() {
  22. if(!this.props.flashEnabled) {
  23. var range = document.createRange();
  24. range.selectNode(this.getDOMNode().getElementsByClassName("cordova-add-command")[0]);
  25. var select = window.getSelection();
  26. select.removeAllRanges();
  27. select.addRange(range);
  28. try {
  29. document.execCommand("copy");
  30. } catch(e) {
  31. // Silently fail for now
  32. }
  33. select.removeAllRanges();
  34. }
  35. },
  36. render: function() {
  37. if(!this.props.plugin) {
  38. // Empty card with loading wheel
  39. return (
  40. <div className="container plugin-results-result">
  41. <div className="row">
  42. <div className="col-sm-9">
  43. <h2>Loading...</h2>
  44. </div>
  45. </div>
  46. </div>
  47. )
  48. }
  49. var license = this.props.plugin.license;
  50. if (license && license.length > 1) {
  51. license = license[0];
  52. }
  53. var downloadField;
  54. var copyIcon;
  55. var npmLink = 'https://www.npmjs.com/package/' + this.props.plugin.name;
  56. if(this.props.plugin.downloadCount) {
  57. var downloadCount = this.props.plugin.downloadCount.toLocaleString();
  58. downloadField = <p className="downloads"><strong>{downloadCount}</strong> downloads last month</p>;
  59. }
  60. if(this.props.plugin) {
  61. copyIcon = (
  62. <img
  63. id={"copy-" + this.props.plugin.name}
  64. className="plugins-copy-to-clipboard"
  65. src="{{ site.baseurl}}/static/img/copy-clipboard-icon.svg"
  66. title="Copy cordova plugin add command to clipboard"
  67. data-toggle="tooltip"
  68. data-placement="auto"
  69. onClick={this.copyTextWithoutFlash} />
  70. );
  71. }
  72. var classes = classNames({
  73. 'container': true,
  74. 'plugin-results-result': true,
  75. 'plugins-featured': this.props.plugin.isOfficial
  76. });
  77. return (
  78. <div className={classes}>
  79. <div className="row">
  80. <div className="col-sm-8 col-xs-8">
  81. <span>
  82. <h2><a href={npmLink} onClick={trackOutboundLink.bind(this, npmLink)} target="_blank">{this.props.plugin.name}</a></h2>
  83. <p className="version_and_author">v{this.props.plugin.version} by <strong>{this.props.plugin.author}</strong></p>
  84. </span>
  85. </div>
  86. <div className="col-sm-4 col-xs-4">
  87. {copyIcon}
  88. </div>
  89. </div>
  90. <div className="row">
  91. <div className="col-sm-8">
  92. <p>{this.props.plugin.description}</p>
  93. <SupportedPlatforms keywords={this.props.plugin.keywords} plugin={this.props.plugin.name}/>
  94. </div>
  95. <div className="col-sm-3 col-sm-offset-1">
  96. <hr className="visible-xs results-divider-line"/>
  97. <p className="license">{license}</p>
  98. {downloadField}
  99. <p className="last-updated">Last updated <strong>{this.props.plugin.modified} days ago</strong></p>
  100. </div>
  101. </div>
  102. <div className="cordova-add-command">
  103. {"cordova plugin add " + this.props.plugin.name}
  104. </div>
  105. </div>
  106. )
  107. },
  108. componentDidMount: function() {
  109. this.setClipboardText();
  110. if(this.props.plugin) {
  111. $(this.getDOMNode()).find(".plugins-copy-to-clipboard").tooltip();
  112. }
  113. },
  114. componentDidUpdate: function() {
  115. this.setClipboardText();
  116. }
  117. });
  118. function trackOutboundLink(url) {
  119. ga('send', 'event', 'outbound', 'click', url);
  120. }
  121. module.exports = Plugin;