angular.module('app.main', [])

.controller('MainController', function($scope, Snippets) {

  // $scope.snippets = [
  //   'some collection  of snippets',
  //   'preexisting in database',
  //   'searchable/filterable',
  //   'paginated probably'
  // ];

  $scope.snippets = [];
  $scope.highlights = {};

  var editors = [];
  var Range = ace.require('ace/range').Range;

  $scope.init = function() {
    Snippets.retrieveSnippets()
      .then(function(snippets) {
        // set snippets data
        $scope.snippets = snippets.data;

        // set annotations/highlights
        // $scope.highlights[snippets.data._id] = snippets.data.highlights;
        // console.log('highlights', snippets.data);
      });
  }();

  // $scope.$watch('snippets', function(snippets) {
  //   if (snippets.length === 0) { return; }

  //   console.log('watch');
  //   console.log(snippets);

  //   snippets.forEach(function(snippet) {
  //     editors[snippet._id] = ace.edit('editor-' + snippet._id);
  //     editors[snippet._id].setTheme('ace/theme/solarized_dark');
  //     editors[snippet._id].session.setMode('ace/mode/javascript');
  //   });
  // });

  $scope.$on('ngRepeatFinished', function(ngRepeatFinishedEvent) {
    $scope.snippets.forEach(function(snippet) {
      // setup ace editor
      editors[snippet._id] = ace.edit('editor-' + snippet._id);
      editors[snippet._id].setTheme('ace/theme/solarized_dark');
      editors[snippet._id].session.setMode('ace/mode/javascript');

      // stop the browser's warning message
      editors[snippet._id].$blockScrolling = Infinity;

      // make editor read only
      editors[snippet._id].setReadOnly(true);

      // add code to editor
      editors[snippet._id].setValue(snippet.code);

      // setup highlights/annotations array
      $scope.highlights[snippet._id] = [];

      // add highlights to code
      snippet.highlights.forEach(function(highlight) {
        // set markers for highlighted code
        var range = new Range(highlight.start.row, highlight.start.column, highlight.end.row, highlight.end.column);
        editors[snippet._id].session.addMarker(range, "highlighter-" + highlight.color);

        // set annotations
        $scope.highlights[snippet._id].push(highlight);
      });
    });
  });

  // $scope.renderEditor = function(id) {
  //   console.log('renderEditor', id);
  //   editors[id] = ace.edit('editor-' + id);
  //   editors[id].setTheme('ace/theme/solarized_dark');
  //   editors[id].session.setMode('ace/mode/javascript');
  // };

})

@@ -88,176 +88,8 @@
 ) %7B%0A
-%0A  // $scope.snippets = %5B%0A  //   'some collection  of snippets',%0A  //   'preexisting in database',%0A  //   'searchable/filterable',%0A  //   'paginated probably'%0A  // %5D;%0A%0A
   $s
@@ -370,619 +370,22 @@
 ta;%0A
-%0A        // set annotations/highlights%0A        // $scope.highlights%5Bsnippets.data._id%5D = snippets.data.highlights;%0A        // console.log('highlights', snippets.data);%0A      %7D);%0A  %7D();%0A%0A  // $scope.$watch('snippets', function(snippets) %7B%0A  //   if (snippets.length === 0) %7B return; %7D%0A%0A  //   console.log('watch');%0A  //   console.log(snippets);%0A%0A  //   snippets.forEach(function(snippet) %7B%0A  //     editors%5Bsnippet._id%5D = ace.edit('editor-' + snippet._id);%0A  //     editors%5Bsnippet._id%5D.setTheme('ace/theme/solarized_dark');%0A  //     editors%5Bsnippet._id%5D.session.setMode('ace/mode/javascript');%0A  //   %7D);%0A  // %7D
+      %7D);%0A  %7D(
 );%0A%0A
@@ -1517,262 +1517,8 @@
 %7D);%0A
-%0A  // $scope.renderEditor = function(id) %7B%0A  //   console.log('renderEditor', id);%0A  //   editors%5Bid%5D = ace.edit('editor-' + id);%0A  //   editors%5Bid%5D.setTheme('ace/theme/solarized_dark');%0A  //   editors%5Bid%5D.session.setMode('ace/mode/javascript');%0A  // %7D;%0A%0A
 %7D)
+;
 %0A

Remove commented out code