Skip to content
This repository has been archived by the owner on Nov 3, 2021. It is now read-only.

Commit

Permalink
Merge pull request #23183 from crdlc/bug-1016228
Browse files Browse the repository at this point in the history
Bug 1016228 - [Collections App] Rename Collections
  • Loading branch information
Cristian Rodriguez committed Sep 9, 2014
2 parents df68032 + 72aa891 commit 91edecd
Show file tree
Hide file tree
Showing 14 changed files with 302 additions and 16 deletions.
2 changes: 1 addition & 1 deletion apps/bookmark/save.html
Expand Up @@ -21,9 +21,9 @@
<!-- Link to component just to copy the default_icon here -->
<!-- <script defer src="shared/elements/gaia_grid/script.js"></script> -->
<script defer src="shared/elements/gaia_grid/js/grid_icon_renderer.js"></script>
<script defer src="shared/js/homescreens/icon.js"></script>

<!-- Specific code -->
<script type="text/javascript" defer src="js/icon.js"></script>
<script type="text/javascript" defer src="js/bookmark_editor.js"></script>
<script type="text/javascript" defer src="js/components/status.js"></script>
<script type="text/javascript" defer src="js/activity_handler.js"></script>
Expand Down
2 changes: 1 addition & 1 deletion apps/bookmark/test/unit/bookmark_editor_test.js
Expand Up @@ -6,7 +6,7 @@

require('/shared/test/unit/load_body_html_helper.js');

requireApp('bookmark/js/icon.js');
require('/shared/js/homescreens/icon.js');
requireApp('bookmark/js/bookmark_editor.js');
require('/shared/js/bookmarks_database.js');
require('/shared/js/url_helper.js');
Expand Down
12 changes: 11 additions & 1 deletion apps/collection/js/activities.js
@@ -1,10 +1,20 @@
'use strict';

/* global CollectionEditor */

(function(exports) {

var Activities = {
'update-collection': function(activity) {
alert('Updating collection!');
CollectionEditor.init({
data: activity.source.data,
onsaved: function() {
activity.postResult('updated');
},
oncancelled: function() {
activity.postError('cancelled');
}
});
}
};

Expand Down
18 changes: 17 additions & 1 deletion apps/collection/js/objects.js
Expand Up @@ -42,6 +42,7 @@
// cf. BaseCollection.save
this.id = props.id || null;
this.name = props.name || '';
this.nonTranslatable = props.nonTranslatable || null;
this.icon = props.icon || null;
this.pinned = props.pinned || [];

Expand Down Expand Up @@ -91,6 +92,10 @@
},

get localizedName() {
if (this.nonTranslatable) {
return this.name;
}

// l10n prefix taken from /shared/locales/collection_categories
var l10nId = 'collection-categoryId-' + this.categoryId;
return navigator.mozL10n.get(l10nId) || this.name;
Expand All @@ -105,6 +110,13 @@
}.bind(this));
},

rename: function rename(name) {
this.name = name;
this.nonTranslatable = true;

return this.write();
},

/**
* Updates the CollectionsDatabase record with the current data.
* If we need to re-render an icon, we do so before saving.
Expand All @@ -129,6 +141,7 @@
var toSave = {
id: this.id,
name: this.name,
nonTranslatable: this.nonTranslatable,
query: this.query,
categoryId: this.categoryId,
cName: this.cName,
Expand Down Expand Up @@ -471,7 +484,10 @@

function QueryCollection(props) {
BaseCollection.call(this, props);
this.name = this.query = props.query;
this.query = props.query;
if (!this.name) {
this.name = this.query;
}
}

QueryCollection.prototype = {
Expand Down
62 changes: 62 additions & 0 deletions apps/collection/js/update_collection.js
@@ -0,0 +1,62 @@
'use strict';

/* global BaseCollection, Icon, GridIconRenderer */
/* exported CollectionEditor */

var CollectionEditor = {
init: function (options) {
this.collection = BaseCollection.create(options.data);

this.onsaved = options.onsaved;
this.oncancelled = options.oncancelled;

this.collectionTitle = document.getElementById('collection-title');
this.collectionTitle.value = this.collection.name || '';

this.collectionIcon = document.getElementById('collection-icon');
this._renderIcon();

this.header = document.getElementById('header');
this.header.addEventListener('action', this.close.bind(this));

this.saveButton = document.getElementById('done-button');
this.saveListener = this.save.bind(this);
this.saveButton.addEventListener('click', this.saveListener);

this.form = document.querySelector('form');
this.form.addEventListener('input', this._checkDoneButton.bind(this));

this.clearButton = document.getElementById('collection-title-clear');
this.clearButton.addEventListener('touchstart',
this._clearTitle.bind(this));
},

close: function() {
this.oncancelled();
},

_renderIcon: function() {
var icon = new Icon(this.collectionIcon, this.collection.icon);
icon.render({
type: GridIconRenderer.TYPE.CLIP
});
},

_clearTitle: function(event) {
event.preventDefault();
this.collectionTitle.value = '';
this._checkDoneButton();
},

_checkDoneButton: function() {
// If name field is blank, the “Done” button should be dimmed and inactive
var title = this.collectionTitle.value.trim();
this.saveButton.disabled = title === '';
},

save: function() {
this.saveButton.removeEventListener('click', this.saveListener);
var newName = this.collectionTitle.value;
this.collection.rename(newName).then(() => this.onsaved());
}
};
4 changes: 4 additions & 0 deletions apps/collection/locales/collection.en-US.properties
Expand Up @@ -16,3 +16,7 @@ edit-done=Done
delete-title=Delete {{name}}
delete-body={{name}} and all of its data will be deleted.
delete=Delete

# update-collection
edit-collection-header=Edit collection
done=Done
28 changes: 28 additions & 0 deletions apps/collection/style/css/update.css
@@ -0,0 +1,28 @@
html, body {
background-color: #f4f4f4;
height: 100%;
}

body section {
height: 100%;
}

form {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: calc(100% - 10rem);
padding: 0 1.5rem;
}

form p {
width: 100%;
}

#collection-icon {
background-repeat: no-repeat;
background-position: center center;
display: block;
margin-bottom: 2.4rem;
}
125 changes: 125 additions & 0 deletions apps/collection/test/unit/update_collection_test.js
@@ -0,0 +1,125 @@
'use strict';

/* global loadBodyHTML, CollectionEditor, Icon */
/* global require, suite, suiteTeardown, suiteSetup, test, assert, sinon */

require('/shared/test/unit/load_body_html_helper.js');
require('/shared/elements/gaia_grid/js/grid_icon_renderer.js');
require('/shared/js/homescreens/icon.js');

requireApp('collection/js/common.js');
requireApp('collection/js/objects.js');
requireApp('collection/js/update_collection.js');

suite('update_collection.js >', function() {

var name = 'Enjoy', iconRenderStub;

var collection = {
id: 134,
categoryId: 423,
name: name
};

suiteSetup(function() {
loadBodyHTML('/update.html');
iconRenderStub = sinon.stub(Icon.prototype, 'render', noop);
});

suiteTeardown(function() {
document.body.innerHTML = '';
iconRenderStub.restore();
});

function noop() {
// Do nothing
}

function dispatchInputEvent() {
CollectionEditor.form.dispatchEvent(new CustomEvent('input'));
}

suite('UI initialized correctly >', function() {
suiteSetup(function() {
CollectionEditor.init({
data: collection
});
});

test('The title was defined accordingly >', function() {
assert.equal(CollectionEditor.collectionTitle.value, name);
});

test('"done" button is disabled initially', function() {
assert.isTrue(CollectionEditor.saveButton.disabled);
});
});

suite('Checking "done" button >', function() {

suiteSetup(function() {
CollectionEditor.init({
data: collection
});
});

test('Typing collection name ', function() {
CollectionEditor.collectionTitle.value = 'Telefonica';
dispatchInputEvent();
assert.isFalse(CollectionEditor.saveButton.disabled);

CollectionEditor.collectionTitle.value = '';
dispatchInputEvent();
assert.isTrue(CollectionEditor.saveButton.disabled);

CollectionEditor.collectionTitle.value = name;
dispatchInputEvent();
assert.isFalse(CollectionEditor.saveButton.disabled);
});

});

suite('Checking "close" button >', function() {

test('Cancelling after clicking "close" button ', function(done) {
CollectionEditor.init({
data: collection,
oncancelled: function() {
this.oncancelled = noop;
done();
}
});

CollectionEditor.header.dispatchEvent(new CustomEvent('action'));
});

});

suite('Saving collection with another name >', function() {
var expectedName = 'Games';

test('Updated successfully ', function(done) {
CollectionEditor.init({
data: collection,
onsaved: function() {
this.onsaved = noop;
done();
}
});

sinon.stub(CollectionEditor.collection, 'rename', function(name) {
assert.equal(name, expectedName);
return {
then: function(resolve) {
resolve();
}
};
});

CollectionEditor.collectionTitle.value = expectedName;
CollectionEditor.saveButton.click();
});

});

});
35 changes: 33 additions & 2 deletions apps/collection/update.html
Expand Up @@ -5,13 +5,44 @@
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1">

<script defer src="shared/js/l10n.js"></script>

<!-- Gaia Components -->
<script defer src="shared/elements/config.js"></script>
<script defer src="shared/elements/gaia-header/dist/script.js"></script>

<link rel="stylesheet" href="shared/elements/gaia-theme/style.css">
<link rel="stylesheet" href="shared/style/input_areas.css">
<link rel="stylesheet" href="style/css/base.css">
<link rel="stylesheet" href="style/css/update.css">

<script defer src="shared/js/collections_database.js"></script>

<script defer src="shared/js/everythingme/eme.js"></script>

<script defer src="js/common.js"></script>
<script defer src="js/objects.js"></script>

<script defer src="shared/elements/gaia_grid/js/grid_icon_renderer.js"></script>
<script defer src="shared/js/homescreens/icon.js"></script>
<script defer src="js/update_collection.js"></script>
<script defer src="js/activities.js"></script>

<link rel="resource" type="application/l10n" href="locales/locales.ini">
</head>
<body>
View a smart collection.
<body class="theme-media">
<section role="region">
<gaia-header id="header" action="close">
<h1 data-l10n-id="edit-collection-header"></h1>
<button id="done-button" disabled type="button" data-l10n-id="done"></button>
</gaia-header>

<form>
<img id="collection-icon">
<p>
<input type="text" id="collection-title" dir="auto"></input>
<button id="collection-title-clear" type="reset">Clear</button>
</p>
</form>
</section>
</body>
</html>
@@ -1,12 +1,12 @@
'use strict';

/* global Icon, GridIconRenderer, MocksHelper, devicePixelRatio */
/* global require, requireApp, suite, test, assert, sinon */
/* global require, suite, test, assert, sinon */

require('/shared/elements/gaia_grid/js/grid_icon_renderer.js');
require('/shared/test/unit/mocks/mock_image.js');

requireApp('bookmark/js/icon.js');
require('/shared/js/homescreens/icon.js');

var mocksHelperForIcon = new MocksHelper([
'Image'
Expand Down
2 changes: 1 addition & 1 deletion shared/elements/gaia_grid/js/grid_view.js
Expand Up @@ -231,7 +231,7 @@

// We do not allow users to launch icons in edit mode
if (action === 'launch' && inEditMode) {
if (icon.detail.type !== 'bookmark' || !icon.isEditable()) {
if (!icon.isEditable()) {
return;
}
// Editing a bookmark in edit mode
Expand Down

0 comments on commit 91edecd

Please sign in to comment.