Commit 5ba03e80 authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Reference videos by name instead of ID.

Video URL-s are now much friendlier.

When video only has an ID, turn the ID into a name (for backwards
compatibility).

Additionally I built a duplicate name checking into the processor
of videos.json, which right away discovered two duplicate videos :)
parent f48d8e9a
Loading
Loading
Loading
Loading
+19 −0
Original line number Diff line number Diff line
require 'jsduck/json_duck'
require 'jsduck/null_object'
require 'jsduck/logger'

module JsDuck

@@ -16,6 +17,24 @@ module JsDuck

    def initialize(filename)
      @videos = JsonDuck.read(filename)
      add_names_if_missing
    end

    # Each video should have a name, which is used in URL to reference the video.
    # For backwards compatibility, when name is missing, we turn the "id" (that must exist)
    # into a name.  Additionally check that no two videos have the same name.
    def add_names_if_missing
      uniq_names = {}
      @videos.each do |group|
        group["items"].each do |video|
          video["name"] = video["id"] unless video["name"]

          if uniq_names[video["name"]]
            Logger.instance.warn(nil, "Two videos have the same name '#{video['name']}'")
          end
          uniq_names[video["name"]] = true
        end
      end
    end

    # Writes videos JSON file to a dir
+9 −9
Original line number Diff line number Diff line
@@ -26,7 +26,7 @@ Ext.define('Docs.controller.Videos', {
            /**
             * @event showVideo
             * Fired after a video is shown. Used for analytics event tracking.
             * @param {Number} video ID of the video.
             * @param {String} name Name of the video.
             */
            "showVideo"
        );
@@ -55,11 +55,11 @@ Ext.define('Docs.controller.Videos', {

        Ext.getCmp('card-panel').layout.setActiveItem('video');
        Ext.getCmp('treecontainer').showTree('videotree');
        var videoId = url.match(/[0-9]+$/)[0];
        var name = url.match(/^#!\/video\/(.*)$/)[1];

        var video = this.getVideo(videoId);
        var video = this.getVideo(name);
        if (!video) {
            this.getController('Failure').show404("Video <b>"+videoId+"</b> was not found.");
            this.getController('Failure').show404("Video <b>"+name+"</b> was not found.");
            return;
        }
        this.getViewport().setPageTitle(video.title);
@@ -68,21 +68,21 @@ Ext.define('Docs.controller.Videos', {
            reRendered = true;
        }
        noHistory || Docs.History.push(url);
        this.fireEvent('showVideo', videoId, {reRendered: reRendered});
        this.fireEvent('showVideo', name, {reRendered: reRendered});
        this.getTree().selectUrl(url);
        this.activeUrl = url;
    },

    // Given an ID returns corresponding video description object
    getVideo: function(id) {
    // Given a name returns corresponding video description object
    getVideo: function(name) {
        if (!this.map) {
            this.map = {};
            Ext.Array.forEach(Docs.data.videos, function(group) {
                Ext.Array.forEach(group.items, function(v) {
                    this.map[v.id] = v;
                    this.map[v.name] = v;
                }, this);
            }, this);
        }
        return this.map[id];
        return this.map[name];
    }
});
+1 −1
Original line number Diff line number Diff line
@@ -64,7 +64,7 @@ Ext.define('Docs.view.TreeContainer', {
                    return {
                        leaf: true,
                        text: video.title,
                        url: '#!/video/' + video.id,
                        url: '#!/video/' + video.name,
                        iconCls: 'icon-video'
                    };
                }
+1 −1
Original line number Diff line number Diff line
@@ -17,7 +17,7 @@ Ext.define('Docs.view.videos.Index', {
            { xtype: 'container', html: '<h1 class="eg">Videos</h1>' },
            Ext.create('Docs.view.ThumbList', {
                itemTpl: [
                    '<dd ext:url="#!/video/{id}"><div class="thumb"><img src="{thumb}"/></div>',
                    '<dd ext:url="#!/video/{name}"><div class="thumb"><img src="{thumb}"/></div>',
                        '<div><h4>{title}',
                        '</h4><p>{[values.description.substr(0,80)]}...</p></div>',
                    '</dd>'