{"size": 1693, "ext": "js", "max_stars_repo_path": "addons/cb.help/views/page.js", "max_stars_repo_name": "cybernetics/codebox", "max_stars_repo_stars_event_min_datetime": "2019-04-22T08:50:01.000Z", "max_stars_repo_stars_event_max_datetime": "2019-04-22T08:50:01.000Z", "max_issues_repo_path": "addons/cb.help/views/page.js", "max_issues_repo_name": "cybernetics/codebox", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "addons/cb.help/views/page.js", "max_forks_repo_name": "cybernetics/codebox", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 28.2166666667, "max_line_length": 89, "alphanum_fraction": 0.4861193148}
define([
    "showdown",
    "extensions/github",
    "extensions/table",
    "less!stylesheets/page.less"
], function(Showdown) {
    var hr = codebox.require("hr/hr");
    var _ = codebox.require("underscore");
    var $ = codebox.require("jQuery");

    var PageView = hr.View.extend({
        className: "addon-help-page",
        events: {
            "click a": "clickLink"
        },

        // Constructor
        initialize: function() {
            PageView.__super__.initialize.apply(this, arguments);
            this.converter = new Showdown.converter({ extensions: ['github', 'table'] });

            this.content = "";
            this.loadPage(this.options.page);
            return this;
        },

        // Load page
        loadPage: function(page) {
            var that = this;

            this.currentPage = page || "README.md";
            return hr.Requests.get("/docs/"+this.currentPage).then(function(content) {
                that.content = content;
            }, function() {
                that.content = "# Error with page: "+_.escape(that.currentPage);
            }).fin(function() {
                that.render();
            });
        },

        // Render
        render: function() {
            this.$el.html(this.converter.makeHtml(this.content));
            return this.ready();
        },

        // Click link
        clickLink: function(e) {
            var a = $(e.currentTarget);
            var url = a.attr("href");
            var r = new RegExp('^(?:[a-z]+:)?//', 'i');
            if (!r.test(url)) {
                e.preventDefault();
                this.loadPage(url);
            }
        } 
    });

    return PageView;
});