<commit_before>'use strict';

var ui = { };

ui.uniqueID = 1;
ui._element = $('<div class="ui" />');
ui._templates = $('<div id="templates" />');
ui._zIndex  = 10;
ui._dialogs = [];

ui.loadTemplate = function(path, callback) {
  ui._templates.load(path, callback);
  $('body').append(ui._element);
};

ui.loadFromTemplate = function(id) {
  var element = ui._templates.find(id).clone();
  id = id + '-' + ui.uniqueID++;
  element.attr('id', id);
  return element;
};

ui.addDialog = function(dialog) {
  ui._element.append(dialog._element);
  ui._dialogs.push(dialog);
  ui.bringToTop(dialog);
};

ui.removeDialog = function(dialog) {
  var index = ui._dialogs.indexOf(dialog);
  if (index === -1) {
    console.warn('Attempted to remove an unregistered dialog');
    return;
  }

  dialog._element.remove();
  dialog._element = null;
  ui._dialogs.splice(index, 1);
  ui._updateDialogOrder();
};

ui.bringToTop = function(dialog) {
  // Do something with dialog
  var index = ui._dialogs.indexOf(dialog);
  if (index === -1) {
    console.warn('Attempted to bring to top an unregistered dialog');
    return;
  }

  ui._dialogs.splice(index, 1);
  ui._dialogs.push(dialog);
  ui._updateDialogOrder();
};

ui._updateDialogOrder = function() {
  for (var i = 0; i < ui._dialogs.length; ++i) {
    ui._dialogs[i]._element.css('z-index', ui._zIndex + i);
  }
};

ui._getWidgetElement = function(type, arg1, arg2) {
  var element = null;

  if (arg1 instanceof ui.Widget && typeof(arg2) === 'string') {
    element = arg1._element.find(arg2);
  } else if (arg1 instanceof jQuery) {
    element = arg1;
  } else if (typeof(arg1) === 'string') {
    var classes = arg1.split('.').join(' ');
    element = $('<div class="' + type + ' ' + classes + '" />');
  } else if (arg1 === undefined && arg2 === undefined) {
    element = $('<div class="' + type + '" />');
  }

  if (element.length === 0) {
    element = null;
  }

  if (element) {
    if (element.length > 1) {
      throw new Error('Tried to create a new ' + type + ' on a jQuery object with multiple elements');
    }

    if (!element.hasClass(type)) {
      throw new Error('Tried to create a new ' + type + ' widget on an element without the class');
    }
  }

  return element;
};

ui.widgetConstructor = function(name, type) {
  return function(arg1, arg2) {
    var element = ui._getWidgetElement(name, arg1, arg2);

    if (element) {
      return new type(element);
    } else {
      return null;
    }
  };
}

ui.KEY_CODES = {<commit_msg>Fix widget creation failing when element === null.<commit_after>'use strict';

var ui = { };

ui.uniqueID = 1;
ui._element = $('<div class="ui" />');
ui._templates = $('<div id="templates" />');
ui._zIndex  = 10;
ui._dialogs = [];

ui.loadTemplate = function(path, callback) {
  ui._templates.load(path, callback);
  $('body').append(ui._element);
};

ui.loadFromTemplate = function(id) {
  var element = ui._templates.find(id).clone();
  id = id + '-' + ui.uniqueID++;
  element.attr('id', id);
  return element;
};

ui.addDialog = function(dialog) {
  ui._element.append(dialog._element);
  ui._dialogs.push(dialog);
  ui.bringToTop(dialog);
};

ui.removeDialog = function(dialog) {
  var index = ui._dialogs.indexOf(dialog);
  if (index === -1) {
    console.warn('Attempted to remove an unregistered dialog');
    return;
  }

  dialog._element.remove();
  dialog._element = null;
  ui._dialogs.splice(index, 1);
  ui._updateDialogOrder();
};

ui.bringToTop = function(dialog) {
  // Do something with dialog
  var index = ui._dialogs.indexOf(dialog);
  if (index === -1) {
    console.warn('Attempted to bring to top an unregistered dialog');
    return;
  }

  ui._dialogs.splice(index, 1);
  ui._dialogs.push(dialog);
  ui._updateDialogOrder();
};

ui._updateDialogOrder = function() {
  for (var i = 0; i < ui._dialogs.length; ++i) {
    ui._dialogs[i]._element.css('z-index', ui._zIndex + i);
  }
};

ui._getWidgetElement = function(type, arg1, arg2) {
  var element = null;

  if (arg1 instanceof ui.Widget && typeof(arg2) === 'string') {
    element = arg1._element.find(arg2);
  } else if (arg1 instanceof jQuery) {
    element = arg1;
  } else if (typeof(arg1) === 'string') {
    var classes = arg1.split('.').join(' ');
    element = $('<div class="' + type + ' ' + classes + '" />');
  } else if (arg1 === undefined && arg2 === undefined) {
    element = $('<div class="' + type + '" />');
  }

  if (element instanceof jQuery && element.length === 0) {
    element = null;
  }

  if (element) {
    if (element.length > 1) {
      throw new Error('Tried to create a new ' + type + ' on a jQuery object with multiple elements');
    }

    if (!element.hasClass(type)) {
      throw new Error('Tried to create a new ' + type + ' widget on an element without the class');
    }
  }

  return element;
};

ui.widgetConstructor = function(name, type) {
  return function(arg1, arg2) {
    var element = ui._getWidgetElement(name, arg1, arg2);

    if (element) {
      return new type(element);
    } else {
      return null;
    }
  };
}

ui.KEY_CODES = {
{'fraction_non_alphanumeric': 0.17619235317303902, 'fraction_numerical': 0.010248324793062672, 'mean_word_length': 3.275484414490312, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '571520', 'n_tokens_mistral': 1764, 'n_tokens_neox': 1717, 'n_words': 417}