Backbonejs - Add Shape Can Drag II

by erick

HTML

<script src="http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.2.1/underscore-min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/backbone.js/0.5.3/backbone-min.js"></script>
<div id="page" style="width:2000px;height:2000px;">
    <button id="new-rectangle">New Rectangle</button>
    <button id="new-circle">New Circle</button>
</div>

CSS

.shape{
    height: 100%;
    width: 100%;
}
.circle {
    border-radius: 50%/50%;
    -moz-border-radius: 50%/50%;
    -webkit-border-radius: 50%/50%;
}
.hide {
    display: none;
}
.control {
    width: 16px;
    height: 16px;
    position: absolute;
    bottom: 2px;
    cursor: pointer;
}
.resize {
    background: top left no-repeat url(http://cdn1.iconfinder.com/data/icons/fugue/icon/arrow-resize-135.png);
    right: 5px;
    cursor: nw-resize;
}
.delete {
    background: top left no-repeat url(http://cdn3.iconfinder.com/data/icons/uidesignicons/delete.png);
    right: 26px;
}

.change-color {
    background: top left no-repeat url(http://cdn1.iconfinder.com/data/icons/silk2/color_wheel.png);
    right: 47px;
}
body { 
  font: 12px  Arial;
}
button {
    margin: 3px;
    background: #999;
    border:2px solid #444;
    text-align:center;
    color: #eee;
    padding: 3px;
    font: bold 16px  Arial;
    border-radius: 5px;
    box-shadow:
        3px 3px 6px rgba(0, 0, 0, .2),
        0px 0px 3px rgba(0, 0, 0, .1)
} 
button:hover {
    background:#888;
    color:#fff;
}

JavaScript

/*
Copyright © Chaker Nakhli 2011
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by
applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License. 
*/

var Shape = Backbone.Model.extend({
    defaults: { x:50, y:50, width:150, height:150, color:'black' },
    setTopLeft: function(x,y) { this.set({ x:x, y:y }); },
    setDim: function(w,h) { this.set({ width:w, height:h }); },
    isCircle: function() { return !!this.get('circle'); }
});

var Document = Backbone.Collection.extend({ model: Shape });

var DocumentView =  Backbone.View.extend({
    id: 'page',
    views: {},
    initialize: function() {
        this.collection.bind('add', function(model) {
            this.views[model.cid] = new ShapeView({ model: model, id:'view_' + model.cid }).render();
        }, this);
        this.collection.bind('remove', function(model) { 
            this.views[model.cid].remove();
            delete this.views[model.cid];
        }, this);
    },
    render: function() {
        return this;
    }
});

var ShapeView = Backbone.View.extend({ 
    initialize: function() {
        $('#page').mousemove(this, this.mousemove).mouseup(this, this.mouseup);
        this.model.bind('change', this.updateView, this);
    },
    render: function() {
        $('#page').append(this.el);
        $(this.el).html('<div class="shape"/>'
                  + '<div class="control delete hide"/>'
                  + '<div class="control change-color hide"/>'
                  + '<div class="control resize hide"/>')
            .css({ position: 'absolute', padding: '10px' });
        if...