| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315 |
- // Licensed to Cloudera, Inc. under one
- // or more contributor license agreements. See the NOTICE file
- // distributed with this work for additional information
- // regarding copyright ownership. Cloudera, Inc. licenses this file
- // to you 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.
- function s4() {
- return Math.floor((1 + Math.random()) * 0x10000)
- .toString(16)
- .substring(1);
- }
- function UUID() {
- return s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4();
- }
- var Column = function (size, rows) {
- var self = this;
- self.id = ko.observable(UUID());
- self.size = ko.observable(size);
- self.rows = ko.observableArray(rows);
- self.oozieStartRow = ko.computed(function () {
- var _row = null;
- ko.utils.arrayForEach(self.rows(), function (row) {
- if ((row.widgets().length > 0 && row.widgets()[0].widgetType() == "start-widget")) {
- _row = row;
- }
- });
- return _row;
- }, self);
- self.oozieEndRow = ko.computed(function () {
- var _row = null;
- ko.utils.arrayForEach(self.rows(), function (row) {
- if ((row.widgets().length > 0 && row.widgets()[0].widgetType() == "end-widget")) {
- _row = row;
- }
- });
- return _row;
- }, self);
- self.oozieRows = ko.computed(function () {
- var _rows = [];
- ko.utils.arrayForEach(self.rows(), function (row) {
- if ((row.widgets().length > 0 && row.widgets()[0].widgetType() != "start-widget" && row.widgets()[0].widgetType() != "end-widget") || row.widgets().length == 0) {
- _rows.push(row);
- }
- });
- return _rows;
- }, self);
- self.enableOozieDropOnBefore = ko.observable(true);
- self.enableOozieDropOnAfter = ko.observable(true);
- self.drops = ko.observableArray(["temp"]);
- self.klass = ko.computed(function () {
- return "card card-home card-column span" + self.size();
- });
- self.addEmptyRow = function (atBeginning, atIndex) {
- return self.addRow(null, atBeginning, atIndex);
- };
- self.addRow = function (row, atBeginning, atIndex) {
- if (typeof row == "undefined" || row == null) {
- row = new Row([], viewModel); // Hacky but needed when a new row is deleted
- }
- if (typeof atIndex != "undefined" && atIndex != null) {
- self.rows.splice(atIndex, 0, row);
- }
- else {
- if (typeof atBeginning == "undefined" || atBeginning == null || !atBeginning) {
- self.rows.push(row);
- }
- else {
- self.rows.unshift(row);
- }
- }
- return row;
- };
- }
- var Row = function (widgets, vm, columns) {
- var self = this;
- self.id = ko.observable(UUID());
- self.widgets = ko.observableArray(widgets);
- self.columns = ko.observableArray(columns ? columns : []);
- self.enableOozieDrop = ko.computed(function(){
- return vm.isEditing && vm.isEditing() && self.widgets && self.widgets().length < 1
- });
- self.enableOozieDropOnBefore = ko.observable(true);
- self.enableOozieDropOnSide = ko.observable(true);
- self.addWidget = function (widget) {
- self.widgets.push(widget);
- };
- self.addEmptyColumn = function (atBeginning) {
- if (self.columns().length == 0){
- var _col = self.addColumn(null, atBeginning);
- if (self.widgets().length > 0){
- var _row = _col.addEmptyRow();
- self.widgets().forEach(function(widget){
- _row.addWidget(widget);
- });
- self.widgets([]);
- }
- }
- return self.addColumn(null, atBeginning);
- };
- self.addColumn = function (column, atBeginning) {
- if (typeof column == "undefined" || column == null) {
- var _size = Math.max(1, Math.floor(12 / (self.columns().length + 1)));
- column = new Column(_size, []); // Hacky but needed when a new row is deleted
- self.columns().forEach(function(col){
- col.size(_size);
- });
- }
- if (typeof atBeginning == "undefined" || atBeginning == null || ! atBeginning) {
- self.columns.push(column);
- }
- else {
- self.columns.unshift(column);
- }
- return column;
- };
- self.move = function (from, to) {
- try {
- vm.columns()[to].addRow(self);
- vm.columns()[from].rows.remove(self);
- }
- catch (exception) {
- }
- }
- self.moveDown = function (col, row) {
- var _i = col.rows().indexOf(row);
- if (_i < col.rows().length - 1) {
- var _arr = col.rows();
- col.rows.splice(_i, 2, _arr[_i + 1], _arr[_i]);
- }
- }
- self.moveUp = function (col, row) {
- var _i = col.rows().indexOf(row);
- if (_i >= 1) {
- var _arr = col.rows();
- col.rows.splice(_i - 1, 2, _arr[_i], _arr[_i - 1]);
- }
- }
- self.remove = function (col, row) {
- $.each(self.columns(), function (i, column) {
- $.each(column.rows(), function (j, row) {
- $.each(row.widgets(), function (k, widget) {
- vm.removeWidget(widget);
- });
- });
- });
- $.each(self.widgets(), function (i, widget) {
- vm.removeWidget(widget);
- });
- col.rows.remove(row);
- }
- self.autosizeWidgets = function () {
- $.each(self.widgets(), function (i, widget) {
- widget.size(Math.floor(12 / self.widgets().length));
- });
- }
- }
- // A widget is generic. It has an id that refer to another object (e.g. facet) with the same id.
- var Widget = function (params) {
- var self = this;
- self.size = ko.observable(params.size).extend({ numeric: 0 });
- self.name = ko.observable(params.name);
- self.id = ko.observable(params.id);
- self.widgetType = ko.observable(typeof params.widgetType != "undefined" && params.widgetType != null ? params.widgetType : "empty-widget");
- self.properties = ko.observable(typeof params.properties != "undefined" && params.properties != null ? params.properties : {});
- self.offset = ko.observable(typeof params.offset != "undefined" && params.offset != null ? params.offset : 0).extend({ numeric: 0 });
- self.isLoading = ko.observable(typeof params.loading != "undefined" && params.loading != null ? params.loading : false);
- self.oozieMovable = ko.computed(function() {
- return ["end-widget", "start-widget", "fork-widget", "decision-widget", "kill-widget", "join-widget"].indexOf(self.widgetType()) == - 1
- });
- self.klass = ko.computed(function () {
- return "card card-widget span" + self.size() + (self.offset() * 1 > 0 ? " offset" + self.offset() : "");
- });
- self.expand = function () {
- self.size(self.size() + 1);
- $("#wdg_" + self.id()).trigger("resize");
- }
- self.compress = function () {
- self.size(self.size() - 1);
- $("#wdg_" + self.id()).trigger("resize");
- }
- self.moveLeft = function () {
- self.offset(self.offset() - 1);
- }
- self.moveRight = function () {
- self.offset(self.offset() + 1);
- }
- self.remove = function (row, widget) {
- if (params.vm != null) {
- params.vm.removeWidget(widget);
- }
- row.widgets.remove(widget);
- }
- };
- Widget.prototype.clone = function () {
- return new Widget({
- size: this.size(),
- id: UUID(),
- name: this.name(),
- widgetType: this.widgetType()
- });
- };
- function fullLayout(vm) {
- setLayout([12], vm);
- }
- function oneSixthLeftLayout(vm) {
- setLayout([2, 10], vm);
- }
- function oneFourthLeftLayout(vm) {
- setLayout([3, 9], vm);
- }
- function oneThirdLeftLayout(vm) {
- setLayout([4, 8], vm);
- }
- function halfHalfLayout(vm) {
- setLayout([6, 6], vm);
- }
- function oneThirdRightLayout(vm) {
- setLayout([8, 4], vm);
- }
- function oneFourthRightLayout(vm) {
- setLayout([9, 3], vm);
- }
- function oneSixthRightLayout(vm) {
- setLayout([10, 2], vm);
- }
- function setLayout(colSizes, vm) {
- // Save previous widgets
- var _allRows = [];
- $(vm.columns()).each(function (cnt, col) {
- var _tRows = [];
- $(col.rows()).each(function (icnt, row) {
- if (row.widgets().length > 0 || (typeof vm.isNested != "undefined" && vm.isNested())) {
- _tRows.push(row);
- }
- });
- _allRows = _allRows.concat(_tRows);
- });
- var _cols = [];
- var _highestCol = {
- idx: -1,
- size: -1
- };
- $(colSizes).each(function (cnt, size) {
- _cols.push(new Column(size, []));
- if (size > _highestCol.size) {
- _highestCol.idx = cnt;
- _highestCol.size = size;
- }
- });
- if (_allRows.length > 0 && _highestCol.idx > -1) {
- _cols[_highestCol.idx].rows(_allRows);
- }
- $(_cols).each(function (cnt, col) {
- if (col.rows().length == 0) {
- col.rows([new Row([], vm)]);
- }
- });
- vm.columns(_cols);
- $(document).trigger("setLayout");
- }
|