/** @scratch /panels/5

*
* include::panels/text.asciidoc[]
*/

/** @scratch /panels/text/0

* == text
* Status: *Stable*
*
* The text panel is used for displaying static text formated as markdown, sanitized html or as plain
* text.
*
*/

define([

'angular',
'app',
'lodash',
'require'

], function (angular, app, _, require) {

'use strict';

var module = angular.module('kibana.panels.text', []);
app.useModule(module);

module.controller('text', function($scope) {
  $scope.panelMeta = {
    status  : "Stable",
    description : "A static text panel that can use plain text, markdown, or (sanitized) HTML"
  };

  // Set and populate defaults
  var _d = {
    /** @scratch /panels/text/5
     *
     * === Parameters
     *
     * mode:: `html', `markdown' or `text'
     */
    mode    : "markdown", // 'html','markdown','text'
    /** @scratch /panels/text/5
     * content:: The content of your panel, written in the mark up specified in +mode+
     */
    content : "",
    style: {},
  };
  _.defaults($scope.panel,_d);

  $scope.init = function() {
    $scope.ready = false;
  };

});

module.directive('markdown', function() {
  return {
    restrict: 'E',
    link: function(scope, element) {
      scope.$on('render', function() {
        render_panel();
      });

      function render_panel() {
        require(['./lib/showdown'], function (Showdown) {
          scope.ready = true;
          var converter = new Showdown.converter();
          var text = scope.panel.content.replace(/&/g, '&')
            .replace(/>/g, '>')
            .replace(/</g, '&lt;');
          var htmlText = converter.makeHtml(text);
          element.html(htmlText);
          // For whatever reason, this fixes chrome. I don't like it, I think
          // it makes things slow?
          if(!scope.$$phase) {
            scope.$apply();
          }
        });
      }

      render_panel();
    }
  };
});

module.filter('newlines', function(){
  return function (input) {
    return input.replace(/\n/g, '<br/>');
  };
});

module.filter('striphtml', function () {
  return function(text) {
    return text
      .replace(/&/g, '&amp;')
      .replace(/>/g, '&gt;')
      .replace(/</g, '&lt;');
  };
});

});