CloneSubfields and problems with it
We are running latest git in our production, and we have stumbled upon variation of bug reported at: http://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=3264 We are having problems with cataloguing/addbiblio.tmpl which doesn't correctly clone subfields (it always clone first one) and after removing one subfield whole JavaScript breaks. After spending a day looking at current code, I decided to try to re-implement it. My motivation is following: - currently code is injected in html page. This makes page load slow, because browser has to stop everything until it parse JavaScript - there is variation of same methods in authorities/authorities.tmpl but it has it's own problems. Splitting code into separate, page-independent reusable code would benefit both of problems To do that I moved selected element into anchor of link, so it can be accessed (and updated) easily from JavaScript. Result is following code: function _subfield_id(a) { console.debug( '_subfield_id', a ); return a.href.substr( a.href.indexOf('#')+1 ); } function clone_subfield( from_a ) { console.debug( 'clone_subfield', from_a ); var subfield_id = _subfield_id(from_a); var $original = $('#' + subfield_id); var $clone = $original.clone(); var new_key = CreateKey(); $clone .attr('id', subfield_id + new_key ) .find('input,select,textarea').each( function() { $(this) .attr('id', function() { return this.id + new_key }) .attr('name', function() { return this.name + new_key }) ; }) .end() .find('label').attr('for', function() { return this.for + new_key }) .end() .find('.subfield_controls > a').each( function() { this.href = '#' + subfield_id + new_key; console.debug( 'fix href', this.href ); }) ; console.debug( 'clone', $clone ); $clone.insertAfter( $original ); } function remove_subfield( from_a ) { console.debug( 'remove_subfield', from_a ); var subfield_id = _subfield_id(from_a); $('#'+subfield_id).remove(); } with html change: <span class="subfield_controls"> <a href="#subfield<!-- TMPL_VAR NAME='tag' --><!-- TMPL_VAR NAME='subfield' --><!-- TMPL_VAR name="random" -->" onclick="clone_subfield(this); return false;"><img src="/intranet-tmpl/prog/img/clone-subfield.png" alt="Clone" title="Clone this subfield" /></a> <a href="#subfield<!-- TMPL_VAR NAME='tag' --><!-- TMPL_VAR NAME='subfield' --><!-- TMPL_VAR name="random" -->" onclick="remove_subfield(this); return false;"><img src="/intranet-tmpl/prog/img/delete-subfield.png" alt="Delete" title="Delete this subfield" /></a> </span> for comparison, take at look at 77 lines of CloneSubfield alone in current koha code. However, I'm very aware that my solution is very jquery-eske, and might be too much for people who haven't used jquery before. I also used $o notation in JavaScript for jQuery objects, and that might also be confusing. I would also love to use event bubbling using .live instead of attaching all click handlers, but I have to check how reliably it works with jquery 1.3.2 which koha uses. Would such a change be accepted into Koha and does it make sense to peruse this refactoring? If you want to follow along this experiment, there is a branch for it in our git: http://git.rot13.org/?p=koha.git;a=shortlog;h=refs/heads/clone-subfields-jqu... -- Dobrica Pavlinusic 2share!2flame dpavlin@rot13.org Unix addict. Internet consultant. http://www.rot13.org/~dpavlin
On 15 October 2010 04:48, Dobrica Pavlinusic <dpavlin@rot13.org> wrote:
We are running latest git in our production, and we have stumbled upon variation of bug reported at:
http://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=3264
We are having problems with cataloguing/addbiblio.tmpl which doesn't correctly clone subfields (it always clone first one) and after removing one subfield whole JavaScript breaks.
After spending a day looking at current code, I decided to try to re-implement it. My motivation is following:
<snip>
Would such a change be accepted into Koha and does it make sense to peruse this refactoring?
There's nothing in it that sets off my alarm bells, I would of course ask people like Owen who do far more front end work than me to check it. But certainly it is worth pursuing. Anything you can do to make that whole page load faster, would be gratefully received also Chris
If you want to follow along this experiment, there is a branch for it in our git:
http://git.rot13.org/?p=koha.git;a=shortlog;h=refs/heads/clone-subfields-jqu...
-- Dobrica Pavlinusic 2share!2flame dpavlin@rot13.org Unix addict. Internet consultant. http://www.rot13.org/~dpavlin _______________________________________________ Koha-devel mailing list Koha-devel@lists.koha-community.org http://lists.koha-community.org/cgi-bin/mailman/listinfo/koha-devel
On Thu, Oct 14, 2010 at 3:18 PM, Chris Cormack <chris@bigballofwax.co.nz> wrote:
<snip>
Would such a change be accepted into Koha and does it make sense to peruse this refactoring?
There's nothing in it that sets off my alarm bells, I would of course ask people like Owen who do far more front end work than me to check it. But certainly it is worth pursuing.
Anything you can do to make that whole page load faster, would be gratefully received also
I have to agree with Chris here, anything which improves the speed of this page would be a welcomed improvement. Kind Regards, Chris
Hi, On Thu, Oct 14, 2010 at 11:48 AM, Dobrica Pavlinusic <dpavlin@rot13.org> wrote:
However, I'm very aware that my solution is very jquery-eske, and might be too much for people who haven't used jquery before.
Being "jQuery-esque" isn't a problem at all - Koha uses jQuery anyway, and using a framework is almost almost better than dealing with hand-written JavaScript code to walk the DOM. Regards, Galen -- Galen Charlton gmcharlt@gmail.com
- currently code is injected in html page. This makes page load slow, because browser has to stop everything until it parse JavaScript
Agreed. However much of the embedded script is reliant on TMPL variables, so it's not possible to eliminate it from the template.
However, I'm very aware that my solution is very jquery-eske, and might be too much for people who haven't used jquery before.
As others have said, we use jQuery in many places in Koha so there's no problem here.
I would also love to use event bubbling using .live instead of attaching all click handlers, but I have to check how reliably it works with jquery 1.3.2 which koha uses.
Upgrading the version of jQuery Koha uses should be a to-do item for 3.4.
Would such a change be accepted into Koha and does it make sense to peruse this refactoring?
Anything that will improve the performance of this page will be very much welcomed. Thanks for sharing, -- Owen -- Web Developer Athens County Public Libraries http://www.myacpl.org
Owen Leonard schreef op vr 15-10-2010 om 10:09 [-0400]:
Upgrading the version of jQuery Koha uses should be a to-do item for 3.4.
See http://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=5184 :) -- Robin Sheat Catalyst IT Ltd. ✆ +64 4 803 2204 GPG: 5957 6D23 8B16 EFAB FEF8 7175 14D3 6485 A99C EB6D
Hie, Notice that I proposed a patch for bug 3264. Indeed, a JQuery-like solution is far better. I also use is at 99% in Javascript. Don't forget to change also Authorities MARC edition. It is nearly the same code a for Biblios. I don't know why there are differences. Regards, -- Fridolyn SOMERS ICT engineer PROGILONE SAS - Lyon - France fridolyn.somers@gmail.com On Thu, Oct 14, 2010 at 5:48 PM, Dobrica Pavlinusic <dpavlin@rot13.org>wrote:
We are running latest git in our production, and we have stumbled upon variation of bug reported at:
http://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=3264
We are having problems with cataloguing/addbiblio.tmpl which doesn't correctly clone subfields (it always clone first one) and after removing one subfield whole JavaScript breaks.
After spending a day looking at current code, I decided to try to re-implement it. My motivation is following:
- currently code is injected in html page. This makes page load slow, because browser has to stop everything until it parse JavaScript - there is variation of same methods in authorities/authorities.tmpl but it has it's own problems. Splitting code into separate, page-independent reusable code would benefit both of problems
To do that I moved selected element into anchor of link, so it can be accessed (and updated) easily from JavaScript. Result is following code:
function _subfield_id(a) { console.debug( '_subfield_id', a ); return a.href.substr( a.href.indexOf('#')+1 ); }
function clone_subfield( from_a ) { console.debug( 'clone_subfield', from_a ); var subfield_id = _subfield_id(from_a); var $original = $('#' + subfield_id); var $clone = $original.clone(); var new_key = CreateKey();
$clone .attr('id', subfield_id + new_key ) .find('input,select,textarea').each( function() { $(this) .attr('id', function() { return this.id + new_key }) .attr('name', function() { return this.name + new_key }) ; }) .end() .find('label').attr('for', function() { return this.for + new_key }) .end() .find('.subfield_controls > a').each( function() { this.href = '#' + subfield_id + new_key; console.debug( 'fix href', this.href ); }) ;
console.debug( 'clone', $clone );
$clone.insertAfter( $original ); }
function remove_subfield( from_a ) { console.debug( 'remove_subfield', from_a ); var subfield_id = _subfield_id(from_a); $('#'+subfield_id).remove(); }
with html change:
<span class="subfield_controls"> <a href="#subfield<!-- TMPL_VAR NAME='tag' --><!-- TMPL_VAR NAME='subfield' --><!-- TMPL_VAR name="random" -->" onclick="clone_subfield(this); return false;"><img src="/intranet-tmpl/prog/img/clone-subfield.png" alt="Clone" title="Clone this subfield" /></a> <a href="#subfield<!-- TMPL_VAR NAME='tag' --><!-- TMPL_VAR NAME='subfield' --><!-- TMPL_VAR name="random" -->" onclick="remove_subfield(this); return false;"><img src="/intranet-tmpl/prog/img/delete-subfield.png" alt="Delete" title="Delete this subfield" /></a> </span>
for comparison, take at look at 77 lines of CloneSubfield alone in current koha code.
However, I'm very aware that my solution is very jquery-eske, and might be too much for people who haven't used jquery before. I also used $o notation in JavaScript for jQuery objects, and that might also be confusing.
I would also love to use event bubbling using .live instead of attaching all click handlers, but I have to check how reliably it works with jquery 1.3.2 which koha uses.
Would such a change be accepted into Koha and does it make sense to peruse this refactoring?
If you want to follow along this experiment, there is a branch for it in our git:
http://git.rot13.org/?p=koha.git;a=shortlog;h=refs/heads/clone-subfields-jqu...
-- Dobrica Pavlinusic 2share!2flame dpavlin@rot13.org Unix addict. Internet consultant. http://www.rot13.org/~dpavlin <http://www.rot13.org/%7Edpavlin> _______________________________________________ Koha-devel mailing list Koha-devel@lists.koha-community.org http://lists.koha-community.org/cgi-bin/mailman/listinfo/koha-devel
participants (7)
-
Chris Cormack -
Christopher Nighswonger -
Dobrica Pavlinusic -
Fridolyn SOMERS -
Galen Charlton -
Owen Leonard -
Robin Sheat