includes/clientside/tinymce/plugins/template/jscripts/template.js
changeset 1 fe660c52c48f
equal deleted inserted replaced
0:902822492a68 1:fe660c52c48f
       
     1 // Import external list url javascript
       
     2 var url = tinyMCE.getParam("template_external_list_url");
       
     3 if (url != null) {
       
     4 	// Fix relative
       
     5 	if (url.charAt(0) != '/' && url.indexOf('://') == -1)
       
     6 		url = tinyMCE.documentBasePath + "/" + url;
       
     7 
       
     8 	document.write('<sc'+'ript language="javascript" type="text/javascript" src="' + url + '"></sc'+'ript>');
       
     9 }
       
    10 
       
    11 var TPU = { //Template Popup Utils
       
    12 	currentTemplateHTML : null,
       
    13 	templates : [],
       
    14 	inst : tinyMCE.getInstanceById(tinyMCE.getWindowArg('editor_id')),
       
    15 	plugin : tinyMCE.getWindowArg('pluginObj'),
       
    16 	data : tinyMCE.selectedInstance.getData('template'),
       
    17 
       
    18  	init : function() {
       
    19  		document.forms[0].insert.value = tinyMCE.getLang('lang_' + this.data.currentAction, 'Insert', true); 
       
    20 		TPU.loadTemplatePaths();
       
    21 
       
    22 		if (this.data.currentAction == "update")
       
    23 			document.getElementById('warning').innerHTML = tinyMCE.getLang('lang_template_warning');
       
    24 
       
    25 		this.resizeInputs();
       
    26 	},
       
    27 
       
    28  	loadTemplatePaths : function() {
       
    29 		var tsrc, sel, x, u;
       
    30 
       
    31  		tsrc = tinyMCE.getParam("template_templates", false);
       
    32  		sel = document.getElementById('tpath');
       
    33 
       
    34 		// Setup external template list
       
    35 		if (!tsrc && typeof(tinyMCETemplateList) != 'undefined') {
       
    36 			for (x=0, tsrc = []; x<tinyMCETemplateList.length; x++)
       
    37 				tsrc.push({title : tinyMCETemplateList[x][0], src : tinyMCETemplateList[x][1], description : tinyMCETemplateList[x][2]});
       
    38 		}
       
    39 
       
    40 		for (x=0; x<tsrc.length; x++) {
       
    41 			u = tsrc[x].src;
       
    42 
       
    43 			// Force absolute
       
    44 			if (u.indexOf('://') == -1 && u.indexOf('/') != 0)
       
    45 				u = tinyMCE.documentBasePath + "/" + u;
       
    46 
       
    47 			tsrc[x].src = u;
       
    48 		}
       
    49 
       
    50 		TPU.templates = tsrc;
       
    51 
       
    52 		for (x = 0; x < tsrc.length; x++)
       
    53 			sel.options[sel.options.length] = new Option(tsrc[x].title, tsrc[x].src);
       
    54 	},
       
    55 
       
    56  	selectTemplate : function(o) {
       
    57 		var x, d = window.frames['templatesrc'].document;
       
    58 
       
    59 		this.currentTemplateHTML = this.plugin._replaceValues(this.getFileContents(o.value));
       
    60 
       
    61 		// Force complete document
       
    62 		if (!/<body/gi.test(this.currentTemplateHTML)) {
       
    63 			this.currentTemplateHTML = '<html xmlns="http://www.w3.org/1999/xhtml">' + 
       
    64 				'<head>' + 
       
    65 					'<title>blank_page</title>' + 
       
    66 					'<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />' + 
       
    67 				'</head>' + 
       
    68 				'<body>' + 
       
    69 				this.currentTemplateHTML + 
       
    70 				'</body>' + 
       
    71 				'</html>';
       
    72 		}
       
    73 
       
    74 		// Write HTML to preview iframe
       
    75 		d.body.innerHTML = this.currentTemplateHTML;
       
    76 
       
    77 		// Display description
       
    78  		for (x = 0; x < TPU.templates.length; x++) {
       
    79 			if (TPU.templates[x].src == o.value) {
       
    80 				document.getElementById('tmpldesc').innerHTML = TPU.templates[x].description;
       
    81 				break;
       
    82 			}
       
    83 		}
       
    84  	},
       
    85 
       
    86  	insertTemplate : function() {
       
    87 		var sel, opt;
       
    88 
       
    89 		sel = document.getElementById('tpath');
       
    90 		opt = sel.options[sel.selectedIndex];
       
    91 
       
    92 		// Is it a template or snippet
       
    93 		if (TPU.currentTemplateHTML.indexOf('mceTmpl'))
       
    94 			tinyMCEPopup.execCommand('mceTemplate', false, {title : opt.text, tsrc : opt.value, body : TPU.currentTemplateHTML});
       
    95 		else
       
    96 			tinyMCEPopup.execCommand('mceInsertContent', false, TPU.currentTemplateHTML);
       
    97 
       
    98 		tinyMCEPopup.close();
       
    99 	},
       
   100 
       
   101 	getFileContents : function(u) {
       
   102 		var x, d, t = 'text/plain';
       
   103 
       
   104 		function g(s) {
       
   105 			x = 0;
       
   106 
       
   107 			try {
       
   108 				x = new ActiveXObject(s);
       
   109 			} catch (s) {
       
   110 			}
       
   111 
       
   112 			return x;
       
   113 		};
       
   114 
       
   115 		x = window.ActiveXObject ? g('Msxml2.XMLHTTP') || g('Microsoft.XMLHTTP') : new XMLHttpRequest();
       
   116 
       
   117 		// Synchronous AJAX load file
       
   118 		x.overrideMimeType && x.overrideMimeType(t);
       
   119 		x.open("GET", u, false);
       
   120 		x.send(null);
       
   121 
       
   122 		return x.responseText;
       
   123 	},
       
   124 
       
   125 	resizeInputs : function() {
       
   126 		var wHeight, wWidth, elm;
       
   127 
       
   128 		if (!self.innerWidth) {
       
   129 			wHeight = document.body.clientHeight - 160;
       
   130 			wWidth = document.body.clientWidth - 40;
       
   131 		} else {
       
   132 			wHeight = self.innerHeight - 160;
       
   133 			wWidth = self.innerWidth - 40;
       
   134 		}
       
   135 
       
   136 		elm = document.getElementById('templatesrc');
       
   137 
       
   138 		if (elm) {
       
   139 			elm.style.height = Math.abs(wHeight) + 'px';
       
   140 			elm.style.width  = Math.abs(wWidth - 5) + 'px';
       
   141 		}
       
   142 	}
       
   143 };