cooking, programming and everyday life xrado

Friday, March 20, 2009

FireSpider and pyGtranslator on GitHub

Since I'm getting so many bug reports and improvement wishes for those two tiny apps I decide to push the source to GitHub repository, available to any one to contribute. I haven't touch them for a while also the code isn't shiny, but any way we are all learning. I wish i could play more with it but I'm currently busy with other things also switching the job and so... 

FireSpider on GitHub and pyGtranslator on GitHub

  tags: project COMMENTS (0)

Thursday, February 19, 2009

Dynamic select boxes

These days i was working on a project, where i needed dynamically loading select boxes. I first tried mootools native methods like removing and injecting new option elements in to select element. That worked just fine in FF, but as you expect in IE did not. IE's select box stood unchanged. I remember while ago i have to re-inject the whole select box to see the changes in IE.

According to DOM, there is a better way. HTMLSelectElement support add and remove method that work in all major browsers. So i have implemented three basic methods for mootools Element class.
Element.implement({
	removeAllOptions: function() {
		if(this.get('tag')!='select') return this;
		for(var i=this.options.length-1;i>=0;i--) this.remove(i);
		return this;
	},

	addOption: function(text,value) {
		if(this.get('tag')!='select') return this;
		var optn = new Element('option');
		if(text) optn.text = text;
		if(value) optn.value = value;
		this.options.add(optn);
		return this;
	},

	removeOption: function(prop,value){
		if(this.get('tag')!='select') return this;
		for(var i=this.options.length-1;i>=0;i--) {
			if (prop=='selected' && this.options[i].selected) this.remove(i);
			if (prop=='value' && this.options[i].value==value) this.remove(i);
			if (prop=='text' && this.options[i].text==value) this.remove(i);
			if (prop=='index' && i==value) this.remove(i);
		}
		return this;
	}
});
Methods hopefully already explaining their self.

USAGE
// remove all options or empty select box
$('selectbox').removaAllOptions();

// add options, one after another
$('selectbox').addOptions('One',1).addOptions('Two',2).addOptions('Two',3);

// removing options 
$('selectbox').removeOptions('text','One');
$('selectbox').removeOptions('value',2);
$('selectbox').removeOptions('selected');
$('selectbox').removeOptions('index',0);
happy selecting ;)
  tags: javascript mootools COMMENTS (0)

Wednesday, January 21, 2009

Simple combining of multiple javascript and css files

I've already wrote about "firefox only includes styles form .css files" and I found out that isn't quite true. Actually firefox doesn't care about the css file extension, but only relay on header Content-Type: text/css. If you as usual have .css file, apache will take care of right content-type, otherwise you will have to take care about it.
header('Content-Type: text/css');
I came across this when i was writing this script:
## combine.php

// Define path prefix
$prefix = realpath('.');

if($_GET['js']) {
	header('Content-Type: application/x-javascript');
	$data = Array('path'=>$prefix.'/template/js/','files'=>$_GET['js'],'ext'=>'.js');
} elseif($_GET['css']) {
	header('Content-Type: text/css');
	$data = Array('path'=>$prefix.'/template/styles/','files'=>$_GET['css'],'ext'=>'.css');
} else exit();

foreach(explode(',',$data['files']) as $file) {
	if($file && file_exists($data['path'].$file.$data['ext'])) readfile($data['path'].$file.$data['ext']);
}
Simple few lines of code, but already do the trick. That will minimise number of server requests and benefit in faster page loading.

Defining the path and file extension is important, otherwise you may expose some other scripts source or even worse, some system files.

Usage in page head
<link rel="stylesheet" type="text/css" href="combine.php?css=style,forms" />
<script type="text/javascript" src="combine.php?js=mootools,validate,fx"></script>
Hope you like it, otherwise If you want more advanced solution try minify.

cheers
  tags: php css javascript COMMENTS (0)

Thursday, January 01, 2009

pyGtranslator 0.2 (new version)

Here is a new version of pyGtranslator (GUI tool for Google translate) with many fixes and some new features. I think now works as it should.

what's new:
  • utf-8 characters works properly now
  • showing new lines fixed
  • added languages swap button
  • settings are now saved on exit
  • better connection error handling

happy translating and a happy new year :)


Downloads

Source
pygtranslator-0.2.tar.gz

Linux (require python, gtk, pygtk)
- ubuntu / debian: pygtranslator_0.2-1_i386.deb
- zenwalk: pygtranslator-0.2-noarch-54.1.tgz dep md5

Windows (require GTK2 runtime)
pygtranslator-0.2-win32.zip

  tags: project python COMMENTS (1)

Thursday, December 11, 2008

pyGtranslator - GUI tool for Google translate

Update: new version pyGtranslator 0.2 avalible
I've been playing with python these days. I made a small GUI tool that brings Google translate to your desktop. Application was easy to code, although I use python occasionally  (wish I could more and learn it better), but want it to make it run on both linux and windows. That took me quite more time. I almost gave up with py2exe and building windows executable, but after hours of googling and trying I figure out that I have to set environmental path to GTK runtime libs. I also made packages for two my favourite linux distros Zenwalk and Ubuntu. pyGtranslator can be improved and also "suggest a better translation" is missing, but that's maybe for late updates, if the need shows. I hope it works for you, otherwise let me know.

pygtranslator

pyGtranslator on GitHub

Downloads

Source
pygtranslator-0.1.tar.gz

Linux (require python, gtk, pygtk)
- ubuntu / debian: pygtranslator_0.1-1_i386.deb
- zenwalk: pygtranslator-0.1-noarch-54.1.tgz md5 dep

Windows (require GTK2 runtime)
pygtranslator-0.1-win32.zip

  tags: project python COMMENTS (2)
pages:  1   2   3   4   5   6   7   8