|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | +<head> |
| 4 | + <!--[if lt IE 9]> |
| 5 | + <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script> |
| 6 | + <![endif]--> |
| 7 | + |
| 8 | + <meta charset="utf-8"> |
| 9 | + <meta http-equiv="X-UA-Compatible" content="IE=edge"> |
| 10 | + <meta name="viewport" content="width=device-width, initial-scale=1"> |
| 11 | + <title>Knockout.js demo</title> |
| 12 | + |
| 13 | + <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css"> |
| 14 | + <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/font-awesome/4.2.0/css/font-awesome.min.css"/> |
| 15 | + <link rel="stylesheet" href="../gridstack.css"/> |
| 16 | + |
| 17 | + <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> |
| 18 | + <script src="//cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.0/jquery-ui.js"></script> |
| 19 | + <script src="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script> |
| 20 | + <script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.7.0/underscore-min.js"></script> |
| 21 | + <script src="//cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script> |
| 22 | + <script src="../gridstack.js"></script> |
| 23 | + |
| 24 | + <style type="text/css"> |
| 25 | + .grid-stack { |
| 26 | + background: lightgoldenrodyellow; |
| 27 | + } |
| 28 | + |
| 29 | + .grid-stack-item-content { |
| 30 | + color: #2c3e50; |
| 31 | + text-align: center; |
| 32 | + background-color: #18bc9c; |
| 33 | + } |
| 34 | + </style> |
| 35 | +</head> |
| 36 | +<body> |
| 37 | + <div class="container-fluid"> |
| 38 | + <h1>knockout.js Demo</h1> |
| 39 | + |
| 40 | + <div> |
| 41 | + <button data-bind="click: add_new_widget">Add new widget</button> |
| 42 | + </div> |
| 43 | + |
| 44 | + <br> |
| 45 | + |
| 46 | + <div data-bind="component: {name: 'dashboard-grid', params: $data}"></div> |
| 47 | + </div> |
| 48 | + |
| 49 | + |
| 50 | + <script type="text/javascript"> |
| 51 | + ko.components.register('dashboard-grid', { |
| 52 | + viewModel: { |
| 53 | + createViewModel: function (controller, componentInfo) { |
| 54 | + var ViewModel = function (controller, componentInfo) { |
| 55 | + var grid = null; |
| 56 | + |
| 57 | + this.widgets = controller.widgets; |
| 58 | + |
| 59 | + this.afterAddWidget = function (items) { |
| 60 | + if (grid == null) { |
| 61 | + grid = $(componentInfo.element).find('.grid-stack').gridstack({ |
| 62 | + auto: false |
| 63 | + }).data('gridstack'); |
| 64 | + } |
| 65 | + |
| 66 | + var item = _.find(items, function (i) { return i.nodeType == 1 }); |
| 67 | + grid.add_widget(item); |
| 68 | + ko.utils.domNodeDisposal.addDisposeCallback(item, function () { |
| 69 | + grid.remove_widget(item); |
| 70 | + }); |
| 71 | + }; |
| 72 | + }; |
| 73 | + |
| 74 | + return new ViewModel(controller, componentInfo); |
| 75 | + } |
| 76 | + }, |
| 77 | + template: { element: 'gridstack-template' } |
| 78 | + }); |
| 79 | + |
| 80 | + $(function () { |
| 81 | + var Controller = function (widgets) { |
| 82 | + var self = this; |
| 83 | + |
| 84 | + this.widgets = ko.observableArray(widgets); |
| 85 | + |
| 86 | + this.add_new_widget = function () { |
| 87 | + this.widgets.push({ |
| 88 | + x: 0, |
| 89 | + y: 0, |
| 90 | + width: Math.floor(1 + 3 * Math.random()), |
| 91 | + height: Math.floor(1 + 3 * Math.random()), |
| 92 | + auto_position: true |
| 93 | + }); |
| 94 | + }; |
| 95 | + |
| 96 | + this.delete_widget = function (item) { |
| 97 | + self.widgets.remove(item); |
| 98 | + }; |
| 99 | + }; |
| 100 | + |
| 101 | + var widgets = [ |
| 102 | + {x: 0, y: 0, width: 2, height: 2}, |
| 103 | + {x: 2, y: 0, width: 4, height: 2}, |
| 104 | + {x: 6, y: 0, width: 2, height: 4}, |
| 105 | + {x: 1, y: 2, width: 4, height: 2} |
| 106 | + ]; |
| 107 | + |
| 108 | + var controller = new Controller(widgets); |
| 109 | + ko.applyBindings(controller); |
| 110 | + }); |
| 111 | + </script> |
| 112 | + |
| 113 | + <template id="gridstack-template"> |
| 114 | + <div class="grid-stack" data-bind="foreach: {data: widgets, afterRender: afterAddWidget}"> |
| 115 | + <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}"> |
| 116 | + <div class="grid-stack-item-content"><button data-bind="click: $root.delete_widget">Delete me</button></div> |
| 117 | + </div></div><!-- <---- NO SPACE BETWEEN THESE CLOSING TAGS --> |
| 118 | + </template> |
| 119 | +</body> |
| 120 | +</html> |
0 commit comments