<commit_before>##
## Tag Autocomplete
##
## A directive for generating tag input with autocomplete support on text input
##
## Attributes
## - mac-tag
##

angular.module("Mac").directive "macTagAutocomplete", [
  "$parse",
  "$http",
  "keys",
  ($parse, $http, key) ->
    restrict:    "E"
    scope:       {}
    templateUrl: "template/tag_autocomplete.html"
    replace:     true

    compile: (element, attr) ->
      urlExp      = attr.macTagAutocompleteUrl
      modelExp    = attr.macTagAutocompleteLocalModel
      valueKey    = attr.macTagAutocompleteValue
      labelKey    = attr.macTagAutocompleteLabel
      selectedExp = attr.macTagAutocompleteSelected
      queryKey    = attr.macTagAutocompleteQuery      or "q"
      delay       = attr.macTagAutocompleteDelay      or 100

      getUrl      = $parse urlExp
      getSelected = $parse selectedExp

      textInput = $(".text-input", element)

      # Update template on label variable name
      tagLabelKey = if labelKey? then ".#{labelKey}" else ""
      $(".tag-label", element).text "{{tag#{tagLabelKey}}}"

      ($scope, element, attrs) ->
        # Getting autocomplete url from parent scope
        Object.defineProperty $scope, "autocompleteUrl",
          get:       -> getUrl $scope.$parent
          set: (url) -> getUrl.assign $scope.$parent, url

        $scope.removeTag = (tag) ->
          index = $scope.tags.indexOf tag
          $scope.tags[index..index] = []
          $scope.updateSelectedArrary()

        # function to update array on parent scope with tag value
        $scope.updateSelectedArrary = ->
          outputTags = _($scope.tags).map (item, i) -> if valueKey? then item[valueKey] else item
          getSelected.assign $scope.$parent, outputTags

        textInput.bind "keydown", (event) ->
          stroke = event.which or event.keyCode
          switch stroke
            when key.BACKSPACE
              if $(this).val().length is 0
                $scope.$apply ->
                  $scope.tags.pop()
                  $scope.updateSelectedArrary()
          return true

        textInput.autocomplete
          delay:     delay
          autoFocus: true
          source: (req, resp) ->
            options =
              method: "GET"
              url:    $scope.autocompleteUrl
              params: {}
            options.params[queryKey] = req.term

            $http(options)
              .success (data, status, headers, config) ->
                # get all selected values
                existingValues = _($scope.tags).pluck valueKey
                # remove selected tags on autocomplete dropdown
                list           = _(data.data).reject (item) -> (item[valueKey] or item) in existingValues
                # convert tags to jquery ui autocomplete format
                resp _(list).map (item) ->
                  label = value = if labelKey? then item[labelKey] else item
                  return {label, value}
                # store the current data for revert lookup
                $scope.currentAutocomplete = data.data

          select: (event, ui) ->
            $scope.$apply ->
              item = _($scope.currentAutocomplete).find (item) -> item[labelKey] is ui.item.label
              $scope.tags.push item
              $scope.updateSelectedArrary()

            setTimeout (->
              textInput.val ""
            ), 0

        $scope.reset = ->
          $scope.tags                = []
          $scope.currentAutocomplete = []

        $scope.reset()
]
<commit_msg>Updated tag autocomplete Added default value to label and value. Increased default delay to 800. Updated comments and added dependencies.<commit_after>##
## Tag Autocomplete
##
## A directive for generating tag input with autocomplete support on text input
##
## Dependencies
## - jQuery UI autocomplete
##
## Attributes
## - mac-tag-autocomplete-url:      url to fetch autocomplete dropdown list data
## - mac-tag-autocomplete-value:    the value to be sent back upon selection
## - mac-tag-autocomplete-label:    the label to display to the users
## - mac-tag-autocomplete-selected: the list of elements selected by the user
## - mac-tag-autocomplete-query:    the query parameter on GET command
## - mac-tag-autocomplete-delay:    time delayed on fetching autocomplete data after keyup
##

angular.module("Mac").directive "macTagAutocomplete", [
  "$parse",
  "$http",
  "keys",
  ($parse, $http, key) ->
    restrict:    "E"
    scope:       {}
    templateUrl: "template/tag_autocomplete.html"
    replace:     true

    compile: (element, attr) ->
      urlExp      = attr.macTagAutocompleteUrl
      valueKey    = attr.macTagAutocompleteValue      or "id"
      labelKey    = attr.macTagAutocompleteLabel      or "name"
      selectedExp = attr.macTagAutocompleteSelected
      queryKey    = attr.macTagAutocompleteQuery      or "q"
      delay       = attr.macTagAutocompleteDelay      or 800

      getUrl      = $parse urlExp
      getSelected = $parse selectedExp

      textInput = $(".text-input", element)

      # Update template on label variable name
      tagLabelKey = if labelKey? then ".#{labelKey}" else ""
      $(".tag-label", element).text "{{tag#{tagLabelKey}}}"

      ($scope, element, attrs) ->
        # Getting autocomplete url from parent scope
        Object.defineProperty $scope, "autocompleteUrl",
          get:       -> getUrl $scope.$parent
          set: (url) -> getUrl.assign $scope.$parent, url

        $scope.removeTag = (tag) ->
          index = $scope.tags.indexOf tag
          $scope.tags[index..index] = []
          $scope.updateSelectedArrary()

        # function to update array on parent scope with tag value
        $scope.updateSelectedArrary = ->
          outputTags = _($scope.tags).map (item, i) -> if valueKey? then item[valueKey] else item
          getSelected.assign $scope.$parent, outputTags

        textInput.bind "keydown", (event) ->
          stroke = event.which or event.keyCode
          switch stroke
            when key.BACKSPACE
              if $(this).val().length is 0
                $scope.$apply ->
                  $scope.tags.pop()
                  $scope.updateSelectedArrary()
          return true

        textInput.autocomplete
          delay:     delay
          autoFocus: true
          source: (req, resp) ->
            options =
              method: "GET"
              url:    $scope.autocompleteUrl
              params: {}
            options.params[queryKey] = req.term

            $http(options)
              .success (data, status, headers, config) ->
                # get all selected values
                existingValues = _($scope.tags).pluck valueKey
                # remove selected tags on autocomplete dropdown
                list           = _(data.data).reject (item) -> (item[valueKey] or item) in existingValues
                # convert tags to jquery ui autocomplete format
                resp _(list).map (item) ->
                  label = value = if labelKey? then item[labelKey] else item
                  return {label, value}
                # store the current data for revert lookup
                $scope.currentAutocomplete = data.data

          select: (event, ui) ->
            $scope.$apply ->
              item = _($scope.currentAutocomplete).find (item) -> item[labelKey] is ui.item.label
              $scope.tags.push item
              $scope.updateSelectedArrary()

            setTimeout (->
              textInput.val ""
            ), 0

        $scope.reset = ->
          $scope.tags                = []
          $scope.currentAutocomplete = []

        $scope.reset()
]

{'fraction_non_alphanumeric': 0.10443864229765012, 'fraction_numerical': 0.0016971279373368146, 'mean_word_length': 1.8553857622064853, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 41, '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': '2856443', 'n_tokens_mistral': 2083, 'n_tokens_neox': 1968, 'n_words': 556}