tweetabletext.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. CKEDITOR.dialog.add('tweetabletextDialog', function(editor) {
  2. return {
  3. title: 'TweetableText Properties',
  4. minWidth: 400,
  5. minHeight: 200,
  6. contents: [
  7. {
  8. id: 'tab-basic',
  9. label: 'Basic Settings',
  10. elements: [
  11. {
  12. type: 'text',
  13. id: 'displaytext',
  14. label: 'Display Text',
  15. validate: CKEDITOR.dialog.validate.notEmpty('Display Text field can not be empty.'),
  16. setup: function(element) {
  17. this.setValue(element.getText());
  18. },
  19. commit: function(element) {
  20. element.setText(this.getValue());
  21. }
  22. },
  23. {
  24. type: 'text',
  25. id: 'tweetabletext',
  26. label: 'Tweetable Text',
  27. validate: CKEDITOR.dialog.validate.notEmpty('Tweetable Text field can not be empty.'),
  28. setup: function(element) {
  29. var splitURL = element.getAttribute('href').split("?");
  30. var dataTweet = splitURL[1].split("=");
  31. var decodedDataTweet = decodeURI(dataTweet[1]);
  32. this.setValue(decodedDataTweet);
  33. },
  34. commit: function(element) {
  35. var twitterBaseUrl = 'http://twitter.com/intent/tweet?';
  36. var encodedValue = encodeURI(this.getValue());
  37. twitterBaseUrl += 'text=' + encodedValue;
  38. element.setAttribute('href', twitterBaseUrl);
  39. // For some reason, without doing this the changes won't be reflected on frontend.
  40. element.setAttribute('data-cke-saved-href', twitterBaseUrl);
  41. }
  42. }
  43. ]
  44. }
  45. ],
  46. onShow: function() {
  47. var selection = editor.getSelection();
  48. var element = selection.getStartElement();
  49. if (element.getAttribute('class') !== 'tweetabletext') {
  50. element = editor.document.createElement('a');
  51. this.insertMode = true;
  52. }
  53. else {
  54. this.insertMode = false;
  55. }
  56. this.element = element;
  57. if (!this.insertMode) {
  58. this.setupContent(this.element);
  59. }
  60. },
  61. onOk: function() {
  62. var dialog = this;
  63. var retrieveElement = this.element;
  64. var getDisplayText = dialog.getValueOf('tab-basic', 'displaytext');
  65. var getTweetableText = dialog.getValueOf('tab-basic', 'tweetabletext');
  66. var getTweetableText = encodeURI(getTweetableText);
  67. var twitterBaseUrl = 'http://twitter.com/intent/tweet?';
  68. var tweetabletext = editor.document.createElement('a');
  69. tweetabletext.setAttribute('class', 'tweetabletext');
  70. twitterBaseUrl += 'text=' + getTweetableText;
  71. tweetabletext.setAttribute('href', twitterBaseUrl);
  72. tweetabletext.setText(getDisplayText);
  73. this.commitContent(retrieveElement);
  74. if (this.insertMode) {
  75. editor.insertElement(tweetabletext);
  76. }
  77. }
  78. };
  79. });