gridstack.js/demo/knockout.html

123 lines
4.5 KiB
HTML
Raw Normal View History

2015-01-19 09:52:29 +01:00
<!DOCTYPE html>
<html lang="en">
<head>
2015-02-28 00:36:18 +01:00
<!--[if lt IE 9]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
2015-01-19 09:52:29 +01:00
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Knockout.js demo</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<link rel="stylesheet" href="../dist/gridstack.css"/>
2015-01-19 09:52:29 +01:00
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.0/jquery-ui.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.5.0/lodash.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>
<script src="../dist/gridstack.js"></script>
2016-08-19 06:44:01 +02:00
<script src="../dist/gridstack.jQueryUI.js"></script>
2015-01-19 09:52:29 +01:00
<style type="text/css">
.grid-stack {
background: lightgoldenrodyellow;
}
.grid-stack-item-content {
color: #2c3e50;
text-align: center;
background-color: #18bc9c;
}
</style>
</head>
<body>
<div class="container-fluid">
<h1>knockout.js Demo</h1>
2015-02-28 00:36:18 +01:00
<div>
2016-02-18 03:48:03 +01:00
<button data-bind="click: addNewWidget">Add new widget</button>
2015-02-28 00:36:18 +01:00
</div>
<br>
2015-01-19 09:52:29 +01:00
<div data-bind="component: {name: 'dashboard-grid', params: $data}"></div>
</div>
<script type="text/javascript">
ko.components.register('dashboard-grid', {
viewModel: {
createViewModel: function (controller, componentInfo) {
var ViewModel = function (controller, componentInfo) {
var grid = null;
this.widgets = controller.widgets;
this.afterAddWidget = function (items) {
2015-02-28 00:36:18 +01:00
if (grid == null) {
grid = $(componentInfo.element).find('.grid-stack').gridstack({
auto: false
}).data('gridstack');
}
var item = _.find(items, function (i) { return i.nodeType == 1 });
2016-02-18 03:48:03 +01:00
grid.addWidget(item);
2015-02-28 00:36:18 +01:00
ko.utils.domNodeDisposal.addDisposeCallback(item, function () {
2016-02-18 03:48:03 +01:00
grid.removeWidget(item);
2015-02-28 00:36:18 +01:00
});
2015-01-19 09:52:29 +01:00
};
};
return new ViewModel(controller, componentInfo);
}
},
template:
[
'<div class="grid-stack" data-bind="foreach: {data: widgets, afterRender: afterAddWidget}">',
2015-02-28 00:36:18 +01:00
' <div class="grid-stack-item" data-bind="attr: {\'data-gs-x\': $data.x, \'data-gs-y\': $data.y, \'data-gs-width\': $data.width, \'data-gs-height\': $data.height, \'data-gs-auto-position\': $data.auto_position}">',
2016-02-18 03:48:03 +01:00
' <div class="grid-stack-item-content"><button data-bind="click: $root.deleteWidget">Delete me</button></div>',
2015-01-19 09:52:29 +01:00
' </div>',
2015-02-28 00:36:18 +01:00
'</div> '
].join('')
2015-01-19 09:52:29 +01:00
});
$(function () {
var Controller = function (widgets) {
2015-02-28 00:36:18 +01:00
var self = this;
2015-01-19 09:52:29 +01:00
this.widgets = ko.observableArray(widgets);
2015-02-28 00:36:18 +01:00
2016-02-18 03:48:03 +01:00
this.addNewWidget = function () {
2015-02-28 00:36:18 +01:00
this.widgets.push({
x: 0,
y: 0,
width: Math.floor(1 + 3 * Math.random()),
height: Math.floor(1 + 3 * Math.random()),
auto_position: true
});
2016-02-18 03:48:03 +01:00
return false;
2015-02-28 00:36:18 +01:00
};
2016-02-18 03:48:03 +01:00
this.deleteWidget = function (item) {
2015-02-28 00:36:18 +01:00
self.widgets.remove(item);
2016-02-18 03:48:03 +01:00
return false;
2015-02-28 00:36:18 +01:00
};
2015-01-19 09:52:29 +01:00
};
var widgets = [
{x: 0, y: 0, width: 2, height: 2},
{x: 2, y: 0, width: 4, height: 2},
{x: 6, y: 0, width: 2, height: 4},
{x: 1, y: 2, width: 4, height: 2}
];
2015-02-28 00:36:18 +01:00
var controller = new Controller(widgets);
ko.applyBindings(controller);
2015-01-19 09:52:29 +01:00
});
</script>
</body>
</html>