Save.js 4.4 KB

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