src/js/widgets.js
author veltr
Fri, 21 Sep 2012 11:18:57 +0900
changeset 964 d7d56ea2d0a6
parent 959 ee11ed1b739e
child 965 eadb7290c325
permissions -rw-r--r--
Adaptations for Knowledge Concierge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
875
43629caa77bc Big refactoring of widget files + started migration of segmentwidget
veltr
parents: 874
diff changeset
     1
/* Definition of an ancestor for the Widget classes */
43629caa77bc Big refactoring of widget files + started migration of segmentwidget
veltr
parents: 874
diff changeset
     2
43629caa77bc Big refactoring of widget files + started migration of segmentwidget
veltr
parents: 874
diff changeset
     3
if (typeof IriSP.Widgets === "undefined") {
43629caa77bc Big refactoring of widget files + started migration of segmentwidget
veltr
parents: 874
diff changeset
     4
    IriSP.Widgets = {}
43629caa77bc Big refactoring of widget files + started migration of segmentwidget
veltr
parents: 874
diff changeset
     5
}
66
13013b9452af Added a new file to the build, widgets.js, to contain widget classes.
hamidouk
parents:
diff changeset
     6
520
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents: 287
diff changeset
     7
/**
868
a525cc2214e7 Started big refactoring
veltr
parents: 866
diff changeset
     8
 * @class IriSP.Widget is an "abstract" class. It's mostly used to define some properties common to every widget.
a525cc2214e7 Started big refactoring
veltr
parents: 866
diff changeset
     9
 *
a525cc2214e7 Started big refactoring
veltr
parents: 866
diff changeset
    10
 *  Note that widget constructors are never called directly by the user. Instead, the widgets are instantiated by functions
a525cc2214e7 Started big refactoring
veltr
parents: 866
diff changeset
    11
 *  defined in init.js
a525cc2214e7 Started big refactoring
veltr
parents: 866
diff changeset
    12
 *
a525cc2214e7 Started big refactoring
veltr
parents: 866
diff changeset
    13
 * @constructor
a525cc2214e7 Started big refactoring
veltr
parents: 866
diff changeset
    14
 * @param player - a reference to the player widget
a525cc2214e7 Started big refactoring
veltr
parents: 866
diff changeset
    15
 * @param config - configuration options for the widget
a525cc2214e7 Started big refactoring
veltr
parents: 866
diff changeset
    16
 */
875
43629caa77bc Big refactoring of widget files + started migration of segmentwidget
veltr
parents: 874
diff changeset
    17
43629caa77bc Big refactoring of widget files + started migration of segmentwidget
veltr
parents: 874
diff changeset
    18
43629caa77bc Big refactoring of widget files + started migration of segmentwidget
veltr
parents: 874
diff changeset
    19
IriSP.Widgets.Widget = function(player, config) {
287
5c7495102bd7 added a spacer div to simplify some graphic animations.
hamidouk
parents: 170
diff changeset
    20
868
a525cc2214e7 Started big refactoring
veltr
parents: 866
diff changeset
    21
    if( typeof player === "undefined") {
a525cc2214e7 Started big refactoring
veltr
parents: 866
diff changeset
    22
        /* Probably an abstract call of the class when
a525cc2214e7 Started big refactoring
veltr
parents: 866
diff changeset
    23
         * individual widgets set their prototype */
a525cc2214e7 Started big refactoring
veltr
parents: 866
diff changeset
    24
        return;
a525cc2214e7 Started big refactoring
veltr
parents: 866
diff changeset
    25
    }
a525cc2214e7 Started big refactoring
veltr
parents: 866
diff changeset
    26
    
a525cc2214e7 Started big refactoring
veltr
parents: 866
diff changeset
    27
    /* Setting all the configuration options */
a525cc2214e7 Started big refactoring
veltr
parents: 866
diff changeset
    28
    var _type = config.type,
958
2aa7fdb0762a Commit before branch switch (for tests)
veltr
parents: 957
diff changeset
    29
        _config = IriSP._.defaults({}, config, player.config.default_options, this.defaults),
868
a525cc2214e7 Started big refactoring
veltr
parents: 866
diff changeset
    30
        _this = this;
a525cc2214e7 Started big refactoring
veltr
parents: 866
diff changeset
    31
    
a525cc2214e7 Started big refactoring
veltr
parents: 866
diff changeset
    32
    IriSP._(_config).forEach(function(_value, _key) {
a525cc2214e7 Started big refactoring
veltr
parents: 866
diff changeset
    33
       _this[_key] = _value;
a525cc2214e7 Started big refactoring
veltr
parents: 866
diff changeset
    34
    });
a525cc2214e7 Started big refactoring
veltr
parents: 866
diff changeset
    35
    
872
d777d05a16e4 finished AnnotationsList and started New PolemicWidget
veltr
parents: 870
diff changeset
    36
    if (typeof this.width === "undefined") {
958
2aa7fdb0762a Commit before branch switch (for tests)
veltr
parents: 957
diff changeset
    37
        this.width = player.config.width;
872
d777d05a16e4 finished AnnotationsList and started New PolemicWidget
veltr
parents: 870
diff changeset
    38
    }
d777d05a16e4 finished AnnotationsList and started New PolemicWidget
veltr
parents: 870
diff changeset
    39
    
868
a525cc2214e7 Started big refactoring
veltr
parents: 866
diff changeset
    40
    /* Setting this.player at the end in case it's been overriden
a525cc2214e7 Started big refactoring
veltr
parents: 866
diff changeset
    41
     * by a configuration option of the same name :-(
a525cc2214e7 Started big refactoring
veltr
parents: 866
diff changeset
    42
     */
a525cc2214e7 Started big refactoring
veltr
parents: 866
diff changeset
    43
    this.player = player;
a525cc2214e7 Started big refactoring
veltr
parents: 866
diff changeset
    44
    
a525cc2214e7 Started big refactoring
veltr
parents: 866
diff changeset
    45
    /* Getting metadata */
870
2c025db10a10 Migrated playerWidget and started annotationsListWidget
veltr
parents: 868
diff changeset
    46
    this.source = player.loadMetadata(this.metadata);
868
a525cc2214e7 Started big refactoring
veltr
parents: 866
diff changeset
    47
    
a525cc2214e7 Started big refactoring
veltr
parents: 866
diff changeset
    48
    /* Call draw when loaded */
a525cc2214e7 Started big refactoring
veltr
parents: 866
diff changeset
    49
    this.source.onLoad(function() {
959
ee11ed1b739e Mashup Player and Dailymotion are now widgets
veltr
parents: 958
diff changeset
    50
        if (_this.media_id) {
ee11ed1b739e Mashup Player and Dailymotion are now widgets
veltr
parents: 958
diff changeset
    51
            _this.media = this.getElement(_this.media_id);
ee11ed1b739e Mashup Player and Dailymotion are now widgets
veltr
parents: 958
diff changeset
    52
        } else {
ee11ed1b739e Mashup Player and Dailymotion are now widgets
veltr
parents: 958
diff changeset
    53
            var _mediaopts = {
ee11ed1b739e Mashup Player and Dailymotion are now widgets
veltr
parents: 958
diff changeset
    54
                is_mashup: _this.is_mashup || false
ee11ed1b739e Mashup Player and Dailymotion are now widgets
veltr
parents: 958
diff changeset
    55
            }
ee11ed1b739e Mashup Player and Dailymotion are now widgets
veltr
parents: 958
diff changeset
    56
            _this.media = this.getCurrentMedia(_mediaopts);
ee11ed1b739e Mashup Player and Dailymotion are now widgets
veltr
parents: 958
diff changeset
    57
        }
ee11ed1b739e Mashup Player and Dailymotion are now widgets
veltr
parents: 958
diff changeset
    58
        
868
a525cc2214e7 Started big refactoring
veltr
parents: 866
diff changeset
    59
        _this.draw();
870
2c025db10a10 Migrated playerWidget and started annotationsListWidget
veltr
parents: 868
diff changeset
    60
    });
868
a525cc2214e7 Started big refactoring
veltr
parents: 866
diff changeset
    61
   
a525cc2214e7 Started big refactoring
veltr
parents: 866
diff changeset
    62
    /* Adding classes and html attributes */
870
2c025db10a10 Migrated playerWidget and started annotationsListWidget
veltr
parents: 868
diff changeset
    63
    this.$ = IriSP.jQuery('#' + this.container);
875
43629caa77bc Big refactoring of widget files + started migration of segmentwidget
veltr
parents: 874
diff changeset
    64
    this.$.addClass("Ldt-TraceMe Ldt-Widget").attr("widget-type", _type);
868
a525cc2214e7 Started big refactoring
veltr
parents: 866
diff changeset
    65
    
916
ec6849bbbdcc Removed Namespacing before rewrite
veltr
parents: 908
diff changeset
    66
    this.l10n = (
ec6849bbbdcc Removed Namespacing before rewrite
veltr
parents: 908
diff changeset
    67
        typeof this.messages[IriSP.language] !== "undefined"
ec6849bbbdcc Removed Namespacing before rewrite
veltr
parents: 908
diff changeset
    68
        ? this.messages[IriSP.language]
ec6849bbbdcc Removed Namespacing before rewrite
veltr
parents: 908
diff changeset
    69
        : (
ec6849bbbdcc Removed Namespacing before rewrite
veltr
parents: 908
diff changeset
    70
            IriSP.language.length > 2 && typeof this.messages[IriSP.language.substr(0,2)] !== "undefined"
ec6849bbbdcc Removed Namespacing before rewrite
veltr
parents: 908
diff changeset
    71
            ? this.messages[IriSP.language.substr(0,2)]
ec6849bbbdcc Removed Namespacing before rewrite
veltr
parents: 908
diff changeset
    72
            : this.messages["en"]
ec6849bbbdcc Removed Namespacing before rewrite
veltr
parents: 908
diff changeset
    73
        )
ec6849bbbdcc Removed Namespacing before rewrite
veltr
parents: 908
diff changeset
    74
    );
875
43629caa77bc Big refactoring of widget files + started migration of segmentwidget
veltr
parents: 874
diff changeset
    75
    
66
13013b9452af Added a new file to the build, widgets.js, to contain widget classes.
hamidouk
parents:
diff changeset
    76
};
74
d7a7d7216371 lots of changes to the player widget...
hamidouk
parents: 66
diff changeset
    77
875
43629caa77bc Big refactoring of widget files + started migration of segmentwidget
veltr
parents: 874
diff changeset
    78
IriSP.Widgets.Widget.prototype.defaults = {}
43629caa77bc Big refactoring of widget files + started migration of segmentwidget
veltr
parents: 874
diff changeset
    79
43629caa77bc Big refactoring of widget files + started migration of segmentwidget
veltr
parents: 874
diff changeset
    80
IriSP.Widgets.Widget.prototype.template = '';
43629caa77bc Big refactoring of widget files + started migration of segmentwidget
veltr
parents: 874
diff changeset
    81
43629caa77bc Big refactoring of widget files + started migration of segmentwidget
veltr
parents: 874
diff changeset
    82
IriSP.Widgets.Widget.prototype.messages = {"en":{}};
43629caa77bc Big refactoring of widget files + started migration of segmentwidget
veltr
parents: 874
diff changeset
    83
43629caa77bc Big refactoring of widget files + started migration of segmentwidget
veltr
parents: 874
diff changeset
    84
IriSP.Widgets.Widget.prototype.templateToHtml = function(_template) {
43629caa77bc Big refactoring of widget files + started migration of segmentwidget
veltr
parents: 874
diff changeset
    85
    return Mustache.to_html(_template, this);
43629caa77bc Big refactoring of widget files + started migration of segmentwidget
veltr
parents: 874
diff changeset
    86
}
43629caa77bc Big refactoring of widget files + started migration of segmentwidget
veltr
parents: 874
diff changeset
    87
43629caa77bc Big refactoring of widget files + started migration of segmentwidget
veltr
parents: 874
diff changeset
    88
IriSP.Widgets.Widget.prototype.renderTemplate = function() {
43629caa77bc Big refactoring of widget files + started migration of segmentwidget
veltr
parents: 874
diff changeset
    89
    this.$.append(this.templateToHtml(this.template));
43629caa77bc Big refactoring of widget files + started migration of segmentwidget
veltr
parents: 874
diff changeset
    90
}
43629caa77bc Big refactoring of widget files + started migration of segmentwidget
veltr
parents: 874
diff changeset
    91
43629caa77bc Big refactoring of widget files + started migration of segmentwidget
veltr
parents: 874
diff changeset
    92
IriSP.Widgets.Widget.prototype.functionWrapper = function(_name) {
870
2c025db10a10 Migrated playerWidget and started annotationsListWidget
veltr
parents: 868
diff changeset
    93
    var _this = this,
2c025db10a10 Migrated playerWidget and started annotationsListWidget
veltr
parents: 868
diff changeset
    94
        _function = this[_name];
2c025db10a10 Migrated playerWidget and started annotationsListWidget
veltr
parents: 868
diff changeset
    95
    if (typeof _function !== "undefined") {
2c025db10a10 Migrated playerWidget and started annotationsListWidget
veltr
parents: 868
diff changeset
    96
        return function() {
2c025db10a10 Migrated playerWidget and started annotationsListWidget
veltr
parents: 868
diff changeset
    97
            return _function.apply(_this, Array.prototype.slice.call(arguments, 0));
2c025db10a10 Migrated playerWidget and started annotationsListWidget
veltr
parents: 868
diff changeset
    98
        }
2c025db10a10 Migrated playerWidget and started annotationsListWidget
veltr
parents: 868
diff changeset
    99
    } else {
957
4da0a5740b6c Starting 'players-as-widgets' branch
veltr
parents: 927
diff changeset
   100
        console.log("Error, Unknown function IriSP.Widgets" + this.type + "." + _name)
4da0a5740b6c Starting 'players-as-widgets' branch
veltr
parents: 927
diff changeset
   101
    }
4da0a5740b6c Starting 'players-as-widgets' branch
veltr
parents: 927
diff changeset
   102
}
4da0a5740b6c Starting 'players-as-widgets' branch
veltr
parents: 927
diff changeset
   103
4da0a5740b6c Starting 'players-as-widgets' branch
veltr
parents: 927
diff changeset
   104
IriSP.Widgets.Widget.prototype.getFunctionOrName = function(_functionOrName) {
4da0a5740b6c Starting 'players-as-widgets' branch
veltr
parents: 927
diff changeset
   105
    switch (typeof _functionOrName) {
4da0a5740b6c Starting 'players-as-widgets' branch
veltr
parents: 927
diff changeset
   106
        case "function":
4da0a5740b6c Starting 'players-as-widgets' branch
veltr
parents: 927
diff changeset
   107
            return _functionOrName;
4da0a5740b6c Starting 'players-as-widgets' branch
veltr
parents: 927
diff changeset
   108
        case "string":
4da0a5740b6c Starting 'players-as-widgets' branch
veltr
parents: 927
diff changeset
   109
            return this.functionWrapper(_functionOrName);
4da0a5740b6c Starting 'players-as-widgets' branch
veltr
parents: 927
diff changeset
   110
        default:
4da0a5740b6c Starting 'players-as-widgets' branch
veltr
parents: 927
diff changeset
   111
            return undefined;
870
2c025db10a10 Migrated playerWidget and started annotationsListWidget
veltr
parents: 868
diff changeset
   112
    }
2c025db10a10 Migrated playerWidget and started annotationsListWidget
veltr
parents: 868
diff changeset
   113
}
2c025db10a10 Migrated playerWidget and started annotationsListWidget
veltr
parents: 868
diff changeset
   114
957
4da0a5740b6c Starting 'players-as-widgets' branch
veltr
parents: 927
diff changeset
   115
IriSP.Widgets.Widget.prototype.onMdpEvent = function(_eventName, _functionOrName) {
4da0a5740b6c Starting 'players-as-widgets' branch
veltr
parents: 927
diff changeset
   116
    this.player.on(_eventName, this.getFunctionOrName(_functionOrName));
4da0a5740b6c Starting 'players-as-widgets' branch
veltr
parents: 927
diff changeset
   117
}
4da0a5740b6c Starting 'players-as-widgets' branch
veltr
parents: 927
diff changeset
   118
4da0a5740b6c Starting 'players-as-widgets' branch
veltr
parents: 927
diff changeset
   119
IriSP.Widgets.Widget.prototype.onMediaEvent = function(_eventName, _functionOrName) {
4da0a5740b6c Starting 'players-as-widgets' branch
veltr
parents: 927
diff changeset
   120
    this.media.on(_eventName, this.getFunctionOrName(_functionOrName));
870
2c025db10a10 Migrated playerWidget and started annotationsListWidget
veltr
parents: 868
diff changeset
   121
}
2c025db10a10 Migrated playerWidget and started annotationsListWidget
veltr
parents: 868
diff changeset
   122
876
03967b6ada7c ArrowWidget, AnnotationWidget
veltr
parents: 875
diff changeset
   123
IriSP.Widgets.Widget.prototype.getWidgetAnnotations = function() {
957
4da0a5740b6c Starting 'players-as-widgets' branch
veltr
parents: 927
diff changeset
   124
    return typeof this.annotation_type !== "undefined" && this.annotation_type ? this.media.getAnnotationsByTypeTitle(this.annotation_type) : this.media.getAnnotations();
876
03967b6ada7c ArrowWidget, AnnotationWidget
veltr
parents: 875
diff changeset
   125
}
03967b6ada7c ArrowWidget, AnnotationWidget
veltr
parents: 875
diff changeset
   126
922
096c06aea8b5 Minor changes
veltr
parents: 916
diff changeset
   127
IriSP.Widgets.Widget.prototype.getWidgetAnnotationsAtTime = function() {
957
4da0a5740b6c Starting 'players-as-widgets' branch
veltr
parents: 927
diff changeset
   128
    var _time = this.media.getCurrentTime();
922
096c06aea8b5 Minor changes
veltr
parents: 916
diff changeset
   129
    return this.getWidgetAnnotations().filter(function(_annotation) {
096c06aea8b5 Minor changes
veltr
parents: 916
diff changeset
   130
        return _annotation.begin <= _time && _annotation.end > _time;
096c06aea8b5 Minor changes
veltr
parents: 916
diff changeset
   131
    });
096c06aea8b5 Minor changes
veltr
parents: 916
diff changeset
   132
}
096c06aea8b5 Minor changes
veltr
parents: 916
diff changeset
   133
959
ee11ed1b739e Mashup Player and Dailymotion are now widgets
veltr
parents: 958
diff changeset
   134
IriSP.Widgets.Widget.prototype.insertSubwidget = function(_selector, _widgetoptions, _propname) {
924
64c2eaafe5e2 Modifications for LDT-Platform
veltr
parents: 922
diff changeset
   135
    var _id = _selector.attr("id"),
927
977a39c4ee80 Added URL Copy function to the Social Widget
veltr
parents: 924
diff changeset
   136
        _this = this,
977a39c4ee80 Added URL Copy function to the Social Widget
veltr
parents: 924
diff changeset
   137
        _type = _widgetoptions.type,
977a39c4ee80 Added URL Copy function to the Social Widget
veltr
parents: 924
diff changeset
   138
        $L = $LAB;
924
64c2eaafe5e2 Modifications for LDT-Platform
veltr
parents: 922
diff changeset
   139
    if (typeof _id == "undefined") {
64c2eaafe5e2 Modifications for LDT-Platform
veltr
parents: 922
diff changeset
   140
        _id = IriSP._.uniqueId(this.container + '_sub_widget_' + _widgetoptions.type);
64c2eaafe5e2 Modifications for LDT-Platform
veltr
parents: 922
diff changeset
   141
        _selector.attr("id", _id);
64c2eaafe5e2 Modifications for LDT-Platform
veltr
parents: 922
diff changeset
   142
    }
64c2eaafe5e2 Modifications for LDT-Platform
veltr
parents: 922
diff changeset
   143
    _widgetoptions.container = _id;
927
977a39c4ee80 Added URL Copy function to the Social Widget
veltr
parents: 924
diff changeset
   144
    if (typeof IriSP.widgetsRequirements[_type] !== "undefined" && typeof IriSP.widgetsRequirements[_type].requires !== "undefined" ) {
977a39c4ee80 Added URL Copy function to the Social Widget
veltr
parents: 924
diff changeset
   145
        for (var _j = 0; _j < IriSP.widgetsRequirements[_type].requires.length; _j++) {
977a39c4ee80 Added URL Copy function to the Social Widget
veltr
parents: 924
diff changeset
   146
            $L.script(IriSP.getLib(IriSP.widgetsRequirements[_type].requires[_j]));
977a39c4ee80 Added URL Copy function to the Social Widget
veltr
parents: 924
diff changeset
   147
        }
977a39c4ee80 Added URL Copy function to the Social Widget
veltr
parents: 924
diff changeset
   148
    }
977a39c4ee80 Added URL Copy function to the Social Widget
veltr
parents: 924
diff changeset
   149
    $L.wait(function() {
977a39c4ee80 Added URL Copy function to the Social Widget
veltr
parents: 924
diff changeset
   150
        _this.player.loadWidget(_widgetoptions, function(_widget) {
959
ee11ed1b739e Mashup Player and Dailymotion are now widgets
veltr
parents: 958
diff changeset
   151
            if (_propname) {
ee11ed1b739e Mashup Player and Dailymotion are now widgets
veltr
parents: 958
diff changeset
   152
                _this[_propname] = _widget;
ee11ed1b739e Mashup Player and Dailymotion are now widgets
veltr
parents: 958
diff changeset
   153
            }
927
977a39c4ee80 Added URL Copy function to the Social Widget
veltr
parents: 924
diff changeset
   154
        });
924
64c2eaafe5e2 Modifications for LDT-Platform
veltr
parents: 922
diff changeset
   155
    });
64c2eaafe5e2 Modifications for LDT-Platform
veltr
parents: 922
diff changeset
   156
}
64c2eaafe5e2 Modifications for LDT-Platform
veltr
parents: 922
diff changeset
   157
520
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents: 287
diff changeset
   158
/**
868
a525cc2214e7 Started big refactoring
veltr
parents: 866
diff changeset
   159
 * This method responsible of drawing a widget on screen.
a525cc2214e7 Started big refactoring
veltr
parents: 866
diff changeset
   160
 */
875
43629caa77bc Big refactoring of widget files + started migration of segmentwidget
veltr
parents: 874
diff changeset
   161
IriSP.Widgets.Widget.prototype.draw = function() {
868
a525cc2214e7 Started big refactoring
veltr
parents: 866
diff changeset
   162
    /* implemented by "sub-classes" */
908
f56199193fad CreateAnnotation widget now posts annotations, Tagcloud can be made segment-dependent
veltr
parents: 900
diff changeset
   163
};