summaryrefslogtreecommitdiff
path: root/web-ui/app/js/tags
diff options
context:
space:
mode:
authorOla Bini <ola.bini@gmail.com>2014-07-31 19:29:33 -0300
committerOla Bini <ola.bini@gmail.com>2014-07-31 19:29:33 -0300
commit04cf441c5ae18400c6b4865b0b37a71718dc9d46 (patch)
treedd0b0d049ec00389e2d4561b226c46eb1682b997 /web-ui/app/js/tags
parent639a663a4c37020003586438fdcd7ac529a00f10 (diff)
Add web-ui based on previous code
Diffstat (limited to 'web-ui/app/js/tags')
-rw-r--r--web-ui/app/js/tags/data/tags.js42
-rw-r--r--web-ui/app/js/tags/ui/tag.js94
-rw-r--r--web-ui/app/js/tags/ui/tag_base.js24
-rw-r--r--web-ui/app/js/tags/ui/tag_list.js93
-rw-r--r--web-ui/app/js/tags/ui/tag_shortcut.js68
5 files changed, 321 insertions, 0 deletions
diff --git a/web-ui/app/js/tags/data/tags.js b/web-ui/app/js/tags/data/tags.js
new file mode 100644
index 00000000..96f08b99
--- /dev/null
+++ b/web-ui/app/js/tags/data/tags.js
@@ -0,0 +1,42 @@
+define(['flight/lib/component', 'page/events'], function (defineComponent, events) {
+ 'use strict';
+
+ var DataTags = defineComponent(dataTags);
+
+ DataTags.all = {
+ name: 'all',
+ ident: '8752888923742657436',
+ query: 'in:all',
+ default: true,
+ counts:{
+ total:0,
+ read:0,
+ starred:0,
+ replied:0
+ }
+ };
+
+ return DataTags;
+
+ function dataTags() {
+ function sendTagsBackTo(on, params) {
+ return function(data) {
+ data.push(DataTags.all);
+ on.trigger(params.caller, events.tags.received, {tags: data});
+ };
+ }
+
+ this.defaultAttrs({
+ tagsResource: '/tags'
+ });
+
+ this.fetchTags = function(event, params) {
+ $.ajax(this.attr.tagsResource)
+ .done(sendTagsBackTo(this, params));
+ };
+
+ this.after('initialize', function () {
+ this.on(document, events.tags.want, this.fetchTags);
+ });
+ }
+});
diff --git a/web-ui/app/js/tags/ui/tag.js b/web-ui/app/js/tags/ui/tag.js
new file mode 100644
index 00000000..311c3c05
--- /dev/null
+++ b/web-ui/app/js/tags/ui/tag.js
@@ -0,0 +1,94 @@
+/*global _ */
+
+define(
+ [
+ 'flight/lib/component',
+ 'views/templates',
+ 'tags/ui/tag_base',
+ 'page/events',
+ 'views/i18n'
+ ],
+
+ function (defineComponent, templates, tagBase, events, i18n) {
+ 'use strict';
+
+ var Tag = defineComponent(tag, tagBase);
+
+ Tag.appendedTo = function (parent, data) {
+ var res = new this();
+ res.renderAndAttach(parent, data);
+ return res;
+ };
+
+ return Tag;
+
+ function tag() {
+
+ this.viewFor = function (tag, template) {
+ return template({
+ tagName: tag.default ? i18n("tags." + tag.name) : tag.name,
+ ident: tag.ident,
+ count: this.badgeType(tag) === 'total' ? tag.counts.total : (tag.counts.total - tag.counts.read),
+ displayBadge: this.displayBadge(tag),
+ badgeType: this.badgeType(tag),
+ icon: tag.icon
+ });
+ };
+
+ this.decreaseReadCountIfMatchingTag = function (ev, data) {
+ if (_.contains(data.tags, this.attr.tag.name)) {
+ this.attr.tag.counts.read++;
+ this.$node.html(this.viewFor(this.attr.tag, templates.tags.tagInner));
+ }
+ };
+
+ this.triggerSelect = function () {
+ this.trigger(document, events.ui.tag.select, { tag: this.attr.tag.name });
+ this.trigger(document, events.search.empty);
+ };
+
+ this.selectTag = function (ev, data) {
+ data.tag === this.attr.tag.name ? this.doSelect(data) : this.doUnselect();
+ };
+
+ this.doUnselect = function () {
+ this.attr.selected = false;
+ this.$node.removeClass('selected');
+ };
+
+ this.doSelect = function (data) {
+ this.attr.selected = true;
+ this.$node.addClass('selected');
+ this.trigger(document, events.ui.mails.cleanSelected);
+ this.trigger(document, events.ui.tag.selected, data);
+ };
+
+ this.addSearchingClass = function() {
+ if (this.attr.tag.name === 'all'){
+ this.$node.addClass('searching');
+ }
+ };
+
+ this.removeSearchingClass = function() {
+ if (this.attr.tag.name === 'all'){
+ this.$node.removeClass('searching');
+ }
+ };
+
+ this.after('initialize', function () {
+ this.on('click', this.triggerSelect);
+ this.on(document, events.ui.tag.select, this.selectTag);
+ this.on(document, events.mail.read, this.decreaseReadCountIfMatchingTag);
+ this.on(document, events.search.perform, this.addSearchingClass);
+ this.on(document, events.search.empty, this.removeSearchingClass);
+ });
+
+ this.renderAndAttach = function (parent, data) {
+ var rendered = this.viewFor(data.tag, templates.tags.tag);
+ parent.append(rendered);
+ this.initialize('#tag-' + data.tag.ident, data);
+ this.on(parent, events.tags.teardown, this.teardown);
+ };
+ }
+ }
+);
diff --git a/web-ui/app/js/tags/ui/tag_base.js b/web-ui/app/js/tags/ui/tag_base.js
new file mode 100644
index 00000000..58f285f7
--- /dev/null
+++ b/web-ui/app/js/tags/ui/tag_base.js
@@ -0,0 +1,24 @@
+define(['views/i18n', 'page/events'], function(i18n, events) {
+
+ function tagBase() {
+ var ALWAYS_HIDE_BADGE_FOR = ['sent', 'trash', 'all'];
+ var TOTAL_BADGE = ['drafts'];
+
+ this.displayBadge = function(tag) {
+ if(_.include(ALWAYS_HIDE_BADGE_FOR, tag.name)) { return false; }
+ if(this.badgeType(tag) === 'total') {
+ return tag.counts.total > 0;
+ } else {
+ return (tag.counts.total - tag.counts.read) > 0;
+ }
+ };
+
+ this.badgeType = function(tag) {
+ return _.include(TOTAL_BADGE, tag.name) ? 'total' : 'unread';
+ };
+
+ }
+
+ return tagBase;
+
+});
diff --git a/web-ui/app/js/tags/ui/tag_list.js b/web-ui/app/js/tags/ui/tag_list.js
new file mode 100644
index 00000000..02eee7f8
--- /dev/null
+++ b/web-ui/app/js/tags/ui/tag_list.js
@@ -0,0 +1,93 @@
+define(
+ [
+ 'flight/lib/component',
+ 'tags/ui/tag',
+ 'views/templates',
+ 'page/events',
+ 'tags/ui/tag_shortcut'
+ ],
+
+ function(defineComponent, Tag, templates, events, TagShortcut) {
+ 'use strict';
+
+ var ICON_FOR = {
+ 'inbox': 'inbox',
+ 'sent': 'send',
+ 'drafts': 'pencil',
+ 'trash': 'trash-o',
+ 'all': 'archive'
+ };
+
+ var ORDER = {
+ 'inbox': '0',
+ 'sent': '1',
+ 'drafts': '2',
+ 'trash': '3',
+ 'all': '4'
+ };
+
+ return defineComponent(tagList);
+
+ function tagOrder(nm) {
+ return ORDER[nm.name] || '999' + nm.name;
+ }
+
+ function tagList() {
+ this.defaultAttrs({
+ defaultTagList: '#default-tag-list',
+ customTagList: '#custom-tag-list'
+ });
+
+ this.renderShortcut = function (tag, tagComponent) {
+ TagShortcut.appendedTo($('#tags-shortcuts'), { linkTo: tag, trigger: tagComponent});
+ };
+
+ function renderTag(tag, defaultList, customList) {
+ var list = tag.default ? defaultList : customList;
+
+ var tagComponent = Tag.appendedTo(list, {tag: tag});
+ if (_.contains(_.keys(ORDER), tag.name)) {
+ this.renderShortcut(tag, tagComponent);
+ }
+ }
+
+ function resetTagList(lists) {
+ _.each(lists, function (list) {
+ this.trigger(list, events.tags.teardown);
+ list.empty();
+ }.bind(this));
+ }
+
+ this.renderTagList = function(tags) {
+ var defaultList = this.select('defaultTagList');
+ var customList = this.select('customTagList');
+
+ resetTagList.bind(this, [defaultList, customList]).call();
+
+ tags.forEach(function (tag) {
+ renderTag.bind(this, tag, defaultList, customList).call();
+ }.bind(this));
+ };
+
+
+ this.loadTagList = function(ev, data) {
+ this.renderTagList(_.sortBy(data.tags, tagOrder));
+ this.trigger(document, events.ui.tags.loaded, { tag: this.attr.currentTag });
+ };
+
+ this.saveTag = function(ev, data) {
+ this.attr.currentTag = data.tag;
+ };
+
+ this.renderTagListTemplate = function () {
+ this.$node.html(templates.tags.tagList());
+ };
+
+ this.after('initialize', function() {
+ this.on(document, events.ui.tagList.load, this.loadTagList);
+ this.on(document, events.ui.tag.selected, this.saveTag);
+ this.renderTagListTemplate();
+ });
+ }
+ }
+);
diff --git a/web-ui/app/js/tags/ui/tag_shortcut.js b/web-ui/app/js/tags/ui/tag_shortcut.js
new file mode 100644
index 00000000..6e5c6960
--- /dev/null
+++ b/web-ui/app/js/tags/ui/tag_shortcut.js
@@ -0,0 +1,68 @@
+define(
+ [
+ 'flight/lib/component',
+ 'views/templates',
+ 'page/events',
+ 'tags/ui/tag_base'
+ ],
+
+ function (describeComponent, templates, events, tagBase) {
+
+ var TagShortcut = describeComponent(tagShortcut, tagBase);
+
+ TagShortcut.appendedTo = function (parent, data) {
+ var res = new this();
+ res.renderAndAttach(parent, data);
+ return res;
+ };
+
+ return TagShortcut;
+
+ function tagShortcut() {
+
+
+ this.renderAndAttach = function (parent, options) {
+ var linkTo = options.linkTo;
+
+ var model = {
+ tagName: linkTo.name,
+ displayBadge: this.displayBadge(linkTo),
+ badgeType: this.badgeType(linkTo),
+ count: this.badgeType(linkTo) === 'total' ? linkTo.counts.total : (linkTo.counts.total - linkTo.counts.read),
+ icon: iconFor[linkTo.name]
+ };
+
+ var rendered = templates.tags.shortcut(model);
+ parent.append(rendered);
+
+ this.initialize(parent.children().last(),options);
+ };
+
+ var iconFor = {
+ 'inbox': 'inbox',
+ 'sent': 'send',
+ 'drafts': 'pencil',
+ 'trash': 'trash-o',
+ 'all': 'archive'
+ };
+
+ this.selectTag = function (ev, data) {
+ data.tag === this.attr.linkTo.name ? this.doSelect() : this.doUnselect();
+ };
+
+ this.doUnselect = function () {
+ this.$node.removeClass('selected');
+ };
+
+ this.doSelect = function () {
+ this.$node.addClass('selected');
+ };
+
+ this.after('initialize', function () {
+ this.on('click', function () { this.attr.trigger.triggerSelect(); });
+ this.on(document, events.ui.tag.select, this.selectTag);
+ });
+
+ }
+ }
+);