| 1 |
define([ |
| 2 |
"dojo", |
| 3 |
"dijit", |
| 4 |
"dijit/registry", |
| 5 |
"dijit/_base/popup", |
| 6 |
"dijit/_editor/_Plugin", |
| 7 |
"dijit/_editor/plugins/LinkDialog", |
| 8 |
"dijit/TooltipDialog", |
| 9 |
"dijit/form/_TextBoxMixin", |
| 10 |
"dijit/form/Button", |
| 11 |
"dijit/form/ValidationTextBox", |
| 12 |
"dijit/form/DropDownButton", |
| 13 |
"dojo/_base/connect", |
| 14 |
"dojo/_base/declare", |
| 15 |
"dojo/_base/sniff", |
| 16 |
"dojox/form/FileUploader", |
| 17 |
"dojo/i18n!dojox/editor/plugins/nls/LocalImage" |
| 18 |
], function(dojo, dijit, registry, popup, _Plugin, LinkDialog, TooltipDialog, |
| 19 |
_TextBoxMixin, Button, ValidationTextBox, DropDownButton, |
| 20 |
connect, declare, has, FileUploader, messages) { |
| 21 |
|
| 22 |
var LocalImage = dojo.declare("dojox.editor.plugins.LocalImage", LinkDialog.ImgLinkDialog, { |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
uploadable: false, |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
uploadUrl: "", |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
baseImageUrl: "", |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
fileMask: "*.jpg;*.jpeg;*.gif;*.png;*.bmp", |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
urlRegExp: "", |
| 63 |
|
| 64 |
|
| 65 |
htmlFieldName:"uploadedfile", |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
_isLocalFile: false, |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
_messages: "", |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
_cssPrefix: "dijitEditorEilDialog", |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
_closable: true, |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
linkDialogTemplate: [ |
| 89 |
"<div style='border-bottom: 1px solid black; padding-bottom: 2pt; margin-bottom: 4pt;'></div>", |
| 90 |
"<div class='dijitEditorEilDialogDescription'>${prePopuTextUrl}${prePopuTextBrowse}</div>", |
| 91 |
"<table role='presentation'><tr><td colspan='2'>", |
| 92 |
"<label for='${id}_urlInput' title='${prePopuTextUrl}${prePopuTextBrowse}'>${url}</label>", |
| 93 |
"</td></tr><tr><td class='dijitEditorEilDialogField'>", |
| 94 |
"<input dojoType='dijit.form.ValidationTextBox' class='dijitEditorEilDialogField'" + |
| 95 |
"regExp='${urlRegExp}' title='${prePopuTextUrl}${prePopuTextBrowse}' selectOnClick='true' required='true' " + |
| 96 |
"id='${id}_urlInput' name='urlInput' intermediateChanges='true' invalidMessage='${invalidMessage}' " + |
| 97 |
"prePopuText='<${prePopuTextUrl}${prePopuTextBrowse}>'>", |
| 98 |
"</td><td>", |
| 99 |
"<div id='${id}_browse' style='display:${uploadable}'>${browse}</div>", |
| 100 |
"</td></tr><tr><td colspan='2'>", |
| 101 |
"<label for='${id}_textInput'>${text}</label>", |
| 102 |
"</td></tr><tr><td>", |
| 103 |
"<input dojoType='dijit.form.TextBox' required='false' id='${id}_textInput' " + |
| 104 |
"name='textInput' intermediateChanges='true' selectOnClick='true' class='dijitEditorEilDialogField'>", |
| 105 |
"</td><td></td></tr><tr><td>", |
| 106 |
"</td><td>", |
| 107 |
"</td></tr><tr><td colspan='2'>", |
| 108 |
"<button dojoType='dijit.form.Button' id='${id}_setButton'>${set}</button>", |
| 109 |
"</td></tr></table>" |
| 110 |
].join(""), |
| 111 |
|
| 112 |
_initButton: function(){ |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
var _this = this; |
| 118 |
this._messages = messages; |
| 119 |
this.tag = "img"; |
| 120 |
var dropDown = (this.dropDown = new TooltipDialog({ |
| 121 |
title: messages[this.command + "Title"], |
| 122 |
onOpen: function(){ |
| 123 |
_this._initialFileUploader(); |
| 124 |
_this._onOpenDialog(); |
| 125 |
TooltipDialog.prototype.onOpen.apply(this, arguments); |
| 126 |
setTimeout(function(){ |
| 127 |
|
| 128 |
_TextBoxMixin.selectInputText(_this._urlInput.textbox); |
| 129 |
_this._urlInput.isLoadComplete = true; |
| 130 |
}, 0); |
| 131 |
}, |
| 132 |
onClose: function(){ |
| 133 |
dojo.disconnect(_this.blurHandler); |
| 134 |
_this.blurHandler = null; |
| 135 |
this.onHide(); |
| 136 |
}, |
| 137 |
onCancel: function(){ |
| 138 |
setTimeout(dojo.hitch(_this, "_onCloseDialog"),0); |
| 139 |
} |
| 140 |
})); |
| 141 |
|
| 142 |
var label = this.getLabel(this.command), |
| 143 |
className = this.iconClassPrefix + " " + this.iconClassPrefix + this.command.charAt(0).toUpperCase() + this.command.substr(1), |
| 144 |
props = dojo.mixin({ |
| 145 |
label: label, |
| 146 |
showLabel: false, |
| 147 |
iconClass: className, |
| 148 |
dropDown: this.dropDown, |
| 149 |
tabIndex: "-1" |
| 150 |
}, this.params || {}); |
| 151 |
|
| 152 |
if(!has('ie')){ |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
props.closeDropDown = function( focus){ |
| 158 |
if(_this._closable){ |
| 159 |
if(this._opened){ |
| 160 |
popup.close(this.dropDown); |
| 161 |
if(focus){ this.focus(); } |
| 162 |
this._opened = false; |
| 163 |
this.state = ""; |
| 164 |
} |
| 165 |
} |
| 166 |
setTimeout(function(){ _this._closable = true; }, 10); |
| 167 |
}; |
| 168 |
} |
| 169 |
|
| 170 |
this.button = new DropDownButton(props); |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
var masks = this.fileMask.split(";"), |
| 175 |
temp = ""; |
| 176 |
dojo.forEach(masks, function(m){ |
| 177 |
m = m.replace(/\./, "\\.").replace(/\*/g, ".*"); |
| 178 |
temp += "|" + m + "|" + m.toUpperCase(); |
| 179 |
}); |
| 180 |
messages.urlRegExp = this.urlRegExp = temp.substring(1); |
| 181 |
|
| 182 |
if(!this.uploadable){ |
| 183 |
messages.prePopuTextBrowse = "."; |
| 184 |
} |
| 185 |
|
| 186 |
messages.id = registry.getUniqueId(this.editor.id); |
| 187 |
messages.uploadable = this.uploadable ? "inline" : "none"; |
| 188 |
this._uniqueId = messages.id; |
| 189 |
this._setContent("<div class='" + this._cssPrefix + "Title'>" + dropDown.title + "</div>" + |
| 190 |
dojo.string.substitute(this.linkDialogTemplate, messages)); |
| 191 |
dropDown.startup(); |
| 192 |
|
| 193 |
var urlInput = (this._urlInput = registry.byId(this._uniqueId + "_urlInput")); |
| 194 |
this._textInput = registry.byId(this._uniqueId + "_textInput"); |
| 195 |
this._setButton = registry.byId(this._uniqueId + "_setButton"); |
| 196 |
|
| 197 |
if(urlInput){ |
| 198 |
var pt = ValidationTextBox.prototype; |
| 199 |
urlInput = dojo.mixin(urlInput, { |
| 200 |
|
| 201 |
isLoadComplete: false, |
| 202 |
isValid: function(isFocused){ |
| 203 |
if(this.isLoadComplete){ |
| 204 |
return pt.isValid.apply(this, arguments); |
| 205 |
}else{ |
| 206 |
return this.get("value").length > 0; |
| 207 |
} |
| 208 |
}, |
| 209 |
reset: function(){ |
| 210 |
this.isLoadComplete = false; |
| 211 |
pt.reset.apply(this, arguments); |
| 212 |
} |
| 213 |
}); |
| 214 |
|
| 215 |
this.connect(urlInput, "onKeyDown", "_cancelFileUpload"); |
| 216 |
this.connect(urlInput, "onChange", "_checkAndFixInput"); |
| 217 |
} |
| 218 |
if(this._setButton){ |
| 219 |
this.connect(this._setButton, "onClick", "_checkAndSetValue"); |
| 220 |
} |
| 221 |
this._connectTagEvents(); |
| 222 |
}, |
| 223 |
|
| 224 |
_initialFileUploader: function(){ |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
var fup = null, |
| 230 |
_this = this, |
| 231 |
widgetId = _this._uniqueId, |
| 232 |
fUpId = widgetId + "_browse", |
| 233 |
urlInput = _this._urlInput; |
| 234 |
|
| 235 |
if(_this.uploadable && !_this._fileUploader){ |
| 236 |
fup = _this._fileUploader = new FileUploader({ |
| 237 |
force: "html", |
| 238 |
uploadUrl: _this.uploadUrl, |
| 239 |
htmlFieldName: _this.htmlFieldName, |
| 240 |
uploadOnChange: false, |
| 241 |
selectMultipleFiles: false, |
| 242 |
showProgress: true |
| 243 |
}, fUpId); |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
fup.reset = function(){ |
| 248 |
_this._isLocalFile = false; |
| 249 |
fup._resetHTML(); |
| 250 |
}; |
| 251 |
|
| 252 |
_this.connect(fup, "onClick", function(){ |
| 253 |
urlInput.validate(false); |
| 254 |
if(!has('ie')){ |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
_this._closable = false; |
| 261 |
} |
| 262 |
}); |
| 263 |
|
| 264 |
|
| 265 |
_this.connect(fup, "onChange", function(data){ |
| 266 |
_this._isLocalFile = true; |
| 267 |
urlInput.set("value", data[0].name); |
| 268 |
urlInput.focus(); |
| 269 |
}); |
| 270 |
|
| 271 |
_this.connect(fup, "onComplete", function(data){ |
| 272 |
var urlPrefix = _this.baseImageUrl; |
| 273 |
urlPrefix = urlPrefix && urlPrefix.charAt(urlPrefix.length - 1) == "/" ? urlPrefix : urlPrefix + "/"; |
| 274 |
urlInput.set("value", urlPrefix + data[0].file); |
| 275 |
_this._isLocalFile = false; |
| 276 |
_this._setDialogStatus(true); |
| 277 |
_this.setValue(_this.dropDown.get("value")); |
| 278 |
}); |
| 279 |
|
| 280 |
_this.connect(fup, "onError", function(evtObject){ |
| 281 |
|
| 282 |
|
| 283 |
console.log("Error occurred when uploading image file!"); |
| 284 |
_this._setDialogStatus(true); |
| 285 |
}); |
| 286 |
} |
| 287 |
}, |
| 288 |
|
| 289 |
_checkAndFixInput: function(){ |
| 290 |
|
| 291 |
|
| 292 |
this._setButton.set("disabled", !this._isValid()); |
| 293 |
}, |
| 294 |
|
| 295 |
_isValid: function(){ |
| 296 |
|
| 297 |
|
| 298 |
return this._urlInput.isValid(); |
| 299 |
}, |
| 300 |
|
| 301 |
_cancelFileUpload: function(){ |
| 302 |
this._fileUploader.reset(); |
| 303 |
this._isLocalFile = false; |
| 304 |
}, |
| 305 |
|
| 306 |
_checkAndSetValue: function(){ |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
if(this._fileUploader && this._isLocalFile){ |
| 314 |
this._setDialogStatus(false); |
| 315 |
this._fileUploader.upload(); |
| 316 |
}else{ |
| 317 |
this.setValue(this.dropDown.get("value")); |
| 318 |
} |
| 319 |
}, |
| 320 |
|
| 321 |
_setDialogStatus: function( value){ |
| 322 |
this._urlInput.set("disabled", !value); |
| 323 |
this._textInput.set("disabled", !value); |
| 324 |
this._setButton.set("disabled", !value); |
| 325 |
}, |
| 326 |
|
| 327 |
destroy: function(){ |
| 328 |
|
| 329 |
|
| 330 |
this.inherited(arguments); |
| 331 |
if(this._fileUploader){ |
| 332 |
this._fileUploader.destroy(); |
| 333 |
delete this._fileUploader; |
| 334 |
} |
| 335 |
} |
| 336 |
}); |
| 337 |
|
| 338 |
var plugin = function(args){ |
| 339 |
return new LocalImage({ |
| 340 |
command: "insertImage", |
| 341 |
uploadable: ("uploadable" in args) ? args.uploadable : false, |
| 342 |
uploadUrl: ("uploadable" in args && "uploadUrl" in args) ? args.uploadUrl : "", |
| 343 |
htmlFieldName: ("uploadable" in args && "htmlFieldName" in args) ? args.htmlFieldName : "uploadedfile", |
| 344 |
baseImageUrl: ("uploadable" in args && "baseImageUrl" in args) ? args.baseImageUrl : "", |
| 345 |
fileMask: ("fileMask" in args) ? args.fileMask : "*.jpg;*.jpeg;*.gif;*.png;*.bmp" |
| 346 |
}); |
| 347 |
}; |
| 348 |
|
| 349 |
|
| 350 |
_Plugin.registry["LocalImage"] = plugin; |
| 351 |
_Plugin.registry["localImage"] = plugin; |
| 352 |
_Plugin.registry["localimage"] = plugin; |
| 353 |
|
| 354 |
return LocalImage; |
| 355 |
|
| 356 |
}); |