|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | +<head> |
| 4 | + <meta charset="utf-8"> |
| 5 | + <meta http-equiv="X-UA-Compatible" content="IE=edge"> |
| 6 | + <meta name="viewport" content="width=device-width, initial-scale=1"> |
| 7 | + <title>Knockout.js demo</title> |
| 8 | + |
| 9 | + <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css"> |
| 10 | + <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/gridstack.js/0.2.2/gridstack.min.js"/> |
| 11 | + |
| 12 | + <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/font-awesome/4.2.0/css/font-awesome.min.css"/> |
| 13 | + <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/gridstack.js/0.2.1/gridstack.min.css"/> |
| 14 | + |
| 15 | + <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries --> |
| 16 | + <!-- WARNING: Respond.js doesn't work if you view the page via file:// --> |
| 17 | + <!--[if lt IE 9]> |
| 18 | + <script src="//oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script> |
| 19 | + <script src="//oss.maxcdn.com/respond/1.4.2/respond.min.js"></script> |
| 20 | + <![endif]--> |
| 21 | + |
| 22 | + <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> |
| 23 | + <script src="//cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.0/jquery-ui.js"></script> |
| 24 | + <script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script> |
| 25 | + <script src="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script> |
| 26 | + <script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.7.0/underscore-min.js"></script> |
| 27 | + <script src="//cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script> |
| 28 | + <script src="//cdnjs.cloudflare.com/ajax/libs/knockout.mapping/2.4.1/knockout.mapping.js"></script> |
| 29 | + <script src="//cdnjs.cloudflare.com/ajax/libs/gridstack.js/0.2.2/gridstack.min.js"></script> |
| 30 | + |
| 31 | + <style type="text/css"> |
| 32 | + .grid-stack { |
| 33 | + background: lightgoldenrodyellow; |
| 34 | + } |
| 35 | + |
| 36 | + .grid-stack-item-content { |
| 37 | + color: #2c3e50; |
| 38 | + text-align: center; |
| 39 | + background-color: #18bc9c; |
| 40 | + } |
| 41 | + </style> |
| 42 | +</head> |
| 43 | +<body> |
| 44 | + <div class="container-fluid"> |
| 45 | + <h1>knockout.js Demo</h1> |
| 46 | + |
| 47 | + <div data-bind="component: {name: 'dashboard-grid', params: $data}"></div> |
| 48 | + </div> |
| 49 | + |
| 50 | + |
| 51 | + <script type="text/javascript"> |
| 52 | + ko.components.register('dashboard-grid', { |
| 53 | + viewModel: { |
| 54 | + createViewModel: function (controller, componentInfo) { |
| 55 | + var ViewModel = function (controller, componentInfo) { |
| 56 | + var grid = null; |
| 57 | + |
| 58 | + this.widgets = controller.widgets; |
| 59 | + |
| 60 | + this.afterAddWidget = function (items) { |
| 61 | + _.each(items, function (item) { |
| 62 | + item = $(item); |
| 63 | + if (item.data('_gridstack_node') || item[0].nodeType != 1) |
| 64 | + return; |
| 65 | + |
| 66 | + if (grid == null) { |
| 67 | + grid = $(componentInfo.element).find('.grid-stack').gridstack({ |
| 68 | + auto: false |
| 69 | + }).data('gridstack'); |
| 70 | + } |
| 71 | + |
| 72 | + grid.add_widget(item); |
| 73 | + ko.utils.domNodeDisposal.addDisposeCallback(item[0], function () { |
| 74 | + grid.remove_widget(item); |
| 75 | + }); |
| 76 | + }, this); |
| 77 | + }; |
| 78 | + }; |
| 79 | + |
| 80 | + return new ViewModel(controller, componentInfo); |
| 81 | + } |
| 82 | + }, |
| 83 | + template: |
| 84 | + [ |
| 85 | + '<div class="grid-stack" data-bind="foreach: {data: widgets, afterRender: afterAddWidget}">', |
| 86 | + ' <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}">', |
| 87 | + ' <div class="grid-stack-item-content" data-bind="text: $index"></div>', |
| 88 | + ' </div>', |
| 89 | + '</div>' |
| 90 | + ].join('\n') |
| 91 | + }); |
| 92 | + |
| 93 | + $(function () { |
| 94 | + var Controller = function (widgets) { |
| 95 | + this.widgets = ko.observableArray(widgets); |
| 96 | + }; |
| 97 | + |
| 98 | + var widgets = [ |
| 99 | + {x: 0, y: 0, width: 2, height: 2}, |
| 100 | + {x: 2, y: 0, width: 4, height: 2}, |
| 101 | + {x: 6, y: 0, width: 2, height: 4}, |
| 102 | + {x: 1, y: 2, width: 4, height: 2} |
| 103 | + ]; |
| 104 | + |
| 105 | + ko.applyBindings(new Controller(widgets)); |
| 106 | + }); |
| 107 | + </script> |
| 108 | +</body> |
| 109 | +</html> |
0 commit comments