From d75483e4a8c9e97de4a336fb1d9e5f85d8465e70 Mon Sep 17 00:00:00 2001 From: Jason Williams Date: Tue, 29 Jul 2014 02:16:21 +0000 Subject: [PATCH] Preserve order of tags as entered by the user. Closes #3133 - Implement an ordered set for the tags property of the tag input controller. Set order is by order added to the post. --- ghost/admin/controllers/post-tags-input.js | 39 +++++++++++++++++++--- ghost/admin/templates/post-tags-input.hbs | 2 +- 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/ghost/admin/controllers/post-tags-input.js b/ghost/admin/controllers/post-tags-input.js index e60fb5a65b..9f82125e8e 100644 --- a/ghost/admin/controllers/post-tags-input.js +++ b/ghost/admin/controllers/post-tags-input.js @@ -1,6 +1,33 @@ var PostTagsInputController = Ember.Controller.extend({ - tags: Ember.computed.alias('parentController.tags'), + tagEnteredOrder: Ember.A(), + + tags: Ember.computed('parentController.tags', function () { + var proxyTags = Ember.ArrayProxy.create({ + content: this.get('parentController.tags') + }), + + temp = proxyTags.get('arrangedContent').slice(); + + proxyTags.get('arrangedContent').clear(); + + this.get('tagEnteredOrder').forEach(function (tagName) { + var tag = temp.find(function (tag) { + return tag.get('name') === tagName; + }); + + if (tag) { + proxyTags.get('arrangedContent').addObject(tag); + temp.removeObject(tag); + } + }); + + temp.forEach(function (tag) { + proxyTags.get('arrangedContent').addObject(tag); + }); + + return proxyTags; + }), suggestions: null, newTagText: null, @@ -36,21 +63,25 @@ var PostTagsInputController = Ember.Controller.extend({ // otherwise create a new one newTag = this.store.createRecord('tag'); newTag.set('name', newTagText); - this.get('tags').pushObject(newTag); + + this.send('addTag', newTag); } this.send('reset'); }, addTag: function (tag) { - if (!Ember.isEmpty(tag) && !this.hasTag(tag.get('name'))) { - this.get('tags').pushObject(tag); + if (!Ember.isEmpty(tag)) { + this.get('tags').addObject(tag); + this.get('tagEnteredOrder').addObject(tag.get('name')); } + this.send('reset'); }, deleteTag: function (tag) { this.get('tags').removeObject(tag); + this.get('tagEnteredOrder').removeObject(tag.get('name')); }, deleteLastTag: function () { diff --git a/ghost/admin/templates/post-tags-input.hbs b/ghost/admin/templates/post-tags-input.hbs index 6d0457446d..6d92101c83 100644 --- a/ghost/admin/templates/post-tags-input.hbs +++ b/ghost/admin/templates/post-tags-input.hbs @@ -1,6 +1,6 @@
-{{#each tags}} +{{#each controller.tags}} {{#view view.tagView tag=this}} {{view.tag.name}} {{/view}}