|
| 1 | +(function () { |
| 2 | + 'use strict'; |
| 3 | + |
| 4 | + angular.module('letsAngular') |
| 5 | + .directive('fwMultiSelect', fwMultiSelect); |
| 6 | + |
| 7 | + fwMultiSelect.$inject = ['$compile', '$timeout', 'Restangular', '$state']; |
| 8 | + |
| 9 | + function fwMultiSelect($compile, $timeout, Restangular) { |
| 10 | + return { |
| 11 | + restrict: 'A', |
| 12 | + priority: 1, |
| 13 | + link: function (scope, element) { |
| 14 | + |
| 15 | + scope.dataReference = $(element) |
| 16 | + |
| 17 | + // var _input = element.find('input'); |
| 18 | + |
| 19 | + // var clickHandler = function () { |
| 20 | + // var _oldVal = _input.val(); |
| 21 | + // var _val = _oldVal + ' '; |
| 22 | + // _input.controller('ngModel').$setViewValue(_val); |
| 23 | + // // scope.$digest; |
| 24 | + // $timeout(function(){ |
| 25 | + // _input.controller('ngModel').$setViewValue(_oldVal); |
| 26 | + // }); |
| 27 | + // }; |
| 28 | + |
| 29 | + // element.find('button').click(clickHandler); |
| 30 | + // _input.click(clickHandler); |
| 31 | + |
| 32 | + // _input.keyup(function(){ |
| 33 | + // if (this.value.trim()==""){ |
| 34 | + // _input.scope().data[_input.attr('name')] = null; |
| 35 | + // } |
| 36 | + // }) |
| 37 | + |
| 38 | + |
| 39 | + |
| 40 | + }, |
| 41 | + controller: function ($scope, $state) { |
| 42 | + /*Multiselect*/ |
| 43 | + $scope.initMultiSelect = false; |
| 44 | + $scope.msmodel = []; |
| 45 | + $scope.filter = {}; |
| 46 | + $scope.msdata = []; |
| 47 | + $scope.setting = {} |
| 48 | + var field = '' |
| 49 | + |
| 50 | + $scope.$on('filter-init', function(scope){ |
| 51 | + var data = scope.targetScope.data || undefined |
| 52 | + var filter = $($scope.dataReference).attr('data-reference') || undefined |
| 53 | + field = filter |
| 54 | + |
| 55 | + if(filter && data){ |
| 56 | + $scope.msmodel = angular.copy(data[filter]) || [] |
| 57 | + } |
| 58 | + }) |
| 59 | + |
| 60 | + // Eventos da biblioteca 'angularjs-dropdown-multiselect' |
| 61 | + |
| 62 | + $scope.changedMultiSelect = function (a) { |
| 63 | + if ($scope.msmodel.length) { |
| 64 | + angular.element('.fw-multiselect-button').css('color', '#555555') |
| 65 | + } else { |
| 66 | + angular.element('.fw-multiselect-button').css('color', '#CCC') |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + $scope.onItemSelect = function (item, $event) { |
| 71 | + |
| 72 | + $scope.data[field] = $scope.msmodel |
| 73 | + var _label = $scope.msdata.find(function (_item) { |
| 74 | + return _item.id == item.id |
| 75 | + }) |
| 76 | + item.label = _label.label |
| 77 | + } |
| 78 | + |
| 79 | + $scope.onItemDeselect = function (item) { |
| 80 | + |
| 81 | + } |
| 82 | + |
| 83 | + // Fim dos eventos |
| 84 | + |
| 85 | + $scope.removeDuplicates= function (a, param){ |
| 86 | + return a.filter(function(item, pos, array){ |
| 87 | + return array.map(function(mapItem){ return mapItem[param]; }).indexOf(item[param]) === pos; |
| 88 | + }) |
| 89 | + } |
| 90 | + |
| 91 | + $scope.makeRequestAutocomplete = function (scope, value, route) { |
| 92 | + $timeout(function() { |
| 93 | + if(!value) value = '[blank]' |
| 94 | + $scope.resource = Restangular.all($scope.route()); |
| 95 | + $scope.resource.customGET('autocomplete/'+route+'/'+value+'?limit=10').then(function (data) { |
| 96 | + scope.options = $scope.removeDuplicates(data.concat(scope.selectedModel),'id'); |
| 97 | + |
| 98 | + $timeout(function() {},0) |
| 99 | + }, function errorCallback() { |
| 100 | + }); |
| 101 | + }) |
| 102 | + } |
| 103 | + |
| 104 | + $scope._debounce = function(cb) { |
| 105 | + var timeout = null; |
| 106 | + return function(data) { |
| 107 | + if (timeout) { |
| 108 | + clearTimeout(timeout); |
| 109 | + } |
| 110 | + timeout = setTimeout(function() { |
| 111 | + cb(data); |
| 112 | + }, 200); |
| 113 | + }; |
| 114 | + }; |
| 115 | + |
| 116 | + $scope.openDropdownByButton = function(name){ |
| 117 | + $timeout(function() { |
| 118 | + $('[data-reference="'+name+'"] button').click() |
| 119 | + }) |
| 120 | + } |
| 121 | + |
| 122 | + // Inicialização e set e eventos |
| 123 | + $scope.onInitMulti = function (event, field) { |
| 124 | + var dropdown = $(event.target) |
| 125 | + dropdown.scope().input.searchFilter = ""; |
| 126 | + |
| 127 | + if (!dropdown.initMultiSelect) { |
| 128 | + var _scope = dropdown.scope() |
| 129 | + dropdown.initMultiSelect = true |
| 130 | + // Popular o msdata pela depois de iniciado |
| 131 | + $scope.makeRequestAutocomplete(_scope,'[blank]', field.name) |
| 132 | + dropdown.parent().find(".dropdown-header").append('<i class="glyphicon glyphicon-search" style=" position: absolute; top: 20px; right: 35px; "></i>') |
| 133 | + |
| 134 | + // Chamar autocomplete toda vez que alguma coisa é digitada no search-filter |
| 135 | + _scope.$watch('input.searchFilter', $scope._debounce(function(data) { |
| 136 | + $scope.makeRequestAutocomplete(_scope,data,field.name) |
| 137 | + })) |
| 138 | + |
| 139 | + // sim, precisa dessa gambiarra pra chumbar os evento e os textos |
| 140 | + |
| 141 | + //set texts |
| 142 | + _scope.texts.buttonDefaultText = "Selecione " + field.label |
| 143 | + _scope.texts.searchPlaceholder = "Buscar " + field.label |
| 144 | + _scope.texts.dynamicButtonTextSuffix = "selecionado(s)" |
| 145 | + |
| 146 | + _scope.externalEvents.onItemSelect = $scope.onItemSelect |
| 147 | + _scope.externalEvents.onSelectionChanged = $scope.changedMultiSelect |
| 148 | + _scope.externalEvents.onItemDeselect = $scope.onItemDeselect |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + // settings que foram chumbadas no html |
| 153 | + // TODO: fix this ... someday ... |
| 154 | + $scope.mssettings = { |
| 155 | + scrollableHeight: '200px', |
| 156 | + scrollable: true, |
| 157 | + buttonDefaultText: 'Tipos de imóveis', |
| 158 | + enableSearch: true, |
| 159 | + styleActive: true, |
| 160 | + showCheckAll: false, |
| 161 | + showUncheckAll: false, |
| 162 | + selectedToTop: true, |
| 163 | + buttonClasses: 'btn btn-default fw-multiselect-button', |
| 164 | + // smartButtonTextConverter: function (itemText, originalItem) { |
| 165 | + // // if (itemText === 'Jhon') { |
| 166 | + // // return 'Jhonny!'; |
| 167 | + // // } |
| 168 | + // return itemText; |
| 169 | + // } |
| 170 | + }; |
| 171 | + |
| 172 | + /*Multiselect End*/ |
| 173 | + }, |
| 174 | + }; |
| 175 | + } |
| 176 | +})(); |
0 commit comments