Save.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. define("dojox/editor/plugins/Save", [
  2. "dojo",
  3. "dijit",
  4. "dojox",
  5. "dijit/form/Button",
  6. "dijit/_editor/_Plugin",
  7. "dojo/_base/connect",
  8. "dojo/_base/declare",
  9. "dojo/i18n",
  10. "dojo/i18n!dojox/editor/plugins/nls/Save"
  11. ], function(dojo, dijit, dojox) {
  12. dojo.declare("dojox.editor.plugins.Save",dijit._editor._Plugin,{
  13. // summary:
  14. // This plugin provides Save cabability to the editor. When
  15. // clicked, the document in the editor frame will be osted to the URL
  16. // provided, or none, if none provided. Users who desire a different save
  17. // function can extend this plugin (via dojo.extend) and over-ride the
  18. // save method while save is in process, the save button is disabled.
  19. // iconClassPrefix: [const] String
  20. // The CSS class name for the button node is formed from `iconClassPrefix`
  21. // and `command`
  22. iconClassPrefix: "dijitAdditionalEditorIcon",
  23. // url [public] String
  24. // The URL to POST the content back to. Used by the save function.
  25. url: "",
  26. // logErrors [public] boolean
  27. // Boolean flag to indicate that the default action for save and
  28. // error handlers is to just log to console. Default is true.
  29. logResults: true,
  30. _initButton: function(){
  31. // summary:
  32. // Over-ride for creation of the save button.
  33. var strings = dojo.i18n.getLocalization("dojox.editor.plugins", "Save");
  34. this.button = new dijit.form.Button({
  35. label: strings["save"],
  36. showLabel: false,
  37. iconClass: this.iconClassPrefix + " " + this.iconClassPrefix + "Save",
  38. tabIndex: "-1",
  39. onClick: dojo.hitch(this, "_save")
  40. });
  41. },
  42. updateState: function(){
  43. // summary:
  44. // Over-ride for button state control for disabled to work.
  45. this.button.set("disabled", this.get("disabled"));
  46. },
  47. setEditor: function(editor){
  48. // summary:
  49. // Over-ride for the setting of the editor.
  50. // editor: Object
  51. // The editor to configure for this plugin to use.
  52. this.editor = editor;
  53. this._initButton();
  54. },
  55. _save: function(){
  56. // summary:
  57. // Function to trigger saving of the editor document
  58. // tags:
  59. // private
  60. var content = this.editor.get("value");
  61. this.save(content);
  62. },
  63. save: function(content){
  64. // summary:
  65. // User over-ridable save function for the editor content.
  66. // Please note that the service URL provided should do content
  67. // filtering of the posted content to avoid XSS injection via
  68. // the data from the editor.
  69. // tags:
  70. // public
  71. // Set the default header to post as a body of text/html.
  72. var headers = {
  73. "Content-Type": "text/html"
  74. };
  75. if(this.url){
  76. var postArgs = {
  77. url: this.url,
  78. postData: content,
  79. headers: headers,
  80. handleAs: "text"
  81. };
  82. this.button.set("disabled", true);
  83. var deferred = dojo.xhrPost(postArgs);
  84. deferred.addCallback(dojo.hitch(this, this.onSuccess));
  85. deferred.addErrback(dojo.hitch(this, this.onError));
  86. }else{
  87. console.log("No URL provided, no post-back of content: " + content);
  88. }
  89. },
  90. onSuccess: function(resp, ioargs){
  91. // summary:
  92. // User over-ridable save success function for editor content.
  93. // Be sure to call this.inherited(arguments) if over-riding this method.
  94. // resp:
  95. // The response from the server, if any, in text format.
  96. // tags:
  97. // public
  98. this.button.set("disabled", false);
  99. if(this.logResults){
  100. console.log(resp);
  101. }
  102. },
  103. onError: function(error, ioargs){
  104. // summary:
  105. // User over-ridable save success function for editor content.
  106. // Be sure to call this.inherited(arguments) if over-riding this method.
  107. // resp:
  108. // The response from the server, if any, in text format.
  109. // tags:
  110. // public
  111. this.button.set("disabled", false);
  112. if(this.logResults){
  113. console.log(error);
  114. }
  115. }
  116. });
  117. // Register this plugin.
  118. dojo.subscribe(dijit._scopeName + ".Editor.getPlugin",null,function(o){
  119. if(o.plugin){ return; }
  120. var name = o.args.name.toLowerCase();
  121. if(name === "save"){
  122. o.plugin = new dojox.editor.plugins.Save({
  123. url: ("url" in o.args)?o.args.url:"",
  124. logResults: ("logResults" in o.args)?o.args.logResults:true
  125. });
  126. }
  127. });
  128. return dojox.editor.plugins.Save;
  129. });