summaryrefslogtreecommitdiff
path: root/web-ui/app/js/tags/ui/tag_list.js
blob: 02eee7f8f6698e070616300d78fd83be34e0ef43 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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();
      });
    }
  }
);