/**
 * upfile 1.1v
 * The tinymce-plugins is used to upfile
 * 
 * https://github.com/Five-great/tinymce-plugins
 * 
 * Copyright 2020, Five(Li Hailong) The Chengdu, China https://www.fivecc.cn/
 *
 * Licensed under MIT
 */
tinymce.PluginManager.add('upfile', function(editor, url) {
	var pluginName='上传文件';
	window.upfile={}; //扔外部公共变量,也可以扔一个自定义的位置
	var baseURL=tinymce.baseURL||'.';
	var iframe1 = baseURL+'/plugins/upfile/upfiles.html';
    upfile.file_callback = editor.getParam('file_callback', undefined, 'function');
	upfile.tinymce = tinymce;
    upfile.res={};
	var openDialog = function() {
		return editor.windowManager.openUrl({
	  title: pluginName,
      size: 'large',
      width: 450,
      height: 450,
			url:iframe1,
			buttons: [
				{
					type: 'cancel',
					text: 'Close'
				},
				{
					type: 'custom',
					text: 'Save',
					name: 'save',
					primary: true
				},
			],
			onAction: function (api, details) {
				switch (details.name) {
					case 'save':
						var res = upfile.res;
						var html = ''+res.text+'';
						editor.insertContent(html);
						upfile.res={};
						api.close();
						break;
					default:
						break;
				}
				
			}
		});
	};
	editor.ui.registry.getAll().icons.upfile || editor.ui.registry.addIcon('upfile','');
	
	editor.ui.registry.addButton('upfile', {
		icon: 'upfile',
    tooltip: pluginName,
		onAction: function() {
			openDialog();
		}
	});
	editor.ui.registry.addMenuItem('upfile', {
		icon: 'upfile',
		text: '图片上传...',
		onAction: function() {
			openDialog();
		}
	});
	return {
		getMetadata: function() {
			return  {
				name: pluginName,
				url: "https://github.com/Five-great/tinymce-plugins",
			};
		}
	};
});