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