<commit_before>/*
 * Bloom v1.1.0
 * Javascript UI Library
 * <NAME>
 * Copyright 2012 <NAME>
 */

Class = {};

Class.extend = function(destination, source) {
  if (typeof source == 'object' || typeof source == 'function') {
    for (var k in source) {
      if (source.hasOwnProperty(k)) {
        destination[k] = source[k];
      }
    }
  }
  return destination;
}

Class.extend_methods = function(destination, source) {
  if (typeof source == 'object' || typeof source == 'function') {
    for (var k in source) {
      if (source.hasOwnProperty(k) && jQuery.isFunction(source[k])) {
        destination[k] = source[k];
      }
    }
  }
  return destination;
}
  
Class.sub_class = function (name, data) {
  var result = {};      
    
  Class.extend_methods(result, Class);
  result.name = name;
  result.parent = this;          
  result.properties = data;
    
  return result;
}

Class.initialize_properties = function(object){
  if (this.parent)
    this.parent.initialize_properties(object)
  
  Class.extend(object, this.properties);

}

Class.initialize_methods = function(object, args){
  if (this.parent) {
    var result = this.parent.initialize_methods(object, args);
    if(result)
      args = result;
  }
  
  if (this.properties.hasOwnProperty('initialize'))
    return this.properties.initialize.apply(object, args);  
}

Class.create = function(){
  var result = {};
  this.initialize_properties(result);
  result.type = this;
  var parameters = Array.prototype.slice.call(arguments);
  this.initialize_methods(result, parameters);
  return result;
}

Class.name = 'Class';
Class.parent = null;
Class.properties = {
  extend: function(source){
    Class.extend(this, source)
  },
  // I may remove this.  Listen is a lot simpler.
  fire: function(method){
    var parameters = Array.prototype.slice.call(arguments);
    parameters = parameters.slice(1, parameters.length);
    
    function fire_method(method, type, target, parameters){
      if(!type.properties.hasOwnProperty(method))
        return;
      
      if(type.parent)
        fire_method(method, type.parent, target, parameters);
      
      
      type.properties[method].apply(target, parameters);
    }
        
    fire_method(method, this.type, this, parameters);
  },
  events: {},
  listen: function(other, name, method){
    if (other.events[name] == null)
      other.events[name] = [];
    
    other.events[name].push({
      method: method,
      listener: this
    });    
  },  
  unlisten: function(other, name) {
    if (other.events[name] == null)
      return;
    
    var list = other.events[name];
    for (var i = list.length - 1; i >= 0; --i) {
      if (list[i].listener === this) {
        list.splice(i, 1);  
      }
    }  
    
    if (list.length == 0) {
      delete other.events[name];
    }
  },
  invoke: function(name){
    if (!this.events[name])
      return;
    
    var args = Array.prototype.slice.call(arguments);
    args.shift();  // Remove 'name' from arguments  
    this.events[name].forEach(function(info){
      info.method.apply(info.listener, args);
    })
  }
};

function Block(name) {
  this.name = name;
  Block.library[name] = this;
}

Block.library = {}
Block.default_extension = '.html';
Block.source_path = "";

Block.load = function (name) {
  var block = new Block(name);
  jQuery.get(Block.source_path + "/" + name + Block.default_extension, function(data) {
    block.html = data;
  });
}

Block.generate = function(name, data) {
  return Block.library[name].generate(data);
}

Block.generate_query = function(name, data) {
  return Block.library[name].generate_query(data);
}

Block.prototype = {
  constructor: Block,
  name: '',
  html: '', 
  
  on_add: function(){},
  generate: function(data) {
    return Element.create(this.generate_query(data), data);
  },
  generate_query: function(data, owner) {
    var output = this.html;
    
    output = output.replace(/@{([\W\w]*?)}(?=\s*(?:<|"))/gm, function(all, code){
      var result = eval(code);
      if(typeof result === "undefined" || result == null)
        return '';
      
      if(result.hasOwnProperty('element')){
        if(owner)
          owner.children.push(result);
        
        return result.element.html();
      }
      
      return result;
    });
    
    //   output = output.replace(/{([^<]*?)}(?=\s*[<$])/m, '');
    
    var result = $(output);

    if (this.on_add)
      this.on_add(result, data);
    return result;
  }
}

var Element = Class.sub_class('Element', {
  children: [],
  initialize: function(source, data) {
        
    // If class defines a certain block to use, 
    // class initialization doesn't pass any jQuery object or html text

    if (this.block) {
      if (data == null)
        data = source;
      this.render(this.block, data);
    }    
    else 
    {
      if (source == null)
        return;
      
      if (source.hasOwnProperty('jquery'))
        this.element = source;  
      else
        this.element = jQuery(source);
      
      this.extend(data);
    }  
  },
  render: function(block_name, data){
    this.extend(data);
    this.element = Block.generate_query(block_name, this);
  },
  bind: function(event, method){
    var self = this;

    this.element.bind(event, function(){
      method.apply(self, arguments);
    })
  },
  bind_child: function(query, event, method){
    var self = this;
    this.element.find(query).bind(event, function(){
      method.apply(self, arguments);
    })
  },
  get_data: function(){
    var args = Array.prototype.slice.call(arguments);
    var method = args.pop();
    jQuery.get(args, function(){
      var args = Array.prototype.slice.call(arguments);
      args.push('json');
      method.apply(this, args);
    });
  },
  post_data: function(){
    var args = Array.prototype.slice.call(arguments);
    var method = args.pop();
    jQuery.get(args, function(){
      var args = Array.prototype.slice.call(arguments);
      args.push('json');
      method.apply(this, args);
    });
  }
});

var Node = Element.sub_class('Node', {
  initialize: function() {
    this.nid = this.element.attr('nid');
    this.type = this.element.attr('type');    
  }
});

var List = Element.sub_class('List', {
  add_button: function(html, click){
    var row = $('<li>' + html + '</li>');
    this.element.append(row); 
    if (click)
      row.find('a').click(click);
  },
  empty: function(){
    this.children.length = 0;
    this.element.empty();
  },
  append:function(item){
    var line = jQuery('<li></li>');
    line.append(item.element);
    this.element.append(line);
    this.children.push(item);
    item.parent = this;
    return item;
  }
});

var Dialog = Element.sub_class('Dialog', {
  active: false,
  width: 340,
  height: 500,
  title: 'Dialog',
  on_close: function(){},
  initialize: function(title, nid){
    if(title)
      this.title = title;
    
    this.nid = nid;
  },
  show: function(){
    var block = this;
    if (block.element.parent().length == 0)
      jQuery('body').append(block.element);
    
    block.dialog = block.element.dialog({
      title: block.title,
      width: block.width,
      height: block.height,
      modal: true,
      close: function(){
        block.element.remove();              
        $(window).unbind();
        block.on_close();
      }     
    });
  },
  update: function() {    
    var block = this;
    jQuery.get(this.query(), function(data) {
      block.fire('on_update', data);

      if (!block.active) {
        block.show();
                      
        $(window).keydown(function(event){
          if(event.keyCode == 13) {
            event.preventDefault();
            return false;
          }
        });
        block.active = true;
      }        
    }, 'json');
  },
  on_update: function(data){},
  query: function(){}
});

var Paged_Dialog = Dialog.sub_class('Paged_Dialog', {
  page: 0,
  rows:10,
  page_size: 5,
  initialize: function() {
    var block = this;
    
    this.element.find('.previous').click(function(){
      if (block.page > 0) {
        --block.page;
        block.update();
      }        
    });
    
    this.element.find('.next').click(function(){
      if (block.page < Math.round(block.rows / block.page_size)) {
        ++block.page;
        block.update();
      }        
    });    
    this.element.find('.filter').change(function(){      
      block.update();
    });
    
    this.element.find('.filter').keyup(function(e) {
      if(e.keyCode == 13){
        e.preventDefault();
        block.update();     
      }
    });    

  },
  on_update: function(data){
    this.rows = data.total;
  }
});

var Upload_Dialog = Dialog.sub_class('Upload_Dialog', {
  block: 'upload-dialog',
  height:200,
  initialize: function(){
    var dialog = this;
    this.element.find('#upload_button').click(function(e) {
      e.preventDefault();
      var block = $(this).parent();
      block.find('#file_control').upload('/retrax_upload_file', {
        nid: page_nid,
        title: block.find('#document_title').val()
      }, function(response) {
        //     dialog.close();
        window.location.reload();
        System.send_messages(response);
      }, 'html');
    });
  }
});

function Form(block_id) {
  return Block.library[block_id].html;
//  return Block.generate(block_id);
}

Form.radio = function(name, options, value) {
  var text = '<select name="' + name + '">';
  for(key in options) {
    text += '<option ';
    if(key == value)
      text += 'selected="selected" ';
    text += 'value="' + key + '">' + options[key] + '</option>';
  }
  text += '</select>';
  return text;
}

System = { 
  send_message: function(){},
  send_messages: function(response){
    if(response.message){
      System.send_message(response.message);
    }
    if(response.messages){
      var message = '';
      for(var index in response.messages){
        message += response.messages[index][0] + '<br/>';
      }

      System.send_message(message);
    }
  },
  post: function(url, data, method) {
    if (!method) {
      method = data;
      data = null;
    }
  
    jQuery.post(url, data, function(response){
      if(!response.result) {
        System.send_message('There was a problem communicating with the server.');
        return;
      }
      if(response.result.toLowerCase() == 'success') {
        if(method)
          method(response);
      }      
      
      System.send_messages(response);
    });
    
  }
}<commit_msg>Figured out a way to create namespaces that isn't stupid.  It's actually pretty cool.<commit_after>/*
 * Bloom v1.1.0
 * Javascript UI Library
 * <NAME>
 * Copyright 2012 <NAME>
 */

Bloom = {};

(function(Bloom){
  Class = {};
  Bloom.Class = Class;

  Class.extend = function(destination, source) {
    if (typeof source == 'object' || typeof source == 'function') {
      for (var k in source) {
        if (source.hasOwnProperty(k)) {
          destination[k] = source[k];
        }
      }
    }
    return destination;
  }

  Class.extend_methods = function(destination, source) {
    if (typeof source == 'object' || typeof source == 'function') {
      for (var k in source) {
        if (source.hasOwnProperty(k) && jQuery.isFunction(source[k])) {
          destination[k] = source[k];
        }
      }
    }
    return destination;
  }
  
  Class.sub_class = function (name, data) {
    var result = {};      
    
    Class.extend_methods(result, Class);
    result.name = name;
    result.parent = this;          
    result.properties = data;
    Bloom[name] = result;
  
    return result;
  }

  Class.initialize_properties = function(object){
    if (this.parent)
      this.parent.initialize_properties(object)
  
    Class.extend(object, this.properties);

  }

  Class.initialize_methods = function(object, args){
    if (this.parent) {
      var result = this.parent.initialize_methods(object, args);
      if(result)
        args = result;
    }
  
    if (this.properties.hasOwnProperty('initialize'))
      return this.properties.initialize.apply(object, args);  
  }

  Class.create = function(){
    var result = {};
    this.initialize_properties(result);
    result.type = this;
    var parameters = Array.prototype.slice.call(arguments);
    this.initialize_methods(result, parameters);
    return result;
  }

  Class.name = 'Class';
  Class.parent = null;
  Class.properties = {
    extend: function(source){
      Class.extend(this, source)
    },
    // I may remove this.  Listen is a lot simpler.
    fire: function(method){
      var parameters = Array.prototype.slice.call(arguments);
      parameters = parameters.slice(1, parameters.length);
    
      function fire_method(method, type, target, parameters){
        if(!type.properties.hasOwnProperty(method))
          return;
      
        if(type.parent)
          fire_method(method, type.parent, target, parameters);
      
      
        type.properties[method].apply(target, parameters);
      }
        
      fire_method(method, this.type, this, parameters);
    },
    events: {},
    listen: function(other, name, method){
      if (other.events[name] == null)
        other.events[name] = [];
    
      other.events[name].push({
        method: method,
        listener: this
      });    
    },  
    unlisten: function(other, name) {
      if (other.events[name] == null)
        return;
    
      var list = other.events[name];
      for (var i = list.length - 1; i >= 0; --i) {
        if (list[i].listener === this) {
          list.splice(i, 1);  
        }
      }  
    
      if (list.length == 0) {
        delete other.events[name];
      }
    },
    invoke: function(name){
      if (!this.events[name])
        return;
    
      var args = Array.prototype.slice.call(arguments);
      args.shift();  // Remove 'name' from arguments  
      this.events[name].forEach(function(info){
        info.method.apply(info.listener, args);
      })
    }
  };

  function Block(name) {
    this.name = name;
    Block.library[name] = this;
  }
  Bloom.Block = Block;

  Block.library = {}
  Block.default_extension = '.html';
  Block.source_path = "";

  Block.load = function (name) {
    var block = new Block(name);
    jQuery.get(Block.source_path + "/" + name + Block.default_extension, function(data) {
      block.html = data;
    });
  }

  Block.generate = function(name, data) {
    return Block.library[name].generate(data);
  }

  Block.generate_query = function(name, data) {
    return Block.library[name].generate_query(data);
  }

  Block.prototype = {
    constructor: Block,
    name: '',
    html: '', 
  
    on_add: function(){},
    generate: function(data) {
      return Element.create(this.generate_query(data), data);
    },
    generate_query: function(data, owner) {
      var output = this.html;
    
      output = output.replace(/@{([\W\w]*?)}(?=\s*(?:<|"))/gm, function(all, code){
        var result = eval(code);
        if(typeof result === "undefined" || result == null)
          return '';
      
        if(result.hasOwnProperty('element')){
          if(owner)
            owner.children.push(result);
        
          return result.element.html();
        }
      
        return result;
      });
    
      //   output = output.replace(/{([^<]*?)}(?=\s*[<$])/m, '');
    
      var result = $(output);

      if (this.on_add)
        this.on_add(result, data);
      return result;
    }
  }

  var Element = Class.sub_class('Element', {
    children: [],
    initialize: function(source, data) {
        
      // If class defines a certain block to use, 
      // class initialization doesn't pass any jQuery object or html text

      if (this.block) {
        if (data == null)
          data = source;
        this.render(this.block, data);
      }    
      else 
      {
        if (source == null)
          return;
      
        if (source.hasOwnProperty('jquery'))
          this.element = source;  
        else
          this.element = jQuery(source);
      
        this.extend(data);
      }  
    },
    render: function(block_name, data){
      this.extend(data);
      this.element = Block.generate_query(block_name, this);
    },
    bind: function(event, method){
      var self = this;

      this.element.bind(event, function(){
        method.apply(self, arguments);
      })
    },
    bind_child: function(query, event, method){
      var self = this;
      this.element.find(query).bind(event, function(){
        method.apply(self, arguments);
      })
    },
    get_data: function(){
      var args = Array.prototype.slice.call(arguments);
      var method = args.pop();
      jQuery.get(args, function(){
        var args = Array.prototype.slice.call(arguments);
        args.push('json');
        method.apply(this, args);
      });
    },
    post_data: function(){
      var args = Array.prototype.slice.call(arguments);
      var method = args.pop();
      jQuery.get(args, function(){
        var args = Array.prototype.slice.call(arguments);
        args.push('json');
        method.apply(this, args);
      });
    }
  });

  var Node = Element.sub_class('Node', {
    initialize: function() {
      this.nid = this.element.attr('nid');
      this.type = this.element.attr('type');    
    }
  });

  var List = Element.sub_class('List', {
    add_button: function(html, click){
      var row = $('<li>' + html + '</li>');
      this.element.append(row); 
      if (click)
        row.find('a').click(click);
    },
    empty: function(){
      this.children.length = 0;
      this.element.empty();
    },
    append:function(item){
      var line = jQuery('<li></li>');
      line.append(item.element);
      this.element.append(line);
      this.children.push(item);
      item.parent = this;
      return item;
    }
  });

  var Dialog = Element.sub_class('Dialog', {
    active: false,
    width: 340,
    height: 500,
    title: 'Dialog',
    on_close: function(){},
    initialize: function(title, nid){
      if(title)
        this.title = title;
    
      this.nid = nid;
    },
    show: function(){
      var block = this;
      if (block.element.parent().length == 0)
        jQuery('body').append(block.element);
    
      block.dialog = block.element.dialog({
        title: block.title,
        width: block.width,
        height: block.height,
        modal: true,
        close: function(){
          block.element.remove();              
          $(window).unbind();
          block.on_close();
        }     
      });
    },
    update: function() {    
      var block = this;
      jQuery.get(this.query(), function(data) {
        block.fire('on_update', data);

        if (!block.active) {
          block.show();
                      
          $(window).keydown(function(event){
            if(event.keyCode == 13) {
              event.preventDefault();
              return false;
            }
          });
          block.active = true;
        }        
      }, 'json');
    },
    on_update: function(data){},
    query: function(){}
  });

  var Paged_Dialog = Dialog.sub_class('Paged_Dialog', {
    page: 0,
    rows:10,
    page_size: 5,
    initialize: function() {
      var block = this;
    
      this.element.find('.previous').click(function(){
        if (block.page > 0) {
          --block.page;
          block.update();
        }        
      });
    
      this.element.find('.next').click(function(){
        if (block.page < Math.round(block.rows / block.page_size)) {
          ++block.page;
          block.update();
        }        
      });    
      this.element.find('.filter').change(function(){      
        block.update();
      });
    
      this.element.find('.filter').keyup(function(e) {
        if(e.keyCode == 13){
          e.preventDefault();
          block.update();     
        }
      });    

    },
    on_update: function(data){
      this.rows = data.total;
    }
  });

  var Upload_Dialog = Dialog.sub_class('Upload_Dialog', {
    block: 'upload-dialog',
    height:200,
    initialize: function(){
      var dialog = this;
      this.element.find('#upload_button').click(function(e) {
        e.preventDefault();
        var block = $(this).parent();
        block.find('#file_control').upload('/retrax_upload_file', {
          nid: page_nid,
          title: block.find('#document_title').val()
        }, function(response) {
          //     dialog.close();
          window.location.reload();
          System.send_messages(response);
        }, 'html');
      });
    }
  });

  function Form(block_id) {
    return Block.library[block_id].html;
  //  return Block.generate(block_id);
  }
  Bloom.Form = Form;

  Form.radio = function(name, options, value) {
    var text = '<select name="' + name + '">';
    for(key in options) {
      text += '<option ';
      if(key == value)
        text += 'selected="selected" ';
      text += 'value="' + key + '">' + options[key] + '</option>';
    }
    text += '</select>';
    return text;
  }

  System = { 
    send_message: function(){},
    send_messages: function(response){
      if(response.message){
        System.send_message(response.message);
      }
      if(response.messages){
        var message = '';
        for(var index in response.messages){
          message += response.messages[index][0] + '<br/>';
        }

        System.send_message(message);
      }
    },
    post: function(url, data, method) {
      if (!method) {
        method = data;
        data = null;
      }
  
      jQuery.post(url, data, function(response){
        if(!response.result) {
          System.send_message('There was a problem communicating with the server.');
          return;
        }
        if(response.result.toLowerCase() == 'success') {
          if(method)
            method(response);
        }      
      
        System.send_messages(response);
      });
    
    }
  }
  Bloom.System = System;

  // I want to keep Bloom open for working in systems outside of the browser, just in case.
  // As a rule of thumb, use Global() to refer to global variables, and
  // use window to refer to window specific members.
  Global = function() {
    return window;
  }
  Bloom.Global = Global;

  Bloom.import_all = function(){
    Class.extend(Global(), Bloom);
    delete Global().extend;
  }
})(Bloom);

{'fraction_non_alphanumeric': 0.15516160802220433, 'fraction_numerical': 0.0029546065001343, 'mean_word_length': 2.2151698330454805, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4839489', 'n_tokens_mistral': 6267, 'n_tokens_neox': 6830, 'n_words': 1378}