<commit_before>// -*- js-indent-level: 2 -*-
var {Log4Moz} = ChromeUtils.import("resource:///modules/gloda/log4moz.js");
const {FPVSDefaultPreferencesLoader} = ChromeUtils.import(
  "chrome://FolderPaneSwitcher/content/defaultPreferencesLoader.jsm");

// Rules:
// 
// Enter title bar:
//   Restart timer
// Exit title bar:
//   Cancel timer
// Drag over:
//   Record current folder for movecopycompleted listener.
// Timer expires:
//   Cache old folder view
//   Switch to all folders view
//   Initiate the recurring watch timer
// Drag finished:
//   Cancel timer
//   Switch to cached folder view, if any
// Watch timer:
//   Is there a cached view? Yes:
//     Is there no current drag session? Yes:
//       Pretend drag finished
//   Is there a cached view? No:
//     Cancel watch timer

var FolderPaneSwitcher = {
  addRemoveButtonsObserver: {
    observe: function() {
      var prefBranch = Components.classes["@mozilla.org/preferences-service;1"]
        .getService(Components.interfaces.nsIPrefBranch);
      var should_be_hidden =
          !prefBranch.getBoolPref("extensions.FolderPaneSwitcher.arrows");
      var is_hidden =
          !!document.getElementById(
            "FolderPaneSwitcher-back-arrow-button").hidden;
      if (should_be_hidden != is_hidden) {
        document.getElementById("FolderPaneSwitcher-back-arrow-button").
          hidden = should_be_hidden;
        document.getElementById("FolderPaneSwitcher-forward-arrow-button").
          hidden = should_be_hidden;
      }
    }
  },

  goBackView: function() {
    var currentMode = gFolderTreeView.mode;
    var prevMode = null;
    var modes = Object.keys(gFolderTreeView._modes)
    for (var i in modes) {
      m = modes[i];
      if (m == currentMode) {
        if (prevMode) {
          gFolderTreeView.mode = prevMode;
          return;
        }
      }
      if (!this.views[m] || (this.views[m]['menu_enabled'] &&
                             this.views[m]['arrows_enabled'])) {
        prevMode = m;
      }
    }
    if (prevMode) {
      gFolderTreeView.mode = prevMode;
    }
  },
    
  goForwardView: function() {
    var currentMode = gFolderTreeView.mode;
    var prevMode = null;
    var modes = Object.keys(gFolderTreeView._modes).reverse()
    for (var i in modes) {
      m = modes[i];
      if (m == currentMode) {
        if (prevMode) {
          gFolderTreeView.mode = prevMode;
          return;
        }
      }
      if (!this.views[m] || (this.views[m]['menu_enabled'] &&
                             this.views[m]['arrows_enabled'])) {
        prevMode = m;
      }
    }
    if (prevMode) {
      gFolderTreeView.mode = prevMode;
    }
  },

  views: null,

  viewsObserver: {
    register: function(logger, views) {
      this.logger = logger;
      this.views = views;
      fpvsUtils.addObserver(fpvsUtils.viewsBranch, "", this);
      for (var name in views) {
        view = views[name];
        if (! view['menu_enabled']) {
          this.observe(fpvsUtils.viewsBranch, "",
                       view['number'] + '.menu_enabled');
        }
      }
    },

    observe: function(aSubject, aTopic, aData) {
      var match = /^(\d+)\.(.*_enabled)$/.exec(aData);
      if (! match) return;
      var viewNum = match[1];
      var which = match[2];
      var enabled = aSubject.getBoolPref(aData);
      var name = fpvsUtils.getStringPref(fpvsUtils.viewsBranch,
                                         viewNum + ".name");
      var view = this.views[name];
      view[which] = enabled;
      if (which != 'menu_enabled') return;
      if (enabled) {
        gFolderTreeView.registerFolderTreeMode(name, view['handler'],
                                               view['display_name']);
      } else {
        view['handler'] = gFolderTreeView._modes[name];
        gFolderTreeView.unregisterFolderTreeMode(name);
      }
    }
  },

  onLoad: function() {
    // Current Thunderbird nightly builds do not load default preferences
    // from overlay add-ons. They're probably going to fix this, but it may go
    // away again at some point in the future, and in any case we'll need to do
    // it ourselves when we convert from overlay to bootstrapped, and there
    // shouldn't be any harm in setting the default values of preferences twice
    // (i.e., both Thunderbird and our code doing it).
    // This is in a try/catch because if it fails it's probably because
    // setStringPref failed, in which case we're running inside an earlier
    // application version which has already loaded the default preferences
    // automatically.
    try {
        var loader = new FPVSDefaultPreferencesLoader();
        loader.parseUri("chrome://FolderPaneSwitcher-defaults/content/" +
                        "preferences/prefs.js");
    } catch (ex) {}

    fpvsUtils.init();

    if (! this.logger) {
      this.logger = Log4Moz.getConfiguredLogger("extensions.FolderPaneSwitcher",
						Log4Moz.Level.Trace,
						Log4Moz.Level.Info,
						Log4Moz.Level.Debug);
    }
    var me = FolderPaneSwitcher;
    var title = document.getElementById("folderPane-toolbar");
    fpvsUtils.updateViews(gFolderTreeView);
    this.views = fpvsUtils.getViews(true);
    this.viewsObserver.register(this.logger, this.views);

    var prefBranch = Components.classes["@mozilla.org/preferences-service;1"]
        .getService(Components.interfaces.nsIPrefBranch);

    title.addEventListener("dragexit", me.onDragExit, false);
    title.addEventListener("drop", me.onDragDrop, false);
    title.addEventListener("dragenter", me.onDragEnter, false);
    title.collapsed = false;
    FolderPaneSwitcher.addRemoveButtonsObserver.observe();
    fpvsUtils.addObserver(prefBranch, "extensions.FolderPaneSwitcher.arrows",
                          FolderPaneSwitcher.addRemoveButtonsObserver, false);
<commit_msg>Fix usage of destructuring assignment to fix default preferences loading<commit_after>// -*- js-indent-level: 2 -*-
var {Log4Moz} = ChromeUtils.import("resource:///modules/gloda/log4moz.js");

// Rules:
// 
// Enter title bar:
//   Restart timer
// Exit title bar:
//   Cancel timer
// Drag over:
//   Record current folder for movecopycompleted listener.
// Timer expires:
//   Cache old folder view
//   Switch to all folders view
//   Initiate the recurring watch timer
// Drag finished:
//   Cancel timer
//   Switch to cached folder view, if any
// Watch timer:
//   Is there a cached view? Yes:
//     Is there no current drag session? Yes:
//       Pretend drag finished
//   Is there a cached view? No:
//     Cancel watch timer

var FolderPaneSwitcher = {
  addRemoveButtonsObserver: {
    observe: function() {
      var prefBranch = Components.classes["@mozilla.org/preferences-service;1"]
        .getService(Components.interfaces.nsIPrefBranch);
      var should_be_hidden =
          !prefBranch.getBoolPref("extensions.FolderPaneSwitcher.arrows");
      var is_hidden =
          !!document.getElementById(
            "FolderPaneSwitcher-back-arrow-button").hidden;
      if (should_be_hidden != is_hidden) {
        document.getElementById("FolderPaneSwitcher-back-arrow-button").
          hidden = should_be_hidden;
        document.getElementById("FolderPaneSwitcher-forward-arrow-button").
          hidden = should_be_hidden;
      }
    }
  },

  goBackView: function() {
    var currentMode = gFolderTreeView.mode;
    var prevMode = null;
    var modes = Object.keys(gFolderTreeView._modes)
    for (var i in modes) {
      m = modes[i];
      if (m == currentMode) {
        if (prevMode) {
          gFolderTreeView.mode = prevMode;
          return;
        }
      }
      if (!this.views[m] || (this.views[m]['menu_enabled'] &&
                             this.views[m]['arrows_enabled'])) {
        prevMode = m;
      }
    }
    if (prevMode) {
      gFolderTreeView.mode = prevMode;
    }
  },
    
  goForwardView: function() {
    var currentMode = gFolderTreeView.mode;
    var prevMode = null;
    var modes = Object.keys(gFolderTreeView._modes).reverse()
    for (var i in modes) {
      m = modes[i];
      if (m == currentMode) {
        if (prevMode) {
          gFolderTreeView.mode = prevMode;
          return;
        }
      }
      if (!this.views[m] || (this.views[m]['menu_enabled'] &&
                             this.views[m]['arrows_enabled'])) {
        prevMode = m;
      }
    }
    if (prevMode) {
      gFolderTreeView.mode = prevMode;
    }
  },

  views: null,

  viewsObserver: {
    register: function(logger, views) {
      this.logger = logger;
      this.views = views;
      fpvsUtils.addObserver(fpvsUtils.viewsBranch, "", this);
      for (var name in views) {
        view = views[name];
        if (! view['menu_enabled']) {
          this.observe(fpvsUtils.viewsBranch, "",
                       view['number'] + '.menu_enabled');
        }
      }
    },

    observe: function(aSubject, aTopic, aData) {
      var match = /^(\d+)\.(.*_enabled)$/.exec(aData);
      if (! match) return;
      var viewNum = match[1];
      var which = match[2];
      var enabled = aSubject.getBoolPref(aData);
      var name = fpvsUtils.getStringPref(fpvsUtils.viewsBranch,
                                         viewNum + ".name");
      var view = this.views[name];
      view[which] = enabled;
      if (which != 'menu_enabled') return;
      if (enabled) {
        gFolderTreeView.registerFolderTreeMode(name, view['handler'],
                                               view['display_name']);
      } else {
        view['handler'] = gFolderTreeView._modes[name];
        gFolderTreeView.unregisterFolderTreeMode(name);
      }
    }
  },

  onLoad: function() {
    // Current Thunderbird nightly builds do not load default preferences
    // from overlay add-ons. They're probably going to fix this, but it may go
    // away again at some point in the future, and in any case we'll need to do
    // it ourselves when we convert from overlay to bootstrapped, and there
    // shouldn't be any harm in setting the default values of preferences twice
    // (i.e., both Thunderbird and our code doing it).
    // This is in a try/catch because if it fails it's probably because
    // setStringPref failed, in which case we're running inside an earlier
    // application version which has already loaded the default preferences
    // automatically.
    try {
        var {DefaultPreferencesLoader} = ChromeUtils.import(
            "chrome://FolderPaneSwitcher/content/defaultPreferencesLoader.jsm");
        var loader = new DefaultPreferencesLoader();
        loader.parseUri("chrome://FolderPaneSwitcher-defaults/content/" +
                        "preferences/prefs.js");
    } catch (ex) {}

    fpvsUtils.init();

    if (! this.logger) {
      this.logger = Log4Moz.getConfiguredLogger("extensions.FolderPaneSwitcher",
						Log4Moz.Level.Trace,
						Log4Moz.Level.Info,
						Log4Moz.Level.Debug);
    }
    var me = FolderPaneSwitcher;
    var title = document.getElementById("folderPane-toolbar");
    fpvsUtils.updateViews(gFolderTreeView);
    this.views = fpvsUtils.getViews(true);
    this.viewsObserver.register(this.logger, this.views);

    var prefBranch = Components.classes["@mozilla.org/preferences-service;1"]
        .getService(Components.interfaces.nsIPrefBranch);

    title.addEventListener("dragexit", me.onDragExit, false);
    title.addEventListener("drop", me.onDragDrop, false);
    title.addEventListener("dragenter", me.onDragEnter, false);
    title.collapsed = false;
    FolderPaneSwitcher.addRemoveButtonsObserver.observe();
    fpvsUtils.addObserver(prefBranch, "extensions.FolderPaneSwitcher.arrows",
                          FolderPaneSwitcher.addRemoveButtonsObserver, false);

{'fraction_non_alphanumeric': 0.11754670306235605, 'fraction_numerical': 0.0018766527339418237, 'mean_word_length': 2.4320843091334896, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1867361', 'n_tokens_mistral': 3425, 'n_tokens_neox': 3371, 'n_words': 803}