| 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 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
iconClassPrefix: "dijitAdditionalEditorIcon", |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
url: "", |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
logResults: true, |
| 34 |
|
| 35 |
_initButton: function(){ |
| 36 |
|
| 37 |
|
| 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 |
|
| 50 |
|
| 51 |
this.button.set("disabled", this.get("disabled")); |
| 52 |
}, |
| 53 |
|
| 54 |
setEditor: function(editor){ |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
this.editor = editor; |
| 60 |
this._initButton(); |
| 61 |
}, |
| 62 |
|
| 63 |
_save: function(){ |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
var content = this.editor.get("value"); |
| 69 |
this.save(content); |
| 70 |
}, |
| 71 |
|
| 72 |
save: function(content){ |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 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 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
this.button.set("disabled", false); |
| 110 |
if(this.logResults){ |
| 111 |
console.log(resp); |
| 112 |
} |
| 113 |
}, |
| 114 |
|
| 115 |
onError: function(error, ioargs){ |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
this.button.set("disabled", false); |
| 124 |
if(this.logResults){ |
| 125 |
console.log(error); |
| 126 |
} |
| 127 |
} |
| 128 |
}); |
| 129 |
|
| 130 |
|
| 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 |
}); |