1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 | 1 1 1 1 1 1 1 10 1 4 4 4 10 10 10 10 10 10 21 1 16 16 16 16 3 13 13 13 13 13 13 13 13 13 13 3 3 7 1 14 14 14 14 3 11 11 11 11 3 3 5 1 6 6 1 1 1 1 1 1 1 7 7 7 4 3 1 3 3 4 4 1 1 2 1 6 2 4 4 5 5 2 1 5 5 5 5 5 5 5 4 1 1 5 1 5 3 2 1 1 1 1 7 3 3 3 1 3 4 1 3 11 20 19 19 19 1 1 | 'use strict'; ;require.register("views/common/filter_view", function (exports, require, module) { /** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF 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. */ /** * Wrapper View for all filter components. Layout template and common actions are located inside of it. * Logic specific for data component(input, select, or custom multi select, which fire any changes on interface) are * located in inner view - <code>filterView</code>. * * If we want to have input filter, put <code>textFieldView</code> to it. * All inner views implemented below this view. * @type {*} */ var App = require('app'); var validator = require('utils/validator'); var wrapperView = Ember.View.extend({ classNames: ['view-wrapper'], layout: Ember.Handlebars.compile('{{yield}} <a href="#" {{action "clearFilter" target="view"}} class="ui-icon ui-icon-circle-close"></a>'), template: Ember.Handlebars.compile('{{#if view.fieldId}}<input type="hidden" id="{{unbound view.fieldId}}" value="" />{{/if}}' + '{{view view.filterView}}' + '{{#if view.showApply}}<button {{action "setValueOnApply" target="view"}} class="apply-btn btn btn-default"><span>{{t common.apply}}</span></button>{{/if}} '), value: null, /** * Column index */ column: null, /** * If this field is exists we dynamically create hidden input element and set value there. * Used for some cases, where this values will be used outside of component */ fieldId: null, /** * This option is useful for Em.Select, if you want get events when same option selected more than one time a raw. * This property is Object[] (for example see App.MainConfigHistoryView.modifiedFilterView), * that have followed structure: * * <code> * [ * { * values: ['DefinedValue1', 'DefinedValue2'], // this properties should be defined in `content` property * displayAs: 'Choose me' // this value will be displayed * } * ] * </code> * @type {Array} * **/ //TODO delete if will not be used triggeredOnSameValue: null, clearFilter: function clearFilter() { this.set('value', this.get('emptyValue')); if (this.get('setPropertyOnApply')) { this.setValueOnApply(); } return false; }, setValueOnApply: function setValueOnApply() { if (this.get('value') == null) { this.set('value', ''); } this.set(this.get('setPropertyOnApply'), this.get('value')); return false; }, /** * Use to determine whether filter is clear or not. Also when we want to set empty value */ emptyValue: '', /** * empty value that actually applied to filtering on server, * used with Select filter where displayed and actual filter value are different */ appliedEmptyValue: "", /** * Whether our <code>value</code> is empty or not * @return {Boolean} */ isEmpty: function isEmpty() { if (Em.isNone(this.get('value'))) { return true; } return this.get('value').toString() === this.get('emptyValue').toString(); }, setValue: function setValue(value) { this.set('value', value); }, /** * Show/Hide <code>Clear filter</code> button. * Also this method updates computed field related to <code>fieldId</code> if it exists. * Call <code>onChangeValue</code> callback when everything is done. */ showClearFilter: function () { if (!this.get('parentNode')) { return; } // get the sort view element in the same column to current filter view to highlight them together var relatedSort = $(this.get('element')).parents('thead').find('.sort-view-' + this.get('column')); if (this.isEmpty()) { this.get('parentNode').removeClass('active-filter'); this.get('parentNode').addClass('notActive'); relatedSort.removeClass('active-sort'); } else { this.get('parentNode').removeClass('notActive'); this.get('parentNode').addClass('active-filter'); relatedSort.addClass('active-sort'); } if (this.get('fieldId')) { this.$('> input').eq(0).val(this.get('value')); } this.onChangeValue(); }.observes('value'), /** * Callback for value changes */ onChangeValue: Em.K, /** * Filter components is located here. Should be redefined */ filterView: Em.View, /** * Update class of parentNode(hide clear filter button) on page load */ didInsertElement: function didInsertElement() { var parent = this.$().parent(); this.set('parentNode', parent); parent.addClass('notActive'); //TODO delete if will not be used //this.checkSelectSpecialOptions(); }, /** * Check for Em.Select that should use dispatching event when option with same value selected more than one time. **/ //TODO delete if will not be used checkSelectSpecialOptions: function checkSelectSpecialOptions() { // check predefined property if (!this.get('triggeredOnSameValue') || !this.get('triggeredOnSameValue').length) return; // add custom additional observer that will handle property changes this.addObserver('value', this, 'valueCustomObserver'); // get the full class attribute to find our select var classInlineAttr = this.get('fieldType').split(',').map(function (className) { return '.' + className.trim(); }).join(''); this.set('classInlineAttr', classInlineAttr); this.get('triggeredOnSameValue').forEach(function (triggeredValue) { triggeredValue.values.forEach(function (value, index) { // option with property `value` var $optionEl = $(this.get('element')).find(classInlineAttr).find('option[value="' + value + '"]'); // should be displayed with `displayAs` caption $optionEl.text(triggeredValue.displayAs); // the second one option should be hidden // as the result, on init stage we show only one option that could be selected if (index === 1) { $optionEl.css('display', 'none'); } }, this); }, this); }, /** * * Custom observer that used for special case of Em.Select related to dispatching event * when option with same value selected more than one time. * **/ //TODO delete if will not be used valueCustomObserver: function valueCustomObserver() { var hiddenValue; this.get('triggeredOnSameValue').forEach(function (triggeredValue) { var values = triggeredValue.values; // find current selected value from `values` list var currentValueIndex = values.indexOf(this.get('value')); if (currentValueIndex < 0) return; // value assigned to hidden option hiddenValue = values[Number(currentValueIndex === 0)]; }, this); if (hiddenValue) { // our select var $select = $(this.get('element')).find(this.get('classInlineAttr')); // now hide option with current value $select.find('option[value="{0}"]'.format(this.get('value'))).css('display', 'none'); // and show option that was hidden $select.find('option[value="{0}"]'.format(hiddenValue)).css('display', 'block'); } } }); /** * Simple input control for wrapperView */ var textFieldView = Ember.TextField.extend({ classNames: ['input-sm', 'form-control'], type: 'text', placeholder: Em.I18n.t('any'), valueBinding: "parentView.value" }); /** * Simple multiselect control for wrapperView. * Used to render blue button and popup, which opens on button click. * All content related logic should be implemented manually outside of it */ var componentFieldView = Ember.View.extend({ classNames: ['btn-group'], classNameBindings: ['isFilterOpen:open:'], /** * Whether popup is shown or not */ isFilterOpen: false, /** * We have <code>value</code> property similar to inputs <code>value</code> property */ valueBinding: 'parentView.value', /** * Clear filter to initial state */ clearFilter: function clearFilter() { this.set('value', ''); }, /** * Onclick handler for <code>cancel filter</code> button */ closeFilter: function closeFilter() { $(document).unbind('click'); this.set('isFilterOpen', false); }, /** * Onclick handler for <code>apply filter</code> button */ applyFilter: function applyFilter() { this.closeFilter(); }, /** * Onclick handler for <code>show component filter</code> button. * Also this function is used in some other places */ clickFilterButton: function clickFilterButton() { var self = this; this.toggleProperty('isFilterOpen'); if (this.get('isFilterOpen')) { var dropDown = this.$('.filter-components'); var firstClick = true; $(document).bind('click', function (e) { if (!firstClick && !$(e.target).closest(dropDown).length) { self.set('isFilterOpen', false); $(document).unbind('click'); } firstClick = false; }); } } }); /** * Simple select control for wrapperView */ var getSelectFieldView = function getSelectFieldView() { return App.DropdownView.extend({ selectionBinding: 'parentView.selected', contentBinding: 'parentView.content', optionValuePath: "value", optionLabelPath: "label" }); }; /** * Result object, which will be accessible outside * @type {Object} */ module.exports = { /** * You can access wrapperView outside */ wrapperView: wrapperView, /** * And also controls views if need it */ textFieldView: textFieldView, componentFieldView: componentFieldView, /** * Quick create input filters * @param config parameters of <code>wrapperView</code> */ createTextView: function createTextView(config) { config.fieldType = config.fieldType || 'input-medium'; config.filterView = textFieldView.extend({ classNames: [config.fieldType] }); return wrapperView.extend(config); }, /** * Quick create multiSelect filters * @param config parameters of <code>wrapperView</code> */ createComponentView: function createComponentView(config) { config.clearFilter = function () { this.forEachChildView(function (item) { if (item.clearFilter) { item.clearFilter(); } }); return false; }; return wrapperView.extend(config); }, /** * Quick create select filters * @param config parameters of <code>wrapperView</code> */ createSelectView: function createSelectView(config) { config.fieldType = config.fieldType || 'input-medium'; config.filterView = getSelectFieldView().extend({ classNames: config.fieldType.split(',').concat('display-inline-block'), attributeBindings: ['disabled', 'multiple'], disabled: false }); config.valueBinding = 'selected.value'; config.clearFilter = function () { this.set('selected', this.get('content').findProperty('value', this.get('emptyValue'))); }; config.setValue = function (value) { this.set('selected', this.get('content').findProperty('value', value)); }; return wrapperView.extend(config); }, /** * returns the filter function, which depends on the type of property * @param type * @param isGlobal check is search global * @return {Function} */ getFilterByType: function getFilterByType(type, isGlobal) { switch (type) { case 'ambari-bandwidth': return function (rowValue, rangeExp) { var compareChar = isNaN(rangeExp.charAt(0)) ? rangeExp.charAt(0) : false; var compareScale = rangeExp.charAt(rangeExp.length - 1); var compareValue = parseFloat(rangeExp.substr(compareChar ? 1 : 0)); if (rangeExp.length === 1 && compareChar !== false) { // User types only '=' or '>' or '<', so don't filter column values return true; } var oneSymbolScales = { k: 1024, m: 1048576, g: 1073741824 }; var twoSymbolsScales = { KB: 1024, MB: 1048576, GB: 1073741824 }; compareValue *= oneSymbolScales[compareScale] ? oneSymbolScales[compareScale] : oneSymbolScales.g; // default value in GB rowValue = jQuery(rowValue).text() ? jQuery(rowValue).text() : rowValue; var convertedRowValue; Iif (rowValue === '<1KB') { convertedRowValue = 1; } else { var rowValueScale = rowValue.substr(rowValue.length - 2, 2); Eif (twoSymbolsScales[rowValueScale]) { convertedRowValue = parseFloat(rowValue) * twoSymbolsScales[rowValueScale]; } } switch (compareChar) { case '<': return compareValue > convertedRowValue; case '>': return compareValue < convertedRowValue; case false: case '=': return compareValue === convertedRowValue; default: return false; } }; case 'duration': return function (rowValue, rangeExp) { var compareChar = isNaN(rangeExp.charAt(0)) ? rangeExp.charAt(0) : false; var compareScale = rangeExp.charAt(rangeExp.length - 1); var compareValue = parseFloat(rangeExp.substr(compareChar ? 1 : 0)); if (rangeExp.length === 1 && compareChar !== false) { // User types only '=' or '>' or '<', so don't filter column values return true; } var oneSymbolsScales = { s: 1000, m: 60000, h: 3600000 }; compareValue *= oneSymbolsScales[compareScale] ? oneSymbolsScales[compareScale] : oneSymbolsScales.s; rowValue = jQuery(rowValue).text() ? jQuery(rowValue).text() : rowValue; switch (compareChar) { case '<': return compareValue > rowValue; case '>': return compareValue < rowValue; case false: case '=': return compareValue == rowValue; default: return false; } }; case 'date': return function (rowValue, rangeExp) { var timePassed = App.dateTime() - rowValue; switch (rangeExp) { case 'Past 1 hour': return timePassed <= 3600000; case 'Past 1 Day': return timePassed <= 86400000; case 'Past 2 Days': return timePassed <= 172800000; case 'Past 7 Days': return timePassed <= 604800000; case 'Past 14 Days': return timePassed <= 1209600000; case 'Past 30 Days': return timePassed <= 2592000000; case 'Any': return true; default: return false; } }; case 'number': return function (rowValue, rangeExp) { var compareChar = rangeExp.charAt(0); var compareValue; if (rangeExp.length === 1) { if (isNaN(parseInt(compareChar, 10))) { // User types only '=' or '>' or '<', so don't filter column values return true; } compareValue = parseFloat(parseFloat(rangeExp).toFixed(2)); } else { Eif (isNaN(parseInt(compareChar, 10))) { compareValue = parseFloat(parseFloat(rangeExp.substr(1, rangeExp.length)).toFixed(2)); } else { compareValue = parseFloat(parseFloat(rangeExp.substr(0, rangeExp.length)).toFixed(2)); } } rowValue = parseFloat(jQuery(rowValue).text() ? jQuery(rowValue).text() : rowValue); switch (compareChar) { case '<': return compareValue > rowValue; case '>': return compareValue < rowValue; case '=': default: return compareValue === rowValue; } }; case 'sub-resource': return function (origin, compareValue) { if (!Array.isArray(compareValue) || !compareValue.length) { return true; } return origin.some(function (item) { for (var i = 0, l = compareValue.length; i < l; i++) { Iif (Array.isArray(compareValue[i].value)) { if (!compareValue[i].value.contains(item.get(compareValue[i].property))) return false; } else { if (item.get(compareValue[i].property) !== compareValue[i].value) return false; } } return true; }); }; case 'multiple': return function (origin, compareValue) { var options = compareValue.split(','); var rowValue = typeof origin === "string" ? origin : origin.mapProperty('componentName').join(" "); var str = new RegExp(compareValue, "i"); for (var i = 0; i < options.length; i++) { Eif (!isGlobal) { str = new RegExp('(\\W|^)' + options[i] + '(\\W|$)'); } if (rowValue.search(str) !== -1) { return true; } } return false; }; case 'boolean': case 'select': return function (origin, compareValue) { return origin === compareValue; }; case 'os': return function (origin, compareValue) { return origin.getEach('osType').contains(compareValue); }; case 'range': return function (origin, compareValue) { if (compareValue[1] && compareValue[0]) { return origin >= compareValue[0] && origin <= compareValue[1]; } if (compareValue[0]) { return origin >= compareValue[0]; } Eif (compareValue[1]) { return origin <= compareValue[1]; } return true; }; case 'alert_status': /** * origin - alertDefinition.summary * compareValue - "OK|WARNING|CRITICAL|UNKNOWN|PENDING" * PENDING means that OK is 0, WARNING is 0, CRITICAL is 0 and UNKNOWN is 0 */ return function (origin, compareValue) { if ('PENDING' === compareValue) { var isPending = true; Em.keys(origin).forEach(function (state) { if (origin[state] && (origin[state].count > 0 || origin[state].maintenanceCount > 0)) { isPending = false; } }); return isPending; } return !!origin[compareValue] && (origin[compareValue].count > 0 || origin[compareValue].maintenanceCount > 0); }; case 'alert_group': return function (origin, compareValue) { return origin.mapProperty('id').contains(compareValue); }; case 'enable_disable': return function (origin, compareValue) { return origin === (compareValue === 'enabled'); }; case 'file_extension': return function (origin, compareValue) { return origin.endsWith(compareValue); }; case 'string': default: return function (origin, compareValue) { if (validator.isValidMatchesRegexp(compareValue)) { var escapedCompareValue = compareValue.replace("(", "\\(").replace(")", "\\)").trim(); var regex = new RegExp(escapedCompareValue, "i"); return regex.test(origin); } return false; }; } }, getComputedServicesList: function getComputedServicesList() { return Em.computed('App.router.clusterController.isLoaded', function () { return [{ value: '', label: Em.I18n.t('common.all') }].concat(App.Service.find().map(function (service) { return { value: service.get('serviceName'), label: service.get('displayName') }; })).concat({ value: 'AMBARI', label: Em.I18n.t('app.name') }); }); } }; }); |