@ -792,21 +792,20 @@ var I18nForms = (function ($) {
var active _locale _selectors ,
add _locale _fields ,
destroy _locale ,
next _localization _number ,
init ,
id _memo = { } ,
memorize _ids ,
number _matcher = /([\[_])(\d+)([\]\[_]{1,2}\w+\]?$)/ ,
observe _add _locale _link ,
observe _destroy _locale _links ,
prepare _for _submit ,
replace _number _in _id _and _ name ,
replace _number _in _name ,
select _first _untaken _option ,
taken _options ,
update _add _link _status ,
update _destroy _link _status ,
update _interaction _elements ,
update _locale _availability ,
init ,
id _memo = [ ] ,
memorize _ids ;
update _locale _availability ;
active _locale _selectors = function ( localized _p ) {
return localized _p . find ( '.locale_selector:not([disabled=disabled])' ) ;
@ -815,13 +814,11 @@ var I18nForms = (function ($) {
add _locale _fields = function ( localized _p ) {
var backup = localized _p . find ( '.translation' ) . first ( ) ,
add _link = localized _p . find ( '.add_locale' ) ,
new _items = backup . clone ( ) ,
next _number = next _localization _number ( localized _p ) ;
new _items = backup . clone ( ) ;
new _items . find ( 'input, textarea' ) . val ( "" ) ;
new _items . insertBefore ( add _link ) ;
replace _number _in _id _and _name ( new _items . find ( 'select, input' ) , next _number ) ;
select _first _untaken _option ( new _items . find ( '.locale_selector' ) , active _locale _selectors ( localized _p ) ) ;
update _interaction _elements ( localized _p ) ;
@ -844,7 +841,7 @@ var I18nForms = (function ($) {
memorize _ids = function ( form ) {
var translations = $ ( '.translation' ) ;
translations . each ( function ( i , element ) {
translations . each ( function ( i , element ) {
var id ,
locale ;
element = $ ( element ) ;
@ -856,18 +853,6 @@ var I18nForms = (function ($) {
id _memo [ locale ] = id ;
}
} ) ;
}
next _localization _number = function ( localized _p ) {
var selectors = localized _p . find ( '.locale_selector' ) ,
taken _numbers ;
taken _numbers = selectors . map ( function ( index , element ) {
number _matcher . exec ( element . name ) ;
return RegExp . $2 ;
} ) ;
return parseInt ( taken _numbers . sort ( ) . get ( ) . pop ( ) , 10 ) + 1 ;
} ;
observe _add _locale _link = function ( ) {
@ -883,81 +868,83 @@ var I18nForms = (function ($) {
} ;
prepare _for _submit = function ( form ) {
var num = 0 ,
var i ,
num = 0 ,
locales = { } ,
locale ,
to _destroy ,
to _keep ,
available _ids = [ ] ,
additional _ids ,
id _elements ,
current _id ,
element ,
translation _classes = [ ] ,
translation _class ,
translation _class _regexp = /\w+_translation/ ,
translation ,
removed _translation ;
removed _translations ,
set _next _number _in _name = function ( elements ) {
replace _number _in _name ( elements , num ) ;
num += 1 ;
} ;
form . find ( '.translation' ) . each ( function ( i , element ) {
var element = $ ( element ) ;
var locale = element . find ( '.locale_selector' ) . val ( ) ;
var id _elements = element . find ( '.translation_id' ) ;
var locale ,
translation _class ;
element = $ ( element ) ;
locale = element . find ( '.locale_selector' ) . val ( ) ;
if ( locales [ locale ] === undefined ) {
locales [ locale ] = $ ( '' ) ;
locales [ locale ] = element ;
} else {
locales [ locale ] = locales [ locale ] . add ( element ) ;
}
locales [ locale ] = locales [ locale ] . add ( element ) ;
translation _class = translation _class _regexp . exec ( element . attr ( 'class' ) ) [ 0 ] ;
translation _class = /\w+_translation/ . exec ( element . attr ( 'class' ) ) [ 0 ] ;
if ( translation _classes . indexOf ( translation _class ) < 0 ) {
translation _classes . push ( translation _class ) ;
}
} ) ;
for ( locale in locale s) {
if ( locales . hasOwnProperty ( locale ) ) {
replace _number _in _id _and _name ( locales [ locale ] . find ( 'select, textarea, input' ) , num ) ;
num += 1 ;
$ . each ( locales , function ( locale , element s) {
var current _id ,
to _destroy ,
to _keep ;
current _id = id _memo [ locale ] !== undefined ? id _memo [ locale ] : '' ;
locales [ locale ] . find ( '.translation_id' ) . attr ( 'value' , current _id ) ;
set _next _number _in _name ( elements . find ( 'select, textarea, input' ) ) ;
to _destroy = locales [ locale ] . filter ( ':hidden' ) ;
to _keep = locales [ locale ] . filter ( ':visible' ) ;
curren t_i d = id _memo [ locale ] !== undefined ? id _memo [ locale ] : '' ;
elements . find ( '.translation_id' ) . attr ( 'value' , current _id ) ;
if ( to _keep . size ( ) > 0 ) {
to _destroy . find ( '.destroy_flag' ) . attr ( 'disabled' , true ) ;
for ( var i = 0 ; i < translation _classes . size ( ) ; i ++ ) {
if ( to _keep . filter ( "." + translation _classes [ i ] ) . size ( ) === 0 ) {
to _destroy . filter ( "." + translation _classes [ i ] ) . find ( 'input[type=text], textarea' ) . attr ( 'value' , '' ) ;
}
to _destroy = elements . filter ( ':hidden' ) ;
to _keep = elements . filter ( ':visible' ) ;
if ( to _keep . size ( ) > 0 ) {
to _destroy . find ( '.destroy_flag' ) . attr ( 'disabled' , true ) ;
for ( i = 0 ; i < translation _classes . size ( ) ; i += 1 ) {
if ( to _keep . filter ( "." + translation _classes [ i ] ) . size ( ) === 0 ) {
to _destroy . filter ( "." + translation _classes [ i ] ) . find ( 'input[type=text], textarea' ) . attr ( 'value' , '' ) ;
}
}
}
}
} ) ;
removed _translations = form . find ( '.translation:hidden' ) ;
var i = 0 ;
for ( locale in id _memo ) {
if ( id _memo . hasOwnProperty ( locale ) && ! locales . hasOwnProperty ( locale ) ) {
translation = $ ( removed _translations [ i ] ) ;
replace _number _in _id _and _name ( translation . find ( 'input, textarea, select' ) , num ) ;
num += 1 ;
$ . each ( id _memo , function ( locale , id ) {
var translation ;
if ( ! locales . hasOwnProperty ( locale ) ) {
translation = $ ( removed _translations [ 0 ] ) ;
removed _translations . filter ( translation ) ;
set _next _number _in _name ( translation . find ( 'input[type=hidden]' ) ) ;
translation . find ( '.destroy_flag' ) . attr ( 'disabled' , false ) ;
translation . find ( '.translation_id' ) . attr ( 'value' , id _memo [ locale ] ) ;
i += 1 ;
}
}
} ) ;
} ;
replace _number _in _id _and _ name = function ( element , rep _number ) {
replace _number _in _name = function ( element , rep _number ) {
element . each ( function ( index , e ) {
e = $ ( e ) ;
e . attr ( 'name' , e . attr ( 'name' ) . replace ( number _matcher , "$1" + rep _number + "$3" ) )
e . attr ( 'name' , e . attr ( 'name' ) . replace ( number _matcher , "$1" + rep _number + "$3" ) ) ;
} ) ;
} ;