Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2009-07-25 00:05:00

driz
Member
From: Huddersfield, UK
Registered: 2008-03-18
Posts: 441
Website

[request] JavaScript plugin like rvm_css

I’ve been using stm_javascript but would much prefer a plugin like rvm_css whereby it saves the JS in the database AND as an actual file in a directory. And be able to choose the directory from the Admin prefs. I have been trying to copy bits of code from rvm_css to stm_javascript but I’m pretty much out of my dept here, but this should be a super learning experience for me.

Any chance of someone helping me release a plugin that works just like rvm_css but with JS files? I’m using stm_javascript as the base.

I have managed to add the Preferences part so you can choose the directory name.
Files would be delivered like: <txp:drz_js n="my_js_file" /> and then rendered as <script src="http://domain.com/scripts/my_js_file.js"></script>

Please contribute if you can thanks.

EDIT: ruud has asked me add an author prefix. so for this I’ll use drz.

if (@txpinterface == 'admin') {
	add_privs('js', '1,2');
	register_tab("presentation", "js", "Scripts");
	register_callback("handle_backend_javascript", "js");
	register_callback('js_prefs', 'prefs', '', 1);
}
else {
	if(gps('js')){
		return_js(gps('js'));
	}
}

function js($array=''){
	extract($array);
	if(!$n){
		$n = "default";
	}
	return sprintf('<script src="http://%s/?js=%s"></script>', $GLOBALS['prefs']['siteurl'], $n);
}

function return_js($name){
	ob_start();
	ob_end_clean();

	$js = fetch("js", "txp_js", 'name', $name);

	if ($js) {
		$js_decoded =  base64_decode($js);
	}else{
		$js_decoded = "alert('Javascript File could not be find!')";
	}
	header('Content-type: application/x-javascript');
	header(sprintf('Content-Length: %d', strlen($js_decoded)));
	echo $js_decoded;
	exit;
}

function handle_backend_javascript(){
	$js_prefix = "txp_js";

	safe_query(sprintf("CREATE TABLE IF NOT EXISTS %s (
						  name varchar(255) NOT NULL default '',
						  js longtext NOT NULL,
						  PRIMARY KEY  (name));", safe_pfx($js_prefix)));
	if(safe_count($js_prefix, "name='default'") == "0"){
		safe_insert($js_prefix, "name='default', js='Ly8gUGxlYXNlIGluc2VydCB5b3VyIEphdmFTY3JpcHQgaGVyZTo='");
	}

	if (!defined('txpinterface')) die('txpinterface is undefined.');
		if (gps('event') == 'js') {
			$step = gps('step');
			require_privs('js');
			switch ($step) {
				case '': js_edit(); 											break;
				case 'js_edit_raw': js_edit();           	break;
				case 'js_edit_form': js_edit();          	break;
				case 'pour': js_edit();	               		break;
				case 'js_save': js_save();              	break;
				case 'js_save_as': js_save_as();         	break;
				case 'js_save_posted': js_save_posted(); 	break;
				case 'js_delete': js_delete();           	break;
				case 'js_edit': js_edit();               	break;
				case 'del_dec': js_edit();               	break;
				case 'add_dec': js_edit();                break;
				case 'add_sel': js_edit();
			}
	}
}

// ------------------------------

//-------------------------------------------------------------
	function js_list($name){
		$out[] = startTable('list','left');
		$rs = safe_rows_start("name as jsname", ("txp_js"),"1=1");
		if ($rs) {
			while ($a = nextRow($rs)) {
				extract($a);
				$namelink = ($name!=$jsname)
				?	eLink('js','','name',$jsname,$jsname)
				:	$jsname;
				$deletelink = ($jsname!='default') ?
					dLink('js','js_delete','name',$jsname) : '';
				$out[] = tr(td($namelink).td($deletelink));
			}
			$out[] =  endTable();
			return join('',$out);
		}
	}

//-------------------------------------------------------------
	function js_edit($message=''){
		pagetop(gTxt("edit_js"),$message);
		global $step,$prefs;
		if (!$step or $step == 'js_save'){
			js_edit_raw();
		} else {
			if ($step=='js_edit_raw' or $step=='pour' or $step=='js_delete') {
				js_edit_raw();
			}
		}
	}

//-------------------------------------------------------------
	function js_edit_raw(){
		global $step;
		$name = (!gps('name') or $step=='js_delete') ? 'default' : gps('name');
		if (gps('copy') && trim(preg_replace('/[<>&"\']/', '', gps('newname'))) )
			$name = gps('newname');

		if ($step=='pour'){
			$buttons =
			gTxt('Name of JavaScript file').': '
			.fInput('text','newname','','edit','','',20).
			hInput('savenew','savenew');
			$thejs = '';
		} else {
			$buttons = '';
			$thejs = base64_decode(fetch("js", "txp_js",'name',$name));
		}

		if ($step!='pour') {
			$left = graf(gTxt('You are editing:').br.strong($name)).
				graf(sLink('js', 'pour', gTxt('Create new JavaScript file')));
		} else {
			$left = '&nbsp;';
		}

		$right =
		hed(gTxt('All JavaScript files'),2).
		js_list($name);

		echo
		startTable('edit').
		tr(
			tdtl(
				$left
			).
			td(
				form(
					graf($buttons).
					'<textarea id="js" class="code" name="js" cols="78" rows="32">'.$thejs.'</textarea>'.br.
					fInput('submit','',gTxt('save'),'publish').
					eInput('js').sInput('js_save').
					hInput('name',$name)
				)
			).
			tdtl(
				$right
			)
		).
		endTable();

	}

// -------------------------------------------------------------

	function js_save_posted(){
		$name = gps('name');
		#$js  = parsePostedjs();
		$js  = doSlash(base64_encode(js_format($js)));

		safe_update("txp_js", "js = '$js'", "name = '".doSlash($name)."'");

		$message = gTxt('js_updated', array('{name}' => $name));

		js_edit($message);
	}

//-------------------------------------------------------------

	function js_save(){
		extract(gpsa(array('name','js','savenew','newname','copy')));
		$js = doSlash(base64_encode($js));

		if ($savenew or $copy)
		{
			$newname = doSlash(trim(preg_replace('/[<>&"\']/', '', gps('newname'))));

			if ($newname and safe_field('name', "txp_js", "name = '$newname'"))
			{
				$message = gTxt('js_already_exists', array('{name}' => $newname));
			}
			elseif ($newname)
			{
				safe_insert("txp_js", "name = '".$newname."', js = '$js'");

				$message = gTxt('js_created', array('{name}' => $newname));
			}
			else
			{
				$message = gTxt('js_name_required');
			}

			js_edit($message);
		}

		else
		{
			safe_update("txp_js", "js = '$js'", "name = '".doSlash($name)."'");

			$message = gTxt('js_updated', array('{name}' => $name));

			js_edit($message);
		}
	}

// -------------------------------------------------------------

//-------------------------------------------------------------
	function js_delete(){
		$name = ps('name');

		if ($name != 'default')
		{
			safe_delete("txp_js", "name = '".doSlash($name)."'");

			js_edit(
				gTxt('js_deleted', array('{name}' => $name))
			);
		}

		else
		{
			echo gTxt('cannot_delete_default_js').'.';
		}
	}

// --------------------------------------------------------------------------------------------------------

// Adds the input field for scripts to the admin preferences

function js_prefs() {
  global $textarray;
  $textarray['js_dir'] = 'Scripts directory';
  if (!safe_field ('name', 'txp_prefs', "name='js_dir'")) {
		safe_insert('txp_prefs', "prefs_id=1, name='js_dir', val='js', type=1, event='admin', html='text_input', position=20");
	}
}

Last edited by driz (2009-07-25 12:48:03)


~ Cameron

Offline

#2 2009-07-25 05:54:12

ruud
Developer Emeritus
From: a galaxy far far away
Registered: 2006-06-04
Posts: 5,068
Website

Re: [request] JavaScript plugin like rvm_css

Please use a 3-char author prefix for your plugin functions!

Offline

#3 2009-07-25 23:38:18

driz
Member
From: Huddersfield, UK
Registered: 2008-03-18
Posts: 441
Website

Re: [request] JavaScript plugin like rvm_css

Yeah I will add drz_ to the beginning of the functions once I’ve got it working.

Anyone able to help with the plugin? Thank you


~ Cameron

Offline

#4 2009-07-26 03:26:32

TheEric
Plugin Author
From: Wyoming
Registered: 2004-09-17
Posts: 566

Re: [request] JavaScript plugin like rvm_css

Ask nicely and don’t paste huge chunks of code and maybe you’ll get some help.

Offline

#5 2009-07-26 04:25:44

mrdale
Member
From: Walla Walla
Registered: 2004-11-19
Posts: 2,215
Website

Re: [request] JavaScript plugin like rvm_css

theEric> meh.

Offline

#6 2009-08-17 07:46:00

makss
Plugin Author
From: Ukraine
Registered: 2008-10-21
Posts: 355
Website

Re: [request] JavaScript plugin like rvm_css


aks_cron : Cron inside Textpattern | aks_article : extended article_custom tag
aks_cache : cache for TxP | aks_dragdrop : Drag&Drop categories (article, link, image, file)

Offline

Board footer

Powered by FluxBB