Merge pull request #482 from nlegoff/fix_admin_fields

[3.8] Fix admin fields application
This commit is contained in:
Romain Neutron
2013-07-11 04:00:18 -07:00
11 changed files with 84 additions and 57 deletions

View File

@@ -970,8 +970,9 @@ class databox_field implements cache_cacheableInterface
$row = $stmt->fetch(PDO::FETCH_ASSOC); $row = $stmt->fetch(PDO::FETCH_ASSOC);
$stmt->closeCursor(); $stmt->closeCursor();
if ($row) if ($row) {
$sorter = (int) $row['sorter']; $sorter = max(1, (int) $row['sorter']);
}
$sql = "INSERT INTO metadatas_structure $sql = "INSERT INTO metadatas_structure
(`id`, `name`, `src`, `readonly`, `indexable`, `type`, `tbranch`, (`id`, `name`, `src`, `readonly`, `indexable`, `type`, `tbranch`,

View File

@@ -4,7 +4,7 @@
</script> </script>
<script type="text/template" id="save_template"> <script type="text/template" id="save_template">
<button type="button" class="btn btn-large btn-success save-all"> <button type="button" class="btn btn-success save-all">
<i class="icon-hdd icon-white"></i> {% trans %}Save all changes{% endtrans %} <i class="icon-hdd icon-white"></i> {% trans %}Save all changes{% endtrans %}
</button> </button>
</script> </script>
@@ -54,7 +54,7 @@
<div class="control-group"> <div class="control-group">
<label class="control-label" for="new-source">{% trans %}Source{% endtrans %}</label> <label class="control-label" for="new-source">{% trans %}Source{% endtrans %}</label>
<div class="controls"> <div class="controls">
<input type="text" id="new-source" class="input-block-level" placeholder=""> <input type="text" placeholder="XMP, IPTC ..." id="new-source" class="input-block-level" placeholder="">
<span class="help-block"></span> <span class="help-block"></span>
</div> </div>
</div> </div>
@@ -248,7 +248,7 @@
</tr> </tr>
<tr> <tr>
<td class="position"> <td class="position">
<%= position + 1 %> <%= position %>
</td> </td>
</tr> </tr>
</table> </table>

View File

@@ -30,6 +30,7 @@ define([
$bottom : $(".row-bottom", this.$scope), $bottom : $(".row-bottom", this.$scope),
$leftBlock : $(".left-block", this.$bottom), $leftBlock : $(".left-block", this.$bottom),
$rightBlock : $(".right-block", this.$bottom), $rightBlock : $(".right-block", this.$bottom),
fieldsToDelete : [],
resizeListBlock: function () { resizeListBlock: function () {
var listBlock = $(".list-block", AdminFieldApp.$leftBlock); var listBlock = $(".list-block", AdminFieldApp.$leftBlock);
listBlock.height(AdminFieldApp.$window.height() - listBlock.offset().top - 10); listBlock.height(AdminFieldApp.$window.height() - listBlock.offset().top - 10);
@@ -92,8 +93,10 @@ define([
AdminFieldApp.$window.trigger("resize"); AdminFieldApp.$window.trigger("resize");
// click on first item list // click on first item list
if (AdminFieldApp.fieldListView.itemViews.length > 0 ) {
_.first(AdminFieldApp.fieldListView.itemViews).clickAction().animate(); _.first(AdminFieldApp.fieldListView.itemViews).clickAction().animate();
} }
}
); );
}; };

View File

@@ -30,9 +30,9 @@ define([
var pattern = new RegExp(letters, "gi"); var pattern = new RegExp(letters, "gi");
return this.filter(function(data) { return _(this.filter(function(data) {
return pattern.test(data.get("name")); return pattern.test(data.get("name"));
}); }));
}, },
comparator: function(item) { comparator: function(item) {
return item.get("sorter"); return item.get("sorter");

View File

@@ -122,10 +122,7 @@ define([
"label_fr" : $("#new-label_fr", this.$el).val(), "label_fr" : $("#new-label_fr", this.$el).val(),
"label_de" : $("#new-label_de", this.$el).val(), "label_de" : $("#new-label_de", this.$el).val(),
"label_nl" : $("#new-label_nl", this.$el).val(), "label_nl" : $("#new-label_nl", this.$el).val(),
"multi": $("#new-multivalued", this.$el).is(":checked"), "multi": $("#new-multivalued", this.$el).is(":checked")
"sorter": AdminFieldApp.fieldsCollection.max(function(model) {
return model.get("sorter");
}).get("sorter") + 1
}); });
field.save(null, { field.save(null, {

View File

@@ -183,23 +183,10 @@ define([
modalView.render(); modalView.render();
modalView.on("modal:confirm", function() { modalView.on("modal:confirm", function() {
self.model.destroy({ AdminFieldApp.fieldsToDelete.push(self.model);
success: function(model, response) {
AdminFieldApp.fieldListView.collection.remove(self.model); AdminFieldApp.fieldListView.collection.remove(self.model);
self._selectModelView(index); self._selectModelView(index);
AdminFieldApp.saveView.updateStateButton();
new AlertView({alert: "info", message: i18n.t("deleted_success", {
postProcess: "sprintf",
sprintf: [model.get("name")]
})
}).render();
},
error: function(xhr, textStatus, errorThrown) {
new AlertView({
alert: "error", message: '' !== xhr.responseText ? xhr.responseText : i18n.t("something_wrong")
}).render();
}
});
}); });
return this; return this;
@@ -217,7 +204,7 @@ define([
_selectModelView: function(index) { _selectModelView: function(index) {
// select previous or next itemview // select previous or next itemview
if (index >= 0) { if (index >= 0) {
AdminFieldApp.fieldListView.itemViews[index].select().animate(); AdminFieldApp.fieldListView.itemViews[index].select().animate().click();
} }
}, },
_toggleLabels: function(event) { _toggleLabels: function(event) {

View File

@@ -30,6 +30,7 @@ define([
// rerender whenever there is a change on the collection // rerender whenever there is a change on the collection
this.collection.bind("reset", this.render, this); this.collection.bind("reset", this.render, this);
this.collection.bind("add", this.render, this); this.collection.bind("add", this.render, this);
this.collection.bind("remove", this._onRemove, this);
this.collection.bind("remove", this.render, this); this.collection.bind("remove", this.render, this);
AdminFieldApp.errorManager.on('add-error', function(error) { AdminFieldApp.errorManager.on('add-error', function(error) {
@@ -112,15 +113,27 @@ define([
return this; return this;
}, },
_onRemove : function (model) {
var models = [];
this.collection.each(function(m) {
if (m.get("sorter") > model.get("sorter")) {
m.set("sorter", m.get("sorter") - 1);
}
models.push(m);
});
this.collection.reset(models);
},
updateSortAction: function(event, model, ui) { updateSortAction: function(event, model, ui) {
var newPosition = ui.item.index(); var newPosition = ui.item.index();
this.collection.remove(model, {silent: true}); this.collection.remove(model, {silent: true});
this.collection.each(function (model, index) { this.collection.each(function (model, index) {
var ordinal = index; var ordinal = index;
if (index >= newPosition) ordinal += 1; if (index >= newPosition) ordinal += 2; else ordinal += 1;
model.set({'sorter': ordinal}, {silent: true}); model.set({'sorter': ordinal}, {silent: true});
}); });
model.set({'sorter': newPosition}, {silent: true}); model.set({'sorter': newPosition + 1}, {silent: true});
this.collection.add(model, {at: newPosition}); this.collection.add(model, {at: newPosition});
this.itemViews[0].animate(Math.abs(ui.firstItemPosition)); this.itemViews[0].animate(Math.abs(ui.firstItemPosition));

View File

@@ -70,6 +70,10 @@ define([
return this; return this;
}, },
click: function() {
this.$el.find('.trigger-click').first().trigger('click');
return this;
},
// scroll to current view in item list // scroll to current view in item list
animate: function (top) { animate: function (top) {
top = top || null; top = top || null;

View File

@@ -37,6 +37,21 @@ define([
if (this._isModelDesync()) { if (this._isModelDesync()) {
this._loadingState(true); this._loadingState(true);
$.when.apply($, _.map(AdminFieldApp.fieldsToDelete, function(m){
return m.destroy({
success: function(model, response) {
AdminFieldApp.fieldsToDelete = _.filter(AdminFieldApp.fieldsToDelete, function(m){
return model.get("id") !== m.get("id");
});
},
error: function(xhr, textStatus, errorThrown) {
new AlertView({
alert: "error", message: '' !== xhr.responseText ? xhr.responseText : i18n.t("something_wrong")
}).render();
}
});
})).done(
function() {
AdminFieldApp.fieldsCollection.save({ AdminFieldApp.fieldsCollection.save({
success: function(fields) { success: function(fields) {
// reset collection with new one // reset collection with new one
@@ -56,6 +71,8 @@ define([
self._loadingState(false); self._loadingState(false);
}); });
} }
);
}
return this; return this;
}, },

View File

@@ -76,7 +76,7 @@ define([
beforeEach(function() { beforeEach(function() {
this.collection = new FieldCollection([ this.collection = new FieldCollection([
{"id": 1, "sbas-id": sbasId, "name": "Categorie", "tag": "XMP:Categorie"}, {"id": 1, "sbas-id": sbasId, "name": "Categorie", "tag": "XMP:Categorie"},
{"id": 2, "sbas-id": sbasId, "name": "Object", "tag": "XMP:Object"}, {"id": 2, "sbas-id": sbasId, "name": "Object", "tag": "XMP:Object"}
], { ], {
"sbas_id": sbasId "sbas_id": sbasId
}); });
@@ -96,7 +96,7 @@ define([
describe("Methods", function() { describe("Methods", function() {
it("should retrieve categorie item if searching terms begins with 'cat'", function() { it("should retrieve categorie item if searching terms begins with 'cat'", function() {
this.collection.search('Cat').length.should.equal(1); this.collection.search('Cat')._wrapped.length.should.equal(1);
}); });
it("should retrieve previous and next index for given model", function() { it("should retrieve previous and next index for given model", function() {
@@ -306,7 +306,7 @@ define([
it("should reorder collection on drop action", function() { it("should reorder collection on drop action", function() {
var ui = {item: {index: function() {return 2;}}}; var ui = {item: {index: function() {return 2;}}};
AdminFieldApp.fieldListView.itemViews[0].dropAction({},ui); AdminFieldApp.fieldListView.itemViews[0].dropAction({},ui);
assert.equal(AdminFieldApp.fieldListView.collection.last().get('sorter'), 2, 'model is updated'); assert.equal(AdminFieldApp.fieldListView.collection.last().get('sorter'), 3, 'model is updated');
}); });
}); });
}); });

View File

@@ -53,6 +53,7 @@
#admin-field-app #collection-fields li table { #admin-field-app #collection-fields li table {
table-layout: fixed; table-layout: fixed;
width: 100%; width: 100%;
height: 100%;
} }
#admin-field-app #collection-fields li .field-name { #admin-field-app #collection-fields li .field-name {
@@ -221,6 +222,10 @@
font-weight: bold font-weight: bold
} }
#admin-field-app .save-all{
padding: 15px;
}
#admin-field-app .overlay { #admin-field-app .overlay {
zoom: 1; zoom: 1;
filter: alpha(opacity=50); filter: alpha(opacity=50);