CanJS jQuery Template

Includes jQuery, CanJS and all of its plugins. Use it as a starting point when you want to demo something.

HTML

<script src="http://canjs.us/release/latest/can.jquery.js"></script>
<script src="http://canjs.us/release/latest/can.construct.proxy.js"></script>
<script src="http://canjs.us/release/latest/can.control.plugin.js"></script>
<script src="http://canjs.us/release/latest/can.observe.attributes.js"></script>
<script src="http://canjs.us/release/latest/can.observe.backup.js"></script>
<script src="http://canjs.us/release/latest/can.observe.delegate.js"></script>
<script src="http://canjs.us/release/latest/can.observe.setter.js"></script>
<script src="http://canjs.us/release/latest/can.observe.validations.js"></script>
<script src="http://canjs.us/release/latest/can.view.modifiers.js"></script>
<script src="http://canjs.us/release/latest/can.fixture.js"></script>
<script src="http://canjs.us/release/latest/can.view.mustache.js"></script>
<script src="http://canjs.us/release/latest/can.construct.super.js"></script>
<script src="http://jquerypp.com/release/latest/jquerypp.js"></script>
<video width=425 height=240 id="trailer" preload controls>
    <source src="http://playground.html5rocks.com/samples/html5_misc/chrome_japan.mp4" type='video/mp4; codecs="avc1.42E01E, mp4a.40.2"' />
    <source src="http://playground.html5rocks.com/samples/html5_misc/chrome_japan.webm" type='video/webm; codecs="vp8, vorbis"' />
    <source src="http://playground.html5rocks.com/samples/html5_misc/chrome_japan.ogv" type='video/ogg; codecs="theora, vorbis"' />
</video>
<div id='container'>
    <div id='slider'></div>
</div>

CSS

#slider {
    border: solid 1px blue;
    background-color: red;
    height: 30px;
    width: 30px;
    cursor: ew-resize;
}
#container {
    border: solid 1px black;
}

JavaScript

makeSlider()


var video = document.getElementById("trailer"),
    time = can.compute(video,"currentTime","timeupdate"),
    duration = can.compute(video,"duration","durationchange");
    
var progress = can.compute(function(newPercent){
    // can only do anything if duration is ready
    var durationValue = duration();
    if(typeof durationValue == "number" && !isNaN(durationValue)){
        if(arguments.length){
            time(newPercent * durationValue)
        } else {
            return time() / durationValue;
        }
    }
})

new Slider("#slider",{
    value: progress
})



function makeSlider(){

    Slider = can.Control.extend({
        init: function(){
            this.updatePosition()
        },
        "draginit": function(el, ev, drag){
            drag.limit(this.element.parent());
            drag.horizontal();
            this.dragging = true;
        },
        "dragmove": function(el, ev, drag){
            
            var container = this.element.parent(),
                sliderOffset = el.offset().left,
                containerOffset = container.offset().left,
                sliderWidth = el.outerWidth(),
                containerWidth = container.width();
            
            containerOffset +=
                parseInt( container.css("paddingLeft") ) +
                parseInt( container.css("borderLeftWidth") )
            
            var value = ( (sliderOffset-containerOffset ) /
                         (containerWidth - sliderWidth) );
            
            
            this.options.value(value)
            
        },
        "dragend": function(){
            this.dragging = false;
        },
        "{value} change": "updatePosition",
        updatePosition: function(){
            if(this.dragging){
                return;   
            }
            var value = this.options.value(),
                container = this.element.parent(),
                containerOffset = container.offset().left,
               ...