<commit_before>angular.module('ldEditor').service 'angularTemplateService', ($rootScope, $compile, mapMediatorService) ->

  # Service

  isAngularTemplate: (snippetModel) ->
    $template = snippetModel.template.$template
    typeof $template.attr('data-template') != "undefined"


  insertAngularTemplate: (snippetModel) ->
    snippetView = doc.document.renderer.snippetViews[snippetModel.id]
    for node in snippetView.$html.find('*[data-is]')
      switch $(node).data('is')
        when 'leaflet-map'
          @loadTemplate($(node), $.proxy(@insertTemplateInstance, this,
            snippetModel, $(node), new Map
              id: snippetModel.id
              mapMediatorService: mapMediatorService
          ))
        when 'd3-choropleth'
          @loadTemplate($(node), $.proxy(@insertTemplateInstance, this,
            snippetModel, $(node), new ChoroplethMap
              id: snippetModel.id
              mapMediatorService: mapMediatorService
            ))
        else
          alert("unknown template value #{$(node).data('is')}")


  loadTemplate: ($node, callback) ->
    dependency = $node.data('dependency')
    resourceList = $node.data('dependency-resources')
    if dependency
      yepnope [{
        test: window.hasOwnProperty(dependency)
        nope: resourceList.split(';')
        complete: =>
          callback()
      }]
    else
      callback()


  insertTemplateInstance: (snippetModel, $directiveRoot, instance) ->
    instanceScope = $rootScope.$new()
    $compile(instance.getTemplate())(instanceScope, (instanceHtml, childScope) =>
      mapMediatorService.set(snippetModel.id, snippetModel, instance, childScope)
      childScope.mapId = snippetModel.id
      $directiveRoot.html(instanceHtml)
    )
    $rootScope.$$phase || instanceScope.$digest() # digest immediately to get maps loading


  removeAngularTemplate: (snippetModel) ->
    scope = mapMediatorService.getScope(snippetModel.id)
    scope.$destroy()
    mapMediatorService.reset(snippetModel.id)<commit_msg>add a yepnope loading queue<commit_after>angular.module('ldEditor').service 'angularTemplateService', ($rootScope, $compile, mapMediatorService) ->

  yepnopeQueue = {}

  # Service

  isAngularTemplate: (snippetModel) ->
    $template = snippetModel.template.$template
    typeof $template.attr('data-template') != "undefined"


  insertAngularTemplate: (snippetModel) ->
    snippetView = doc.document.renderer.snippetViews[snippetModel.id]
    for node in snippetView.$html.find('*[data-is]')
      switch $(node).data('is')
        when 'leaflet-map'
          @loadTemplate($(node), $.proxy(@insertTemplateInstance, this,
            snippetModel, $(node), new Map
              id: snippetModel.id
              mapMediatorService: mapMediatorService
          ))
        when 'd3-choropleth'
          @loadTemplate($(node), $.proxy(@insertTemplateInstance, this,
            snippetModel, $(node), new ChoroplethMap
              id: snippetModel.id
              mapMediatorService: mapMediatorService
            ))
        else
          alert("unknown template value #{$(node).data('is')}")


  loadTemplate: ($node, callback) ->
    dependency = $node.data('dependency')
    resourceList = $node.data('dependency-resources')
    if dependency
      cb = yepnopeQueue[dependency]
      unless cb
        cb = $.Callbacks()
        yepnopeQueue[dependency] = cb
      cb.add(callback)

      yepnope [{
        test: window.hasOwnProperty(dependency)
        nope: resourceList.split(';')
        complete: =>
          yepnopeQueue[dependency].fire()
      }]
    else
      callback()


  insertTemplateInstance: (snippetModel, $directiveRoot, instance) ->
    instanceScope = $rootScope.$new()
    $compile(instance.getTemplate())(instanceScope, (instanceHtml, childScope) =>
      mapMediatorService.set(snippetModel.id, snippetModel, instance, childScope)
      childScope.mapId = snippetModel.id
      $directiveRoot.html(instanceHtml)
    )
    $rootScope.$$phase || instanceScope.$digest() # digest immediately to get maps loading


  removeAngularTemplate: (snippetModel) ->
    scope = mapMediatorService.getScope(snippetModel.id)
    scope.$destroy()
    mapMediatorService.reset(snippetModel.id)
{'fraction_non_alphanumeric': 0.12408586930879925, 'fraction_numerical': 0.00047180938900684123, 'mean_word_length': 3.1125121241513094, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, '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': '2397737', 'n_tokens_mistral': 1234, 'n_tokens_neox': 1173, 'n_words': 217}