File size: 8,644 Bytes
597ee18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
define(['App', 'jquery', 'underscore', 'backbone', 'hbs!template/header', 'view/userbar-view', 'view/header-sr-display', 'view/basem-view', 'model/sidebar', 'cookie'],
  function(App, $, _, Backbone, HeaderTmpl, UserbarView, SRDisplay, BaseView, SidebarModel, Cookie) {
    return BaseView.extend({
      template: HeaderTmpl,
      events: {
        'click .tabmenu-right li': 'changeGridOption',
        'click .drop-down-header-toggle': 'toggleDropdown',
        //'click #sr-display': 'toggleDropdown', //will destroy the menu after the user makes a selection
        'click #userbar-logged-out': 'showLoginPopup',
        'click #mobileOptions': 'showMobileOptions',
        'click .tabmenu li': 'destroyMobileOptions'
      },
      ui: {
        'siteTable': '#siteTable',
        'nextprev': '.nextprev',
        'headerImg': '#header-img',
        'pagenameA': '.pagename-a',
        'hot': '.hot',
        'New': '.new',
        'rising': '.rising',
        'controversial': '.controversial',
        'top': '.top',
        'normal': '#normal',
        'small': '#small',
        'large': '#large',
        'grid': '#grid',
        'srDisplay': '#sr-display',
        'srBar': '#sr-bar',
        'tabmenu': '.tabmenu'
      },
      regions: {
        'btmRightHeader': '#header-bottom-right',
        'popupWindow': '#popupWindow',
        'srDisplay': '#sr-display'
      },
      initialize: function(data) {
        _.bindAll(this, _.functions(this));
        this.listenTo(App, "header:update", this.updateHeader, this);
        //this.listenTo(App,"login", this.updateSubreddits, this); //so we update the users subreddits after they login
        this.listenTo(App, "logout", this.updateSubreddits, this);
        this.listenTo(App, "header:updateSortOrder", this.updateSortOrder, this);
        this.listenTo(App, "header:refreshSubreddits", this.refreshSubreddits, this);
        this.listenTo(App, 'header:showLoginBox', this.showLoginPopup, this);
        //load the subreddits on the top bar
        //we want to always display the default subreddits at first because they take a long time to get back from the api
        this.listenTo(App.subreddits.mine, 'sync', this.displayMySubreddits)

        if (typeof data !== 'undefined' && typeof data.subName !== 'undefined') {
          this.dynamicStylesheet(data.subName)
        }

      },
      onRender: function() {

        this.changeActiveGrid(App.settings.get('gridOption')) //so we are highlighting the correct grid option on page load

        this.btmRightHeader.show(new UserbarView())
        this.srDisplay.show(new SRDisplay())
        this.displayMySubreddits()

        this.checkHashOpt()

        //check if user has no subreddits
        if (App.subreddits.mine.length < 2) {
          App.subreddits.mine.fetch();
        }

      },
      onBeforeDestroy: function() {

      },

      checkHashOpt: function() {
        if (window.location.hash) {
          var hash = window.location.hash.substring(1); //Puts hash in variable, and removes the # character
          if (hash === "hideOpts") {
            setTimeout(function() {
              $('.tabmenu-right').hide()
            })

          }

        }
      },

      showMobileOptions: function() {
        this.ui.tabmenu.slideToggle()
      },
      destroyMobileOptions: function() {
        if ($(document).width() < App.mobileWidth) {
          //only destroy the mobile options menu if we are in mobile mode
          this.ui.tabmenu.slideUp()
        }

      },
      //no longer using this popup, sending user directly to login reddit page
      showLoginPopup: function(e) {
        var self = this
        if (e) {
          e.preventDefault()
          e.stopPropagation();
        }
        //require(['view/login-popup-view'], function(LoginPopupView) {
        //self.popupWindow.show(new LoginPopupView())
        //});
        //require(['view/oauth'], function(OauthPopupView) {
        //self.popupWindow.show(new OauthPopupView())
        //});

      },

      updateHeader: function(model) {
        this.model = model
          //this.userbar.render()
        this.ui.pagenameA.prop("href", model.get('rname'))
        this.ui.pagenameA.text(model.get('display_name'))
        var header_img = model.get('header_img')
        if (typeof header_img === 'undefined' || header_img === null) {
          this.ui.headerImg.attr("src", '/img/logo.png');
        } else {
          this.ui.headerImg.attr("src", header_img);
        }
        this.ui.hot.prop("href", model.get('rname'))
        this.ui.New.prop("href", model.get('rname') + "/new")
        this.ui.rising.prop("href", model.get('rname') + "/rising")
        this.ui.controversial.prop("href", model.get('rname') + "/controversial")
        this.ui.top.prop("href", model.get('rname') + "/top")

      },
      updateSortOrder: function(data) {
        //console.log(data)
        var sortOrder = data.sortOrder
        var domain = data.domain
        var subName = data.subName
        this.ui.hot.parent().removeClass('selected');
        this.ui.New.parent().removeClass('selected');
        this.ui.rising.parent().removeClass('selected');
        this.ui.controversial.parent().removeClass('selected');
        this.ui.top.parent().removeClass('selected');
        this.$('.' + sortOrder).parent().addClass('selected');

        if (domain === null) {
          //http://localhost/r/funny/new
          this.ui.hot.attr("href", "/r/" + subName + '/');
          this.ui.New.attr("href", "/r/" + subName + '/new');
          this.ui.rising.attr("href", "/r/" + subName + '/rising');
          this.ui.controversial.attr("href", "/r/" + subName + '/controversial');
          this.ui.top.attr("href", "/r/" + subName + '/top');
        } else {
          //http://localhost/domain/i.imgur.com/new
          this.ui.hot.attr("href", "/domain/" + domain + '/');
          this.ui.New.attr("href", "/domain/" + domain + '/new');
          this.ui.rising.attr("href", "/domain/" + domain + '/rising');
          this.ui.controversial.attr("href", "/domain/" + domain + '/controversial');
          this.ui.top.attr("href", "/domain/" + domain + '/top');
        }
      },

      changeGridOption: function(e) {
        e.preventDefault()
        e.stopPropagation();
        var id = this.$(e.currentTarget).attr('id')
        App.trigger("subreddit:changeGridOption", {
          gridOption: id
        });
        App.settings.set('gridOption', id)
        this.changeActiveGrid(id) //so we are highlighting the correct grid option on page load
        $.cookie('gridOption', id, {
          path: '/'
        });
      },
      changeActiveGrid: function(id) {
        if (typeof id === 'undefined' || id === null || id === "") {
          id = 'normal'
        }

        this.ui.normal.removeClass('selected');
        this.ui.small.removeClass('selected');
        this.ui.large.removeClass('selected');
        this.ui.grid.removeClass('selected');
        this.$('#' + id).addClass('selected');
      },
      refreshSubreddits: function() {
        $.removeCookie('subreddits', {
          path: '/'
        });
        this.updateSubreddits()
      },
      updateSubreddits: function() {
        App.subreddits.mine.reset()
          //query the api for /me.json

        App.subreddits.mine.fetch();
      },
      toggleDropdown: function() {
        App.trigger('header-sr-display:toggle')

      },
      displayMySubreddits: function(response, subreddits) {
        var self = this;
        this.ui.srBar.html(" ") //clear the top

        //    Normal Format: 
        //			<li><a href="/r/pics/">pics</a></li>
        //   Every Subreddit after the first one has a seperator:  
        //			<li><span class="separator">-</span><a href= "/r/funny/">funny</a></li>
        //if (this.checkIfLoggedIn() === false) {
        //App.subreddits.loadDefaultSubreddits()
        //}
        //App.subreddits = this.mySubreddits
        var seperator = '';
        var count = 0;
        var subredditStr = ''
        App.subreddits.mine.each(function(model) {

          if (count !== 0) {
            seperator = '<span class="separator">-</span>';
          }

          if (model.get('display_name') != "announcements" && model.get('display_name') != "blog") {

            subredditStr += '<li>' + seperator + '<a href="/r/' + model.get('display_name') + '/">' + model.get('display_name') + '</a></li>'

            count++;
          }
        })

        self.ui.srBar.html(subredditStr)

        //this.displayDropChoices()
        App.trigger("submit:subreddits");
      }

    });
  });