| 1 |
(function($) { |
| 2 |
_bogo = _bogo || {}; |
| 3 |
|
| 4 |
$(function() { |
| 5 |
$('body.nav-menus-php .menu-item').bogoAddLocaleSelector(); |
| 6 |
}); |
| 7 |
|
| 8 |
$(document).ajaxSuccess(function(event, request, settings) { |
| 9 |
$('body.nav-menus-php .menu-item').bogoAddLocaleSelector(); |
| 10 |
}); |
| 11 |
|
| 12 |
$.fn.bogoAddLocaleSelector = function() { |
| 13 |
return this.each(function() { |
| 14 |
if (_bogo.hasSelector(this)) { |
| 15 |
return; |
| 16 |
} |
| 17 |
|
| 18 |
var id = $(this).attr('id').replace('menu-item-', ''); |
| 19 |
var $selector = _bogo.selector(id); |
| 20 |
$(this).find('.menu-item-settings').prepend($selector); |
| 21 |
}); |
| 22 |
} |
| 23 |
|
| 24 |
_bogo.hasSelector = function(elm) { |
| 25 |
return $(elm).is(':has(.bogo-locale-options)'); |
| 26 |
} |
| 27 |
|
| 28 |
_bogo.selector = function(id) { |
| 29 |
var $selector = $('<fieldset class="bogo-locale-options"></fieldset>'); |
| 30 |
|
| 31 |
if (_bogo.availableLanguages) { |
| 32 |
var $legend = $('<legend></legend>').append(_bogo.selectorLegend); |
| 33 |
$selector.append($legend); |
| 34 |
|
| 35 |
$.each(_bogo.availableLanguages, function(i, val) { |
| 36 |
var checked = false; |
| 37 |
|
| 38 |
if (! _bogo.locales[id] || -1 < $.inArray(i, _bogo.locales[id])) { |
| 39 |
checked = true; |
| 40 |
} |
| 41 |
|
| 42 |
$selector.append(_bogo.checkbox(id, i, checked)); |
| 43 |
}); |
| 44 |
} |
| 45 |
|
| 46 |
return $selector; |
| 47 |
} |
| 48 |
|
| 49 |
_bogo.checkbox = function(id, locale, checked) { |
| 50 |
var prefix = _bogo.cbPrefix || 'bogo-locale'; |
| 51 |
var name_attr = prefix + '[' + id + '][]'; |
| 52 |
var id_attr = 'edit-' + prefix + '-' + id + '-' + locale; |
| 53 |
|
| 54 |
var $cb = $('<input type="checkbox" />'); |
| 55 |
$cb.attr('name', name_attr); |
| 56 |
$cb.attr('id', id_attr); |
| 57 |
$cb.attr('value', locale); |
| 58 |
$cb.prop('checked', checked); |
| 59 |
|
| 60 |
var $label = $('<label class="bogo-locale-option"></label>'); |
| 61 |
$label.attr('for', id_attr); |
| 62 |
$label.append(_bogo.langName(locale)); |
| 63 |
|
| 64 |
if (checked) { |
| 65 |
$label.addClass('checked'); |
| 66 |
} |
| 67 |
|
| 68 |
return $label.prepend($cb); |
| 69 |
} |
| 70 |
|
| 71 |
_bogo.langName = function(locale) { |
| 72 |
return _bogo.availableLanguages[locale] || ''; |
| 73 |
} |
| 74 |
|
| 75 |
$(function() { |
| 76 |
$('body.options-general-php select#WPLANG').each(function() { |
| 77 |
$(this).find('option[selected="selected"]').removeAttr('selected'); |
| 78 |
var val = _bogo.defaultLocale || 'en_US'; |
| 79 |
val = ( 'en_US' == val ? '' : val ); |
| 80 |
$(this).find('option[value="' + val + '"]').first().attr('selected', 'selected'); |
| 81 |
}); |
| 82 |
}); |
| 83 |
|
| 84 |
$(function() { |
| 85 |
$('#bogo-add-translation').click(function() { |
| 86 |
if (! _bogo.post_id) { |
| 87 |
return; |
| 88 |
} |
| 89 |
|
| 90 |
var locale = $('#bogo-translations-to-add').val(); |
| 91 |
var rest_url = _bogo.rest_api.url |
| 92 |
+ 'posts/' + _bogo.post_id |
| 93 |
+ '/translations/' + locale; |
| 94 |
$('#bogo-add-translation').next('.spinner').css('visibility', 'visible'); |
| 95 |
|
| 96 |
$.ajax({ |
| 97 |
type: 'POST', |
| 98 |
url: rest_url, |
| 99 |
beforeSend: function(xhr) { |
| 100 |
xhr.setRequestHeader('X-WP-Nonce', _bogo.rest_api.nonce); |
| 101 |
} |
| 102 |
}).done(function(response) { |
| 103 |
var post = response[locale]; |
| 104 |
|
| 105 |
if (! post) { return; } |
| 106 |
|
| 107 |
var $added = $('<a></a>').attr({ |
| 108 |
href: post.edit_link, |
| 109 |
target: '_blank' |
| 110 |
}).html(post.title.rendered); |
| 111 |
|
| 112 |
$added = $('<li></li>').append($added).append( |
| 113 |
' [' + post.lang.name + ']'); |
| 114 |
$('#bogo-translations').append($added); |
| 115 |
|
| 116 |
$('#bogo-translations-to-add option[value="' + locale + '"]').detach(); |
| 117 |
|
| 118 |
if ($('#bogo-translations-to-add option').length < 1) { |
| 119 |
$('#bogo-add-translation-actions').detach(); |
| 120 |
} |
| 121 |
}).always(function() { |
| 122 |
$('#bogo-add-translation').next('.spinner').css('visibility', 'hidden'); |
| 123 |
}); |
| 124 |
}); |
| 125 |
}); |
| 126 |
|
| 127 |
$(function() { |
| 128 |
if ( 'languages_page_bogo-texts' == pagenow ) { |
| 129 |
$(window).on('beforeunload', function(event) { |
| 130 |
var changed = false; |
| 131 |
|
| 132 |
$('#bogo-translatable-text-strings :text').each(function() { |
| 133 |
if (this.defaultValue != $(this).val()) { |
| 134 |
changed = true; |
| 135 |
} |
| 136 |
}); |
| 137 |
|
| 138 |
if (changed) { |
| 139 |
event.returnValue = _bogo.saveAlert; |
| 140 |
return _bogo.saveAlert; |
| 141 |
} |
| 142 |
}); |
| 143 |
|
| 144 |
$('#bogo-translatable-text-strings').submit(function() { |
| 145 |
$(window).off('beforeunload'); |
| 146 |
}); |
| 147 |
|
| 148 |
$('#select-locale').change(function() { |
| 149 |
location = 'admin.php?page=bogo-texts&locale=' + $(this).val(); |
| 150 |
}); |
| 151 |
} |
| 152 |
}); |
| 153 |
|
| 154 |
})(jQuery); |
| 155 |
|