<commit_before>define ['angular', 'lodash', 'app', 'imjs'], (ng, L, {filters}, {Service}) ->

  filters.register 'templateTitle', -> ({title}) ->
    title.replace(/-->/g, '\u21E8').replace(/<--/g, '\u21E6')

  injectables = ['$scope', '$log', '$timeout', '$q', '$filter', 'Mines', 'ClassUtils']

  validPath = (model, path) ->
    try
      model.makePath path
      path
    catch e
      false

  isBetween = (model, inputType, outputType) ->
    inputType = validPath model, inputType?.className
    outputType = validPath model, outputType?.className
    (template) ->
      ok = true
      if inputType
        ok and= L.some template.constraints, ({editable, path}) ->
          path = template.makePath(path)
          if path.isAttribute()
            path = path.getParent()
          editable and path.isa(inputType)
      if outputType
        ok and= L.some template.views, (path) ->
          path = template.makePath(path).getParent()
          path.isa(outputType)
      ok

  templateData = ({constraints, views, title, name, description, constraintLogic}) ->
    {constraints, views, title, name, description, constraintLogic}

  filterTemplates = ({templates, model, inputType, outputType}) ->
    if templates and model
      f = isBetween model, inputType, outputType
      (templateData t for t in templates when f t) # Need to json-ify data for presentation.

  return Array injectables..., (scope, log, timeout, Q, filters, Mines, ClassUtils) ->
    scope.defaults = {}

    setTemplates = ({query}) -> (ts) ->
      Q.all(query t for _, t of ts).then (qs) -> timeout ->
        scope.templates = qs
        scope.filteredTemplates = filterTemplates scope

    scope.$on 'reset', (evt) ->
      scope.outputType = scope.defaults.outputType
      scope.inputType = scope.defaults.inputType
      scope.templateFilter = null

    scope.runQuery = (q) ->
      scope.$emit 'start-history',
        verb:
          ed: "ran"
          ing: "running"
        thing: "#{ filters('templateTitle')(q) } template query"
        tool: 'show-table',
        data:
          url: scope.connection.root,
          token: scope.connection.token,
          query: q

    scope.classes = []
    scope.inputType = scope.outputType = scope.serviceName = ''

    fetchingDefaultMine = Mines.get 'default'

    fetchingDefaultMine.then ({ident}) -> timeout -> scope.serviceName = ident

    connecting = fetchingDefaultMine.then Service.connect

    connecting.then (connection) -> scope.connection = connection

    connecting.then (connection) -> connection.fetchModel().then ClassUtils.setClasses scope

    connecting.then (connection) -> connection.fetchTemplates().then setTemplates connection

    updateTemplates = -> scope.filteredTemplates = filterTemplates scope
    typeWatcher = ({inputType, outputType}) -> inputType?.className + outputType?.className

    scope.$watch typeWatcher, updateTemplates

<commit_msg>Prefer constraints to where, and use service convention<commit_after>define ['angular', 'lodash', 'app', 'imjs'], (ng, L, {filters}, {Service}) ->

  filters.register 'templateTitle', -> ({title}) ->
    title.replace(/-->/g, '\u21E8').replace(/<--/g, '\u21E6')

  injectables = ['$scope', '$log', '$timeout', '$q', '$filter', 'Mines', 'ClassUtils']

  validPath = (model, path) ->
    try
      model.makePath path
      path
    catch e
      false

  isBetween = (model, inputType, outputType) ->
    inputType = validPath model, inputType?.className
    outputType = validPath model, outputType?.className
    (template) ->
      ok = true
      if inputType
        ok and= L.some template.constraints, ({editable, path}) ->
          path = template.makePath(path)
          if path.isAttribute()
            path = path.getParent()
          editable and path.isa(inputType)
      if outputType
        ok and= L.some template.views, (path) ->
          path = template.makePath(path).getParent()
          path.isa(outputType)
      ok

  templateData = ({constraints, views, title, name, description, constraintLogic}) ->
    {constraints, views, title, name, description, constraintLogic}

  filterTemplates = ({templates, model, inputType, outputType}) ->
    if templates and model
      f = isBetween model, inputType, outputType
      (templateData t for t in templates when f t) # Need to json-ify data for presentation.

  return Array injectables..., (scope, log, timeout, Q, filters, Mines, ClassUtils) ->
    scope.defaults = {}

    setTemplates = ({query}) -> (ts) ->
      Q.all(query t for _, t of ts).then (qs) -> timeout ->
        scope.templates = qs
        scope.filteredTemplates = filterTemplates scope

    scope.$on 'reset', (evt) ->
      scope.outputType = scope.defaults.outputType
      scope.inputType = scope.defaults.inputType
      scope.templateFilter = null

    scope.runQuery = (q) ->
      scope.$emit 'start-history',
        verb:
          ed: "ran"
          ing: "running"
        thing: "#{ filters('templateTitle')(q) } template query"
        tool: 'show-table',
        data:
          service:
            root: scope.connection.root,
          query: q

    scope.classes = []
    scope.inputType = scope.outputType = scope.serviceName = ''

    fetchingDefaultMine = Mines.get 'default'

    fetchingDefaultMine.then ({ident}) -> timeout -> scope.serviceName = ident

    connecting = fetchingDefaultMine.then Service.connect

    connecting.then (connection) -> scope.connection = connection

    connecting.then (connection) -> connection.fetchModel().then ClassUtils.setClasses scope

    connecting.then (connection) -> connection.fetchTemplates().then setTemplates connection

    updateTemplates = -> scope.filteredTemplates = filterTemplates scope
    typeWatcher = ({inputType, outputType}) -> inputType?.className + outputType?.className

    scope.$watch typeWatcher, updateTemplates


{'fraction_non_alphanumeric': 0.13054563997960225, 'fraction_numerical': 0.002039775624681285, 'mean_word_length': 2.8282368249837346, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 53, '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': '5440507', 'n_tokens_mistral': 1686, 'n_tokens_neox': 1642, 'n_words': 440}