Skip to content

Commit

Permalink
bug 3183: Improve album creation
Browse files Browse the repository at this point in the history
update popup selectize with new album
init popup selectize with main selectize value

git-svn-id: http://piwigo.org/svn/trunk@30630 68402e56-0260-453c-a942-63ccdbb3a9ee
  • Loading branch information
mistic100 committed Nov 30, 2014
1 parent 5f80cb5 commit f1a7d6b
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 77 deletions.
2 changes: 2 additions & 0 deletions admin/themes/default/js/LocalStorageCache.js
Expand Up @@ -94,6 +94,8 @@
* must return new data
*/
AbstractSelectizer.prototype._selectize = function($target, globalOptions) {
$target.data('cache', this);

this.get(function(data) {
$target.each(function() {
var filtered, value, defaultValue,
Expand Down
139 changes: 65 additions & 74 deletions admin/themes/default/js/addAlbum.js
@@ -1,38 +1,49 @@
jQuery.fn.pwgAddAlbum = function(options) {
if (!options.cache) {
options = options || {};

var $popup = jQuery('#addAlbumForm'),
$albumParent = $popup.find('[name="category_parent"]')
$button = jQuery(this),
$target = jQuery('[name="'+ $button.data('addAlbum') +'"]'),
cache = $target.data('cache');

if (!$target[0].selectize) {
jQuery.error('pwgAddAlbum: target must use selectize');
}
if (!cache) {
jQuery.error('pwgAddAlbum: missing categories cache');
}

var $popup = jQuery('#addAlbumForm');


function init() {
if ($popup.data('init')) {
return;
}
$popup.data('init', true);
options.cache.selectize($popup.find('[name="category_parent"]'), {

cache.selectize($albumParent, {
'default': 0,
'filter': function(categories) {
categories.push({
id: 0,
fullname: '------------',
global_rank: 0
});


if (options.filter) {
categories = options.filter.call(this, categories);
}

return categories;
}
});

$popup.find('form').on('submit', function(e) {
e.preventDefault();

jQuery('#categoryNameError').text('');

var albumParent = $popup.find('[name="category_parent"]'),
parent_id = albumParent.val(),
name = $popup.find('[name=category_name]').val(),
target = $popup.data('target');

var parent_id = $albumParent.val(),
name = $popup.find('[name=category_name]').val();

jQuery('#categoryNameError').toggle(!name);
if (!name) {
return;
}

jQuery.ajax({
url: 'ws.php?format=json',
Expand All @@ -48,79 +59,59 @@ jQuery.fn.pwgAddAlbum = function(options) {
},
success: function(data) {
jQuery('#albumCreationLoading').hide();
jQuery('[data-add-album="'+ target +'"]').colorbox.close();
$button.colorbox.close();

var newAlbum = {
id: data.result.id,
name: name,
fullname: name,
global_rank: '0',
dir: null,
nb_images: 0,
pos: 0
};

var parentSelectize = $albumParent[0].selectize;

var newAlbum = data.result.id,
newAlbum_name = '',
newAlbum_rank = '0';

if (parent_id != 0) {
newAlbum_name = albumParent[0].selectize.options[parent_id].fullname +' / ';
newAlbum_rank = albumParent[0].selectize.options[parent_id].global_rank +'.1';
}
newAlbum_name+= name;

var $albumSelect = jQuery('[name="'+ target +'"]');

// target is a normal select
if (!$albumSelect[0].selectize) {
var new_option = jQuery('<option/>')
.attr('value', newAlbum)
.attr('selected', 'selected')
.text(newAlbum_name);

$albumSelect.find('option').removeAttr('selected');

if (parent_id==0) {
$albumSelect.prepend(new_option);
}
else {
$albumSelect.find('option[value='+ parent_id +']').after(new_option);
}
}
// target is selectize
else {
var selectize = $albumSelect[0].selectize;

if (jQuery.isEmptyObject(selectize.options)) {
options.cache.clear();
options.cache.selectize($albumSelect, {
'default': newAlbum,
'value': newAlbum
});
}
else {
$albumSelect[0].selectize.addOption({
id: newAlbum,
fullname: newAlbum_name,
global_rank: newAlbum_rank
});

$albumSelect[0].selectize.setValue(newAlbum);
}
var parent = parentSelectize.options[parent_id];
newAlbum.fullname = parent.fullname + ' / ' + newAlbum.fullname;
newAlbum.global_rank = parent.global_rank + '.1';
newAlbum.pos = parent.pos + 1;
}

albumParent.val('');
jQuery('#albumSelection, .selectFiles, .showFieldset').show();
var targetSelectize = $target[0].selectize;
targetSelectize.addOption(newAlbum);
targetSelectize.setValue(newAlbum.id);

parentSelectize.addOption(newAlbum);

if (options.afterSelect) {
options.afterSelect();
}
},
error: function(XMLHttpRequest, textStatus, errorThrows) {
jQuery('#albumCreationLoading').hide();
jQuery('#categoryNameError').text(errorThrows).css('color', 'red');
alert(errorThrows);
}
});
});
}

this.colorbox({
inline: true,
href: '#addAlbumForm',
width: 650, height: 300,
onComplete: function() {
init();
$popup.data('target', jQuery(this).data('addAlbum'));
$popup.find('[name=category_name]').focus();
if (!$popup.data('init')) {
init();
}

jQuery('#categoryNameError').hide();
$popup.find('[name=category_name]').val('').focus();
$albumParent[0].selectize.setValue($target.val() || 0);
}
});

return this;
};
2 changes: 1 addition & 1 deletion admin/themes/default/js/batchManagerGlobal.js
Expand Up @@ -111,7 +111,7 @@ jQuery('[data-datepicker]').pwgDatepicker({
cancelButton: lang.Cancel
});

jQuery('[data-add-album]').pwgAddAlbum({ cache: categoriesCache });
jQuery('[data-add-album]').pwgAddAlbum();

$("input[name=remove_author]").click(function () {
if ($(this).is(':checked')) {
Expand Down
3 changes: 2 additions & 1 deletion admin/themes/default/template/include/add_album.inc.tpl
Expand Up @@ -14,7 +14,8 @@
<br><br>

{'Album name'|@translate}<br>
<input name="category_name" type="text" maxlength="255"> <span id="categoryNameError"></span>
<input name="category_name" type="text" maxlength="255">
<span id="categoryNameError" style="color:red;">{'The name of an album must not be empty'|translate}</span>
<br><br><br>

<input type="submit" value="{'Create'|@translate}">
Expand Down
6 changes: 5 additions & 1 deletion admin/themes/default/template/photos_add_direct.tpl
Expand Up @@ -38,7 +38,11 @@ categoriesCache.selectize(jQuery('[data-selectize=categories]'), {
}
});

jQuery('[data-add-album]').pwgAddAlbum({ cache: categoriesCache });
jQuery('[data-add-album]').pwgAddAlbum({
afterSelect: function() {
jQuery("#albumSelection, .selectFiles, .showFieldset").show();
}
});

var pwg_token = '{$pwg_token}';
var photosUploaded_label = "{'%d photos uploaded'|translate}";
Expand Down

0 comments on commit f1a7d6b

Please sign in to comment.