< !doctype html>
< html lang="es-UY">
< head>
< title>test< /title>
< script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js" type="text/javascript">< /script>
< /head>
< body>
< script>
/**
* disablingFocus - disable all the document instead a single element - put 4 masks around the given element
so it appear that all except that element is disabled. */
var lib = {
maskStyle: {
zIndex: 9998,
position: "absolute",
display: "block",
opacity: 0.5,
backgroundColor: "#111111"
},
mask: null,
disableAllBut: function(el) {
if(!lib.masks) {
lib.masks=[];
for(var i = 0; i< 4; i++) {
var maskEl = document.createElement("div");
document.body.appendChild(maskEl);
$(maskEl).attr({id: "disableAllButMask"+i});
$(maskEl).css(lib.maskStyle);
lib.masks.push(maskEl)
}
}
var x = $(el).offset().left,
y = $(el).offset().top,
w = $(el).width(),
h = $(el).height();
var maxWidth = 3333, //$(document.body).offset().width,
maxHeight = 3333; //$(document.body).offset().height;
$(lib.masks[0]).css({
"left": "0px", "top": "0px", "height": y+"px", "width": maxWidth+"px"
});
$(lib.masks[1]).css({
"left": "0px", "top": (y+h)+"px", "height": maxHeight+"px", "width": maxWidth+"px"
});
$(lib.masks[2]).css({
"left": 0+"px", "top": (y)+"px", "height": h+"px", "width": x+"px"
});
$(lib.masks[3]).css({
"left": (x+w)+"px", "top": (y)+"px", "height": h+"px", "width": maxWidth+"px"
})
},
"":""
};
window.disabler=lib;
window.disableAllBut=lib.disableAllBut;
< /script>
< style type="text/css">
< /style>
< div class="apagado">
hola 1
< table>
< tr>< td>hola hola< /td>
< td>
< div class="prendido">
hola 2< p>hhhhh< a href="">hshshs< /a>< /p>
< /div>
< /td>
< /tr>
< tr>
< div id='div3'>
hola 3 asd < span id="div4">asd< /span> as d
< /div>
< /tr>
< /table>
< /div>
< button onclick="disableAllBut(document.getElementById('div4'))">click me for the disabling< /button>
< /html>
The blog of Sebastián Gurin about software development. "A place to store those little things that time will erase if not written...."
lunes, 2 de julio de 2012
Disable all but one html element with jquery
They asked me to be able to disable all the html document but a single html element. Have done this with jquery like the following html snippet. The idea is to add 4 absolute positioned boxed "around" the given element, with big z-index with some transparency:
viernes, 29 de junio de 2012
Disable HTML elements programatically
They asked me to disable / disable some html elements in the client side with javascript so the user cannot interact with cerrtain parts of an html document at some point. My sollution, based on jquery, is to put an html element in front of each elments we want to disable. This mask should be transparent and positioned absolutely with a big z-index. The following is an HTML document that demonstrates the problem and contains a simple implementation of a sollution. Hope it can be usefull to somebody needing something similar.
< !doctype html>
< html lang="es-UY">
< head>
< script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js" type="text/javascript">< /script>
< title>test< /title>
< /head>
< script type="text/javascript">
var lib = {
idCounter: 0,
disablers: [],
/**
* disable the given html elements by putting a mask html element with the same bounds in front of each given element. use disablers.enable for enabling them programatically.
* @param el - a reference to the elements to disable. For example, ".c1" will disable all elements of CSS class c1, "#el1" will disable the element id el1.
* @param maskCSS - optional - an object with extra CSS value for the mask, for example: {"background-color": "red", opacity: 0.2}
* @return the masks elements (array of jquery object) created
*/
disable: function(el, maskCSS) {
var masks = [];
$(el).each(function(){
masks.push(lib.disableOne(el, maskCSS));
});
return masks;
},
/**
* the same as disable, but treat the given object as a single element
*/
disableOne: function(el, maskCSS) {
var id = $(el).attr("id");
if(!id) {
lib.idCounter++;
id="disabler_"+lib.idCounter;
$(el).attr({"id": id});
}
if(!lib.disablers[id]) {
var dis = document.createElement("div");
document.body.appendChild(dis);
lib.disablers[id]=$(dis);
}
var dis = lib.disablers[id];
$(dis).css({
"position": "absolute", "z-index": 99999, "display":
"block", "left": $(el).offset().left, "top": $(el).offset().top,
"width": $(el).width(), "height": $(el).height(), "opacity": 0.4, "background-color": "black"
});
$(dis).show();
if(maskCSS)
$(dis).css(maskCSS);
return dis;
},
enable: function(el) {
if($(el).attr("id") & & lib.disablers[$(el).attr("id")]) {
var dis = lib.disablers[$(el).attr("id")];
dis.hide();
}
}
};
window.disabler=lib;
< /script>
< p>The following table contains a disabled element< /p>
< table>
< tr>< td id="el1">< button>hello< /button>< /td>< td>< p>alskjdlaksjd< /p>< /td>< /tr>
< tr>< td>< button onclick="window.disabler.enable('#el1')">click me for enable < /button>< /td>< td>< p>als< a href="">kjdlaksj< /a>d< /p>< /td>< /tr>
< /table>
< script type="text/javascript">
//examples:
//disable a single element with id "el1"
disabler.disable("#el1");
//disable all < a> elements:
disabler.disable("a");
< /script>
< /body>
< /html>
domingo, 10 de junio de 2012
JavaScript function inBetween
JavaScript function inBetween - be notified when a javascript function is called N times in between a time lapsus of T milliseconds.
This idea borned when reading the work of Ben Alman for function throttle : http://benalman.com/projects/jquery-throttle-debounce-plugin/
In that case the problem is to execute a function no more than once every T milliseconds. This is very helpful when you want to control how much an event handler is called back. I put a common example for events that are called intensively, like mousemove, or drag in my raphaël tutorial: http://cancerbero.vacau.com/ raphaeltut/index.html#sec- events-function-throttle There I explain very usefull cases of function throttle.
The thing is that function throttle made me think on the opposite problem: being notified when a function is called N times in a time lapsus of T milliseconds. This can be useful for registering double, triple, cuadruple clicks, and in general nth-clicks events.I call this, function inBetween (don't have a better name, suggestions accepted). See for example, how I would use inBetween with jquery for neing notified when a triple click occurs in between 1000 ms:
This is a complete working example: http://jsfiddle.net/RP7mW/11/.
Also there is a section in my raphaël tutorial for registering nth-click listeners to raphaël shapes using inBetween(). Link: http://cancerbero.vacau.com/raphaeltut/index.html#sec-events-inbetween
And this is the inBetween function code:
Hope this can be of some use for those who need some advance event - function stuff like this.
This idea borned when reading the work of Ben Alman for function throttle : http://benalman.com/projects/jquery-throttle-debounce-plugin/
In that case the problem is to execute a function no more than once every T milliseconds. This is very helpful when you want to control how much an event handler is called back. I put a common example for events that are called intensively, like mousemove, or drag in my raphaël tutorial: http://cancerbero.vacau.com/
The thing is that function throttle made me think on the opposite problem: being notified when a function is called N times in a time lapsus of T milliseconds. This can be useful for registering double, triple, cuadruple clicks, and in general nth-clicks events.I call this, function inBetween (don't have a better name, suggestions accepted). See for example, how I would use inBetween with jquery for neing notified when a triple click occurs in between 1000 ms:
function click3Hendler(evt) {
alert(this.clickCount+" clicks detected. last click xcoord: "+evt.clientX);
};
$("#someElement").click(inBetween(3, 1000, cb, {clickCount: 3}));
This is a complete working example: http://jsfiddle.net/RP7mW/11/.
Also there is a section in my raphaël tutorial for registering nth-click listeners to raphaël shapes using inBetween(). Link: http://cancerbero.vacau.com/raphaeltut/index.html#sec-events-inbetween
And this is the inBetween function code:
/**
* inBetween resolves the problem of being notified when a function is called N times in in between a time lapsus of T ms.
* @param n the amount of times.
* @param t the time lapsus in ms
* @param callback - the function to be called when the returned fcuntion is called at least n times in a lapsus of n ms.
* @return a new function - when that function is called n times in a lapsus
of t ms, then callback function will be called using the context object as
the callback context.
*/
function inBetween(n, t, callback, context) {
var sb = [];
sb.push("var that = arguments.callee; ")
sb.push("var thisTime = new Date().getTime(); ")
sb.push("var arr = that['ARR'];");
sb.push("if(!arr){");
sb.push(" arr = []; ");
sb.push(" for(var i = 0; i < that['N']; i++) arr.push(thisTime); ");
sb.push(" that['ARR'] = arr;");
sb.push(" that['COUNT']=0");
sb.push("}");
sb.push("that['COUNT']++; ");;
sb.push("arr.push(thisTime);");
sb.push("var lastTime = arr.shift();");
sb.push("if(that['COUNT'] >= that['N']) {");
sb.push(" that['COUNT']=1; ");
sb.push(" for(var i = 0; i < that['N']; i++) arr[i] = thisTime; ");
sb.push(" if(thisTime-lastTime < that['T']) ");
sb.push(" that['CB'].apply(that['CTX'], arguments); ");
sb.push("}");
var fn = new Function(sb.join(""));
fn['N']=n;
fn['T']=t;
fn['CB']=callback;
fn['CTX']=context;
return fn;
};
Hope this can be of some use for those who need some advance event - function stuff like this.
jueves, 17 de mayo de 2012
raphaelJs tutorial
I'm working on a raphaeljs tutorial. The tutorial is available here.
I made it in pure html and css, with a little javascript based on jquery.
It has nice dynamic features like
I made it in pure html and css, with a little javascript based on jquery.
It has nice dynamic features like
- references-links, this is links that opens in an iframe showing an anchor iframes with a method or property documentation of raphaeljs reference .
- code examples that can be runned online. A dialog with a raphal paper will be open with the example inside. The javascript code is prettyfied very using code-google-pretify
- auto creation of a table of content from h1, h2, h3, etc. HTML elements.
- auto creation of an index of terms, using class="index" in those html elements which contents I want to use as an index term.
viernes, 9 de marzo de 2012
RaphaelMarkup - XML syntax for great drawing Javascript API
My new pet project is http://code.google.com/p/raphaelmarkup/, let me present it to you:
Some links:
feature examples: http://cancerbero.vacau.com/raphaelmarkup/src/doc/examples.html
different ways of including raphael XML in HTML documents:
http://cancerbero.vacau.com/raphaelmarkup/src/doc/usage.html
documented raphael XML document definition in XML Schema:
http://cancerbero.vacau.com/raphaelmarkup/src/raphael.xsd
resume:
Write and validate your raphael drawings in xml, stylize with CSS like
syntax. Access any raphael paper instances in an XML DOM using jquery.
Use CSS 3 selectors for locating your shapes in the raphael DOM.
long description:
raphaël javascript library has a portable and nice API for drawing
vector graphics with javascript. This project, RaphaelMarkup, try to
bring declarative parts of raphaël API to XML so you can draw with XML
and CSS. Some important things that raphaelmarkup supports:
* RaphaelMarkup define a XML document type for writing raphaël in
XML. A XML Schema document definition is provided so users can
validate their xml drawings.
* It contains a javascript renderer for easily render xml drawings
in your HTML documents.
Provides several ways of including XML drawings in HTML documents,
like embedding code, ajax, XSL stylesheets, etc.
* Creates an XML DOM that can be manipulated programatically with
tools like jquery. and then easily update the drawing with the changes
in the DOM
* CSS like syntax for raphaël shapes attributes. Raphael elements
support id and class attributes, and CSS3 selectors are supported.
Note that this is not CSS, some cascading rules are different and
raphael attributes are not valid CSS.
* Easy scripting for rendered documents.
* Utilities for easy export any raphael paper to XML drawing code.
RaphaelMarkup depends on raphael and jquery libraries. jquery is used
for XML parsing and XML DOM Manipulation.
The project just started, many things still need revision and some
parts of raphaeljs api still need works. Expect improvements soon! I'm
announcing it here for feedback so please if you have any suggestions.
Expect project inprovements soon!
Some links:
feature examples: http://cancerbero.vacau.com/raphaelmarkup/src/doc/examples.html
different ways of including raphael XML in HTML documents:
http://cancerbero.vacau.com/raphaelmarkup/src/doc/usage.html
documented raphael XML document definition in XML Schema:
http://cancerbero.vacau.com/raphaelmarkup/src/raphael.xsd
resume:
Write and validate your raphael drawings in xml, stylize with CSS like
syntax. Access any raphael paper instances in an XML DOM using jquery.
Use CSS 3 selectors for locating your shapes in the raphael DOM.
long description:
raphaël javascript library has a portable and nice API for drawing
vector graphics with javascript. This project, RaphaelMarkup, try to
bring declarative parts of raphaël API to XML so you can draw with XML
and CSS. Some important things that raphaelmarkup supports:
* RaphaelMarkup define a XML document type for writing raphaël in
XML. A XML Schema document definition is provided so users can
validate their xml drawings.
* It contains a javascript renderer for easily render xml drawings
in your HTML documents.
Provides several ways of including XML drawings in HTML documents,
like embedding code, ajax, XSL stylesheets, etc.
* Creates an XML DOM that can be manipulated programatically with
tools like jquery. and then easily update the drawing with the changes
in the DOM
* CSS like syntax for raphaël shapes attributes. Raphael elements
support id and class attributes, and CSS3 selectors are supported.
Note that this is not CSS, some cascading rules are different and
raphael attributes are not valid CSS.
* Easy scripting for rendered documents.
* Utilities for easy export any raphael paper to XML drawing code.
RaphaelMarkup depends on raphael and jquery libraries. jquery is used
for XML parsing and XML DOM Manipulation.
The project just started, many things still need revision and some
parts of raphaeljs api still need works. Expect improvements soon! I'm
announcing it here for feedback so please if you have any suggestions.
Expect project inprovements soon!
jQuery: accessing XML doms created with $.parseXML and problems with append(string)
In the project raphaelmarkup I'm exploiting jquery API for loading and manipulating XML. First I parse the XML sources and then I can use jquery API for accessing and manipulating it. A documented example:
This has some advantages: its uses the internal XML browser parser, the jquery API is a portable way of accessing the XML DOM.
Now I want to document a problem that I had with this using jquery's append(String) method.
In the previous example, dom.find("p").append("hello ) won't work. This is mainly because append(String) method will create the new elements using the global html document (the global "document variable) instead or using our XML document object. So we must do the following for appending an element :
the complete example based on qunit (ok() is an assertEquals()) :
Being a great tool for parsing and accessing xml documents in xhtml documents, It is a shame that jquery don't let to use arbitrary xml markup strings in methods like append, prepend, etc. :( But it seems that it is a thing we can live without
//some xml string example. the XML must not have any <!doctype definition
var xmlStr = '<p><o><b></b></o><b></b></p>';
//create a jquery dom object for that xml document.
var xmldoc1 = $.parseXML(xmlStr); //the native XMLDocument object
var dom = $(xmldoc1); //the jquery DOM object that we will use fopr accessing
//finds some element and sets attributes using the jquery API
dom.find("p>o").attr("attr1", "val1");
This has some advantages: its uses the internal XML browser parser, the jquery API is a portable way of accessing the XML DOM.
Now I want to document a problem that I had with this using jquery's append(String) method.
In the previous example, dom.find("p").append("
var g1 = $(xmldoc1.createElement("g"));
dom.find("p>o").append(g1);
g1.test("hello");
the complete example based on qunit (ok() is an assertEquals()) :
var xmlStr = ''
var dom = $($.parseXML(xmlStr));
ok(dom.find("b").size()==2, "jquery xml dom 1"); //OK
//set attribute and check -
dom.find("p").attr("attr1", "val1");
ok(dom.find("p").attr("attr1")=="val1", "jquery xml dom 12");
ok($("p", dom).size()==1, "13");//OK
/* a bug, using dom.find("p").append('<d>hello</d>') won't work. it seems
jquery won't let creation of non HTML valid code. */
dom.find("p").append('<d>hello</d>'); //this doesn't work
ok(dom.find("d").size()==1, "jquery xml dom 2"); //FAIL!
/* but this way works. Don't use append(string) but create the element
manually, and pass the dom object to append() */
var c1 = $(dom.get(0).createElement("c")); //make sure new el is created in our xml doc
dom.find("p").append(c1);
ok($("c", dom).size()==1, "14"); //OK
A function for creation of arbitrary xml elements :
/**
* create a new element in raphael XML dom
* @param parent an html dom or jquery selector or object
* where to append the created child.
* @return the created jquery dom element
*/
createElement: function(parent, tagName, attrs) {
parent=$(parent);
var xmldoc = null;
//if they send us a document, we append it on the documentElement
if(rm.isDocument(parent)) {
parent=$(parent.prop("documentElement"));
}
xmldoc=parent.prop("ownerDocument");
var e = $(xmldoc.createElement(tagName));
if(attrs)
e.attr(attrs);
parent.append(e);
return e;
},
Being a great tool for parsing and accessing xml documents in xhtml documents, It is a shame that jquery don't let to use arbitrary xml markup strings in methods like append, prepend, etc. :( But it seems that it is a thing we can live without
jueves, 8 de marzo de 2012
Advance gnome configuration with gconf
In gnome you can configure a lot of advanced stuff using gconf that are not available in GUI programs like gnome-control-center. For example, in default gnomes it is common that new application windows steal the focus of current window. In my case, I launch eclipse (that takes some time to show the window) and a console. While writing on the console, when the eclipse window appears it steals the focus to the console and this is very annoying.
There is no option in visual tools like gnome-control-center for configuring this, but using the gconftool command line program we can check the state of the configuration property that controls the "new windows focus", like this:
gconftool-2 --get /apps/metacity/general/focus_new_windows
or can change its value to "strict" that will prevent focus stealing:
gconftool-2 --set /apps/metacity/general/focus_new_windows --type string strict
More, using the application gconf-editor one can inspect all available properties and edit them. Note that "metacity" is just another gnome application, in this case the window manager (the program that administer windows). The new windows focus policy is a responsability of metacity.
There is no option in visual tools like gnome-control-center for configuring this, but using the gconftool command line program we can check the state of the configuration property that controls the "new windows focus", like this:
gconftool-2 --get /apps/metacity/general/focus_new_windows
or can change its value to "strict" that will prevent focus stealing:
gconftool-2 --set /apps/metacity/general/focus_new_windows --type string strict
More, using the application gconf-editor one can inspect all available properties and edit them. Note that "metacity" is just another gnome application, in this case the window manager (the program that administer windows). The new windows focus policy is a responsability of metacity.
Suscribirse a:
Entradas (Atom)