<commit_before>//
// TODO - all of this needs refactoring
//

import * as core     from '../core'
import * as app      from './app'
import * as charts   from '../charts/core'
import Client        from '../client'
import Dashboard     from '../models/dashboard'
import DashboardItem from '../models/items/item'
import Container     from '../models/items/container'
import Query         from '../models/data/query'
import {transforms}  from '../models/transform/transform'

declare var $, URI, window, bootbox, ts

const log = core.logger('manager')

class DashboardHolder {
  url : string
  element : any
  dashboard : Dashboard

  constructor(url, element) {
    this.url = url
    this.element = element
    this.dashboard = null
  }

  setRange(from, until) {
    let url = new URI(this.url)
    if (from) {
      url.setQuery('from', from)
    }
    if (until) {
      url.setQuery('until', until)
    }
    this.url = url.href()
  }
}

export class Manager {
  current: any /* DashboardHolder */
  current_transform: any
  current_list: Dashboard[]
  client: Client

  constructor(client?: Client) {
    this.client = client ? client : new Client()
  }

  set_current(value) : Manager {
    this.current = value
    return this
  }

  find(href: string) : Dashboard {
    if (!this.current_list)
      return null
    return this.current_list.find(d => d.href === href)
  }

  /**
   * Register an event handler for processing a dashboard once it's
   * loaded and ready.
   */
  onDashboardLoaded(handler) : Manager {
    core.events.on(this, app.Event.DASHBOARD_LOADED, handler)
    return this
  }

  /**
   * List all dashboards.
   */
  list(path, handler) : void {
    this.client.dashboard_list({path: path})
      .then(handler)
      .catch((req, status, error) => {
        this.error(`Error listing dashboards. ${error}`)
      })
  }

  // Web Components. I want Web Components. TBD.
  render_dashboard_list(path, element, handler?) : void {
    let mgr = this
    let fn = (data) => {
      if (data && data.length) {
        this.current_list = data
        if (handler) {
          handler(data)
        }
        $(element).html(ts.templates.listing.dashboard_list({dashboards: data}))
        ts.user.list_favorites().forEach(function(d) {
          $('[data-ds-href="' + d.href + '"].ds-favorite-indicator').html('<i class="fa fa-lg fa-star"></i>')
          $('[data-ds-href="' + d.href + '"]').addClass('ds-favorited')
          $('tr[data-ds-href="' + d.href + '"]').addClass('active')
        })
      }
    }
    if (path instanceof Array) {
      fn(path)
    } else {
      this.list(path, fn)
    }
  }

  default_error_handler(xhr, status, error) : void {
    console.log(xhr)
    console.log(status)
    console.log(error)
  }

  error(message, options: any = {}) : void {
    options.type = options.type || 'danger'
    $.growl({
      message: message,
      title: 'Error',
      icon: 'fa fa-exclamation-circle'
    }, options)
  }

  warning(message, options: any = {}) : void {
    options.type = options.type || 'warning'
    $.growl({
      message: message,
      title: options.title || 'Warning',
      icon: options.icon || 'fa fa-exclamation-circle'
    }, options)
  }

  success(message, options: any = {}) : void {
    options.type = options.type || 'success'
    $.growl({
      message: message,
      title: options.title || 'Success',
      icon: options.icon || 'fa fa-check-circle'
    }, options)
  }

  /**
   * Set up us the API call.
   */
  _prep_url(base_url: string, options: any) : any {
    let url = new URI(base_url).setQuery('rendering', true)
    let context = app.context(url.query(true))

    context.from = options.from || context.from
    context.until = context.until || options.until

    url.setQuery('from', context.from)
    if (context.until) {
      url.setQuery('until', context.until)
    }

    context.url = url.href()

    if (typeof(options.interactive) != 'undefined') {
      context.interactive = options.interactive
    } else if (context.params.interactive) {
      context.interactive = context.params.interactive != 'false'
    }

    return context<commit_msg>Update growl calls to notify<commit_after>//
// TODO - all of this needs refactoring
//

import * as core     from '../core'
import * as app      from './app'
import * as charts   from '../charts/core'
import Client        from '../client'
import Dashboard     from '../models/dashboard'
import DashboardItem from '../models/items/item'
import Container     from '../models/items/container'
import Query         from '../models/data/query'
import {transforms}  from '../models/transform/transform'

declare var $, URI, window, bootbox, ts

const log = core.logger('manager')

class DashboardHolder {
  url : string
  element : any
  dashboard : Dashboard

  constructor(url, element) {
    this.url = url
    this.element = element
    this.dashboard = null
  }

  setRange(from, until) {
    let url = new URI(this.url)
    if (from) {
      url.setQuery('from', from)
    }
    if (until) {
      url.setQuery('until', until)
    }
    this.url = url.href()
  }
}

export class Manager {
  current: any /* DashboardHolder */
  current_transform: any
  current_list: Dashboard[]
  client: Client

  constructor(client?: Client) {
    this.client = client ? client : new Client()
  }

  set_current(value) : Manager {
    this.current = value
    return this
  }

  find(href: string) : Dashboard {
    if (!this.current_list)
      return null
    return this.current_list.find(d => d.href === href)
  }

  /**
   * Register an event handler for processing a dashboard once it's
   * loaded and ready.
   */
  onDashboardLoaded(handler) : Manager {
    core.events.on(this, app.Event.DASHBOARD_LOADED, handler)
    return this
  }

  /**
   * List all dashboards.
   */
  list(path, handler) : void {
    this.client.dashboard_list({path: path})
      .then(handler)
      .catch((req, status, error) => {
        this.error(`Error listing dashboards. ${error}`)
      })
  }

  // Web Components. I want Web Components. TBD.
  render_dashboard_list(path, element, handler?) : void {
    let mgr = this
    let fn = (data) => {
      if (data && data.length) {
        this.current_list = data
        if (handler) {
          handler(data)
        }
        $(element).html(ts.templates.listing.dashboard_list({dashboards: data}))
        ts.user.list_favorites().forEach(function(d) {
          $('[data-ds-href="' + d.href + '"].ds-favorite-indicator').html('<i class="fa fa-lg fa-star"></i>')
          $('[data-ds-href="' + d.href + '"]').addClass('ds-favorited')
          $('tr[data-ds-href="' + d.href + '"]').addClass('active')
        })
      }
    }
    if (path instanceof Array) {
      fn(path)
    } else {
      this.list(path, fn)
    }
  }

  default_error_handler(xhr, status, error) : void {
    console.log(xhr)
    console.log(status)
    console.log(error)
  }

  error(message, options: any = {}) : void {
    options.type = options.type || 'danger'
    $.notify({
      message: message,
      title: 'Error',
      icon: 'fa fa-exclamation-circle'
    }, options)
  }

  warning(message, options: any = {}) : void {
    options.type = options.type || 'warning'
    $.notify({
      message: message,
      title: options.title || 'Warning',
      icon: options.icon || 'fa fa-exclamation-circle'
    }, options)
  }

  success(message, options: any = {}) : void {
    options.type = options.type || 'success'
    $.notify({
      message: message,
      title: options.title || 'Success',
      icon: options.icon || 'fa fa-check-circle'
    }, options)
  }

  /**
   * Set up us the API call.
   */
  _prep_url(base_url: string, options: any) : any {
    let url = new URI(base_url).setQuery('rendering', true)
    let context = app.context(url.query(true))

    context.from = options.from || context.from
    context.until = context.until || options.until

    url.setQuery('from', context.from)
    if (context.until) {
      url.setQuery('until', context.until)
    }

    context.url = url.href()

    if (typeof(options.interactive) != 'undefined') {
      context.interactive = options.interactive
    } else if (context.params.interactive) {
      context.interactive = context.params.interactive != 'false'
    }

    return context
{'fraction_non_alphanumeric': 0.15020427781783224, 'fraction_numerical': 0.0, 'mean_word_length': 2.5311837081035216, 'pattern_counts': {'":': 0, '<': 7, '<?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': '4924309', 'n_tokens_mistral': 2681, 'n_tokens_neox': 2636, 'n_words': 627}