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