<commit_before>(function(){
window.NotificationsView = Backbone.CollectionView.extend({
  views: {},

  tagName: "li",
  id: "notifications",
  containerSelector: "ul",

  _unreadCount: 0,

  events: {
    "click": "clickHandler"
  },

  initialize: function () {
    this.collection.on("add", this.add, this);
    this.collection.on("reset", this.reset, this);

    this.modelView = NotificationView;

    this.setupNotificationsFetch();

    this.$el.find('ul').preventScrollPropagation();
  },

  beforeReset: function () {
    this.setUnreadCount(0);
  },

  afterAdd: function (notification) {
    if ( notification.get('unread') === true ) {
      this.setUnreadCount( this._unreadCount + 1 );
    }
  },

  setUnreadCount: function (count) {
    var $unread = this.$el.find('span.unread');
    this._unreadCount = count;

    $unread.text(this._unreadCount);

    if ( count > 0 ) {
      $unread.show();
    } else {
      $unread.hide();
    }
  },

  markAsRead: function () {
    this.collection.markAsRead({
      success: function () {
        this.setUnreadCount(0);
      }
    });
  },

  setupNotificationsFetch: function () {
    var args = arguments;
    var self = this;

    if ( ! this._visible ) {
      this.collection.fetch({
        success: function () {
          setTimeout(function () {
            args.callee.apply(self, args);
          }, 7000);
        }
      });
    } else {
      setTimeout(function () {
        args.callee.apply(self, args);
      }, 7000);
    }
  },

  clickHandler: function (e) {
    var self = this;
    var $dropdown = this.$el.find('ul');

    if ( ! $dropdown.is(':visible' ) ) {
      this.showDropdown();
    } else {
      this.hideDropdown();
    }

    e.stopPropagation();
  },

  showDropdown: function () {
    this._visible = true;

    this.$el
      .addClass('open')
      .find('ul').show();

    this.markAsRead();

    this._bindWindowClick();
  },

  hideDropdown: function () {
    this._visible = false;

    this.$el
      .removeClass("open")
      .find('ul').hide();

    this._unbindWindowClick();
  },

  _bindWindowClick: function () {
    var self = this;

    $(window).on('click.notifications', function ( e ) {
      if ( ! $( e.target ).closest('ul').is('#notifications-dropdown') ) {
        self.hideDropdown();
      }<commit_msg>Mark views as read when closing the dropdown<commit_after>(function(){
window.NotificationsView = Backbone.CollectionView.extend({
  views: {},

  tagName: "li",
  id: "notifications",
  containerSelector: "ul",

  _unreadCount: 0,

  events: {
    "click": "clickHandler"
  },

  initialize: function () {
    this.collection.on("add", this.add, this);
    this.collection.on("reset", this.reset, this);

    this.modelView = NotificationView;

    this.setupNotificationsFetch();

    this.$el.find('ul').preventScrollPropagation();
  },

  beforeReset: function () {
    this.setUnreadCount(0);
  },

  afterAdd: function (notification) {
    if ( notification.get('unread') === true ) {
      this.setUnreadCount( this._unreadCount + 1 );
    }
  },

  setUnreadCount: function (count) {
    var $unread = this.$el.find('span.unread');
    this._unreadCount = count;

    $unread.text(this._unreadCount);

    if ( count > 0 ) {
      $unread.show();
    } else {
      $unread.hide();
    }
  },

  markAsRead: function () {
    var self = this;

    this.collection.markAsRead({
      success: function () {
        self.markViewsForUnreadification();
        self.setUnreadCount(0);
      }
    });
  },

  markViewsForUnreadification: function () {
    this._shouldMarkUnread = true;
  },

  setupNotificationsFetch: function () {
    var args = arguments;
    var self = this;

    if ( ! this._visible ) {
      this.collection.fetch({
        success: function () {
          setTimeout(function () {
            args.callee.apply(self, args);
          }, 7000);
        }
      });
    } else {
      setTimeout(function () {
        args.callee.apply(self, args);
      }, 7000);
    }
  },

  clickHandler: function (e) {
    var self = this;
    var $dropdown = this.$el.find('ul');

    if ( ! $dropdown.is(':visible' ) ) {
      this.showDropdown();
    } else {
      this.hideDropdown();
    }

    e.stopPropagation();
  },

  showDropdown: function () {
    this._visible = true;

    this.$el
      .addClass('open')
      .find('ul').show();

    this.markAsRead();

    this._bindWindowClick();
  },

  hideDropdown: function () {
    this._visible = false;

    this.$el
      .removeClass("open")
      .find('ul').hide();

    if ( this._shouldMarkUnread === true ) {
      this._shouldMarkUnread = false;

      _.forEach(this.views, function ( view ) {
        view.markAsRead();
      });
    }

    this._unbindWindowClick();
  },

  _bindWindowClick: function () {
    var self = this;

    $(window).on('click.notifications', function ( e ) {
      if ( ! $( e.target ).closest('ul').is('#notifications-dropdown') ) {
        self.hideDropdown();
      }
{'fraction_non_alphanumeric': 0.16344428941598566, 'fraction_numerical': 0.00518237990831174, 'mean_word_length': 2.4252559726962457, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4251180', 'n_tokens_mistral': 1660, 'n_tokens_neox': 1589, 'n_words': 276}