Guest User

Untitled

a guest
Oct 20th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.24 KB | None | 0 0
  1. (function(){
  2. 'use strict';
  3.  
  4. var config;
  5. var settingsDialog;
  6.  
  7. Office.initialize = function(reason){
  8. config = getConfig();
  9.  
  10. jQuery(document).ready(function(){
  11. // Check if add-in is configured
  12. if (config && config.gitHubUserName) {
  13. // If configured load the gist list
  14. loadGists(config.gitHubUserName);
  15. } else {
  16. // Not configured yet
  17. $('#not-configured').show();
  18. }
  19.  
  20. // When insert button is clicked, build the content
  21. // and insert into the body.
  22. $('#insert-button').on('click', function(){
  23. var gistId = $('.ms-ListItem.is-selected').children('.gist-id').val();
  24. getGist(gistId, function(gist, error) {
  25. if (gist) {
  26. buildBodyContent(gist, function (content, error) {
  27. if (content) {
  28. Office.context.mailbox.item.body.setSelectedDataAsync(content,
  29. {coercionType: Office.CoercionType.Html}, function(result) {
  30. if (result.status == 'failed') {
  31. showError('Could not insert Gist: ' + result.error.message);
  32. }
  33. });
  34. } else {
  35. showError('Could not create insertable content: ' + error);
  36. }
  37. });
  38. } else {
  39. showError('Could not retrieve Gist: ' + error);
  40. }
  41. });
  42. });
  43.  
  44. // When the settings icon is clicked, open the settings dialog
  45. $('#settings-icon').on('click', function(){
  46. // Display settings dialog
  47. var url = new URI('../settings/dialog.html').absoluteTo(window.location).toString();
  48. if (config) {
  49. // If the add-in has already been configured, pass the existing values
  50. // to the dialog
  51. url = url + '?gitHubUserName=' + config.gitHubUserName + '&defaultGistId=' + config.defaultGistId;
  52. }
  53.  
  54. var dialogOptions = { width: 20, height: 40, displayInIframe: true };
  55.  
  56. Office.context.ui.displayDialogAsync(url, dialogOptions, function(result) {
  57. settingsDialog = result.value;
  58. settingsDialog.addEventHandler(Microsoft.Office.WebExtension.EventType.DialogMessageReceived, receiveMessage);
  59. settingsDialog.addEventHandler(Microsoft.Office.WebExtension.EventType.DialogEventReceived, dialogClosed);
  60. });
  61. })
  62. });
  63. };
  64.  
  65. function loadGists(user) {
  66. $('#error-display').hide();
  67. $('#not-configured').hide();
  68. $('#gist-list-container').show();
  69.  
  70. getUserGists(user, function(gists, error) {
  71. if (error) {
  72.  
  73. } else {
  74. buildGistList($('#gist-list'), gists, onGistSelected);
  75. }
  76. });
  77. }
  78.  
  79. function onGistSelected() {
  80. $('.ms-ListItem').removeClass('is-selected');
  81. $(this).addClass('is-selected');
  82. $('#insert-button').removeAttr('disabled');
  83. }
  84.  
  85. function showError(error) {
  86. $('#not-configured').hide();
  87. $('#gist-list-container').hide();
  88. $('#error-display').text(error);
  89. $('#error-display').show();
  90. }
  91.  
  92. function receiveMessage(message) {
  93. config = JSON.parse(message.message);
  94. setConfig(config, function(result) {
  95. settingsDialog.close();
  96. settingsDialog = null;
  97. loadGists(config.gitHubUserName);
  98. });
  99. }
  100.  
  101. function dialogClosed(message) {
  102. settingsDialog = null;
  103. }
  104. })();
Add Comment
Please, Sign In to add comment