PageRenderTime 19ms CodeModel.GetById 6ms app.highlight 9ms RepoModel.GetById 2ms app.codeStats 0ms

/source/Plug-in/kind/plugins/insertfile/insertfile.js

http://prosporous.googlecode.com/
JavaScript | 129 lines | 115 code | 4 blank | 10 comment | 9 complexity | a8273780b78d5de5a8936ba45a41018a MD5 | raw file
  1/*******************************************************************************
  2* KindEditor - WYSIWYG HTML Editor for Internet
  3* Copyright (C) 2006-2011 kindsoft.net
  4*
  5* @author Roddy <luolonghao@gmail.com>
  6* @site http://www.kindsoft.net/
  7* @licence http://www.kindsoft.net/license.php
  8*******************************************************************************/
  9
 10KindEditor.plugin('insertfile', function(K) {
 11	var self = this, name = 'insertfile',
 12		allowFileManager = K.undef(self.allowFileManager, false),
 13		uploadJson = K.undef(self.uploadJson, self.basePath + 'php/upload_json.php'),
 14		lang = self.lang(name + '.');
 15	
 16	self.plugin.fileDialog = function(options) {
 17		var fileUrl = K.undef(options.fileUrl, 'http://'),
 18			fileTitle = K.undef(options.fileTitle, ''),
 19			clickFn = options.clickFn;
 20		var html = [
 21			'<div style="padding:10px 20px;">',
 22			'<div class="ke-dialog-row">',
 23			'<label for="keUrl" style="width:60px;">' + lang.url + '</label>',
 24			'<input type="text" id="keUrl" name="url" class="ke-input-text" style="width:160px;" /> &nbsp;',
 25			'<input type="button" class="ke-upload-button" value="' + lang.upload + '" /> &nbsp;',
 26			'<span class="ke-button-common ke-button-outer">',
 27			'<input type="button" class="ke-button-common ke-button" name="viewServer" value="' + lang.viewServer + '" />',
 28			'</span>',
 29			'</div>',
 30			//title
 31			'<div class="ke-dialog-row">',
 32			'<label for="keTitle" style="width:60px;">' + lang.title + '</label>',
 33			'<input type="text" id="keTitle" class="ke-input-text" name="title" value="" style="width:160px;" /></div>',
 34			'</div>',
 35			//form end
 36			'</form>',
 37			'</div>'
 38			].join('');
 39		var dialog = self.createDialog({
 40			name : name,
 41			width : 450,
 42			height : 180,
 43			title : self.lang(name),
 44			body : html,
 45			yesBtn : {
 46				name : self.lang('yes'),
 47				click : function(e) {
 48					var url = K.trim(urlBox.val()),
 49						title = titleBox.val();
 50					if (url == 'http://' || K.invalidUrl(url)) {
 51						alert(self.lang('invalidUrl'));
 52						urlBox[0].focus();
 53						return;
 54					}
 55					if (K.trim(title) === '') {
 56						title = url;
 57					}
 58					clickFn.call(self, url, title);
 59				}
 60			},
 61			beforeRemove : function() {
 62				viewServerBtn.remove();
 63				uploadbutton.remove();
 64			}
 65		}),
 66		div = dialog.div;
 67
 68		var urlBox = K('[name="url"]', div),
 69			viewServerBtn = K('[name="viewServer"]', div),
 70			titleBox = K('[name="title"]', div);
 71
 72		var uploadbutton = K.uploadbutton({
 73			button : K('.ke-upload-button', div)[0],
 74			fieldName : 'imgFile',
 75			url : K.addParam(uploadJson, 'dir=file'),
 76			afterUpload : function(data) {
 77				dialog.hideLoading();
 78				if (data.error === 0) {
 79					var url = K.formatUrl(data.url, 'absolute');
 80					urlBox.val(url);
 81					if (self.afterUpload) {
 82						self.afterUpload.call(self, url);
 83					}
 84					alert(self.lang('uploadSuccess'));
 85				} else {
 86					alert(data.message);
 87				}
 88			},
 89			afterError : function(html) {
 90				dialog.hideLoading();
 91				self.errorDialog(html);
 92			}
 93		});
 94		uploadbutton.fileBox.change(function(e) {
 95			dialog.showLoading(self.lang('uploadLoading'));
 96			uploadbutton.submit();
 97		});
 98		if (allowFileManager) {
 99			viewServerBtn.click(function(e) {
100				self.loadPlugin('filemanager', function() {
101					self.plugin.filemanagerDialog({
102						viewType : 'LIST',
103						dirName : 'file',
104						clickFn : function(url, title) {
105							if (self.dialogs.length > 1) {
106								K('[name="url"]', div).val(url);
107								self.hideDialog();
108							}
109						}
110					});
111				});
112			});
113		} else {
114			viewServerBtn.hide();
115		}
116		urlBox.val(fileUrl);
117		titleBox.val(fileTitle);
118		urlBox[0].focus();
119		urlBox[0].select();
120	};
121	self.clickToolbar(name, function() {
122		self.plugin.fileDialog({
123			clickFn : function(url, title) {
124				var html = '<a href="' + url + '" data-ke-src="' + url + '" target="_blank">' + title + '</a>';
125				self.insertHtml(html).hideDialog().focus();
126			}
127		});
128	});
129});