-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from modulr/dev
Update 1.0.6
- Loading branch information
Showing
5 changed files
with
327 additions
and
291 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,162 +1,180 @@ | ||
(function(){ | ||
'use strict'; | ||
'use strict'; | ||
|
||
angular | ||
.module('mdr.select2', []) | ||
.directive('mdrSelect2', [function(){ | ||
/** | ||
* @param url {string} | ||
* @param src {array} | ||
* @param options {string} | ||
* @param model {object} | ||
* @param selected {object} | ||
* @param allow-clear {boolean} | ||
* @param placeholder {string} | ||
* @param disabled {boolean} | ||
* @param required {boolean} | ||
*/ | ||
return { | ||
restrict: 'E', | ||
controller: 'mdrSelect2Ctrl', | ||
scope: { | ||
url: '@', | ||
src: '=', | ||
options: '@', | ||
model: '=', | ||
selected: '=', | ||
allowClear: '@', | ||
placeholder: '@', | ||
disabled: '=', | ||
required: '=' | ||
}, | ||
template: '<select class="form-control select2" id="selectId_{{$id}}" ng-options="{{options}}" ng-model="model" ng-disabled="disabled" ng-required="required"><option></option></select>' | ||
}; | ||
}]) | ||
.controller('mdrSelect2Ctrl', ['$scope', '$element', '$attrs', 'mdrSelect2Service', function($scope, $element, $attrs, mdrSelect2Service){ | ||
angular | ||
.module('mdr.select2', []) | ||
.directive('mdrSelect2', [function(){ | ||
/** | ||
* @param url {string} | ||
* @param src {array} | ||
* @param options {string} | ||
* @param model {object} | ||
* @param selected {object} | ||
* @param allow-clear {boolean} | ||
* @param placeholder {string} | ||
* @param disabled {boolean} | ||
* @param required {boolean} | ||
*/ | ||
return { | ||
restrict: 'E', | ||
controller: 'mdrSelect2Ctrl', | ||
scope: { | ||
url: '@', | ||
src: '=', | ||
options: '@', | ||
model: '=', | ||
selected: '=', | ||
allowClear: '@', | ||
placeholder: '@', | ||
disabled: '=', | ||
required: '=' | ||
}, | ||
template: '<select class="form-control select2" id="selectId_{{$id}}" ng-options="{{options}}" ng-model="model" ng-disabled="disabled" ng-required="required"><option></option></select>' | ||
}; | ||
}]) | ||
.controller('mdrSelect2Ctrl', ['$scope', '$element', '$attrs', 'mdrSelect2Service', function($scope, $element, $attrs, mdrSelect2Service){ | ||
|
||
$scope.placeholder = 'Select'; | ||
$scope.allowClear = false; | ||
$scope.placeholder = 'Select'; | ||
$scope.allowClear = false; | ||
|
||
initialize(); | ||
initialize(); | ||
|
||
// Cuando cambia el scope url se cargan los datos | ||
// When scope url change data is loaded | ||
$scope.$watch('url', function(newValue, oldValue) | ||
{ | ||
if(newValue !== undefined){ | ||
|
||
// Cuando cambia url se cargan los datos | ||
$scope.$watch('url', function(newValue, oldValue) | ||
{ | ||
if(newValue !== undefined){ | ||
var lastChar = newValue.substr(-1); | ||
// Si el ultimo caracter no es una barra | ||
// If lastChar is not a slash | ||
if (lastChar !== '/') { | ||
|
||
var options = getOptions(); | ||
$scope[options.collection] = []; | ||
setPlaceholder('Loading...'); | ||
var options = getOptions(); | ||
$scope[options.collection] = []; | ||
setPlaceholder('Loading...'); | ||
|
||
mdrSelect2Service.find(newValue) | ||
.then(function (data) { | ||
if (data.length <= 0 || data.length === undefined){ | ||
setPlaceholder('No results found'); | ||
} else { | ||
$scope[options.collection] = data; | ||
initialize(); | ||
selected($scope.selected); | ||
} | ||
}) | ||
.catch(function (error) { | ||
setPlaceholder('No results found'); | ||
mdrSelect2Service.find(newValue) | ||
.then(function (data) { | ||
if (data.length <= 0 || data.length === undefined){ | ||
setPlaceholder('No results found'); | ||
} else { | ||
$scope[options.collection] = data; | ||
initialize(); | ||
selected($scope.selected); | ||
} | ||
}) | ||
.catch(function (error) { | ||
setPlaceholder('No results found'); | ||
}); | ||
} | ||
} | ||
}); | ||
} | ||
}); | ||
// Cuando cambia src se cargan los datos | ||
$scope.$watchCollection('src', function(newValue, oldValue) | ||
{ | ||
if(newValue !== undefined){ | ||
var options = getOptions(); | ||
$scope[options.collection] = newValue; | ||
initialize(); | ||
selected($scope.selected); | ||
} | ||
}); | ||
// Cuando cambia model | ||
$scope.$watch('model', function(newValue, oldValue) | ||
{ | ||
// Se inicializa el modelo | ||
if(newValue === undefined || newValue === null){ | ||
$scope.model = {}; | ||
setTimeout(function() { | ||
$("#selectId_" + $scope.$id).val('').trigger('change.select2'); | ||
},0); | ||
} | ||
}); | ||
// Cuando cambia selected se manda llamar la funcion selected | ||
$scope.$watch('selected', function(newValue, oldValue) | ||
{ | ||
selected(newValue); | ||
}); | ||
// Se crea el metodo que inicializa el select2 | ||
function initialize() | ||
{ | ||
setTimeout(function() { | ||
$("#selectId_" + $scope.$id).select2({ | ||
placeholder: $scope.placeholder, | ||
allowClear: $scope.allowClear | ||
// Cuando cambia el scope src se cargan los datos | ||
// When scope src change data is loaded | ||
$scope.$watchCollection('src', function(newValue, oldValue) | ||
{ | ||
if(newValue !== undefined){ | ||
var options = getOptions(); | ||
$scope[options.collection] = newValue; | ||
initialize(); | ||
selected($scope.selected); | ||
} | ||
}); | ||
// Cuando cambia el scope model | ||
// When scope model change | ||
$scope.$watch('model', function(newValue, oldValue) | ||
{ | ||
// Si el scope modelo es undefined o null | ||
// If scope model is undefined or null | ||
if(newValue === undefined || newValue === null){ | ||
// Se inicializa (limpia) el modelo y el elemento select2 | ||
// Model and select2 element initialize (clear) | ||
$scope.model = {}; | ||
setTimeout(function() { | ||
$("#selectId_" + $scope.$id).val('').trigger('change.select2'); | ||
},0); | ||
} | ||
}); | ||
},0); | ||
} | ||
// Se crea el metodo que cambia el placeholder del select2 | ||
function setPlaceholder(placeholder) | ||
{ | ||
setTimeout(function() { | ||
$("#selectId_" + $scope.$id).select2({ | ||
placeholder: placeholder | ||
// Cuando cambia selected se manda llamar la funcion selected | ||
// When scope selected change selected function is called | ||
$scope.$watch('selected', function(newValue, oldValue) | ||
{ | ||
selected(newValue); | ||
}); | ||
},0); | ||
} | ||
// Selecciona el model(ng-model) y el val en el select2 | ||
function selected(value) | ||
{ | ||
if(value !== undefined){ | ||
var options = getOptions(); | ||
angular.forEach($scope[options.collection], function(val, key){ | ||
if(value == val[options.track]){ | ||
$scope.model = val; | ||
// Metodo que inicializa el elemento select2 | ||
// Method initialize select2 element | ||
function initialize() | ||
{ | ||
setTimeout(function() { | ||
$("#selectId_" + $scope.$id).val(value).trigger("change"); | ||
$("#selectId_" + $scope.$id).select2({ | ||
placeholder: $scope.placeholder, | ||
allowClear: $scope.allowClear | ||
}); | ||
},0); | ||
return false; | ||
} | ||
}); | ||
} | ||
} | ||
// Se crea el metodo que obtiene el nombre de la coleccion de options | ||
function getOptions() | ||
{ | ||
var parts = $scope.options.split(' '); | ||
var posCollection = parts.indexOf('in'); | ||
} | ||
// Metodo que establece el placeholder del elemento select2 | ||
// Method establish placeholder in select2 element | ||
function setPlaceholder(placeholder) | ||
{ | ||
setTimeout(function() { | ||
$("#selectId_" + $scope.$id).select2({ | ||
placeholder: placeholder | ||
}); | ||
},0); | ||
} | ||
// Metodo que asigna el ng-model y el val en el elemento select2 | ||
// Method assign ng-model value and select2 element val | ||
function selected(value) | ||
{ | ||
if(value !== undefined){ | ||
var options = getOptions(); | ||
angular.forEach($scope[options.collection], function(val, key){ | ||
if(value == val[options.track]){ | ||
$scope.model = val; | ||
setTimeout(function() { | ||
$("#selectId_" + $scope.$id).val(value).trigger("change"); | ||
},0); | ||
return false; | ||
} | ||
}); | ||
} | ||
} | ||
// Metodo que obtiene las opciones (coleccion y track) | ||
// Method get options (collection and track) | ||
function getOptions() | ||
{ | ||
var parts = $scope.options.split(' '); | ||
var posCollection = parts.indexOf('in'); | ||
|
||
var posTrack = parts.indexOf('track'); | ||
var track = parts[posTrack+2].split('.'); | ||
var posTrack = parts.indexOf('track'); | ||
var track = parts[posTrack+2].split('.'); | ||
|
||
var options = { | ||
collection: parts[posCollection+1], | ||
track: track[1] | ||
}; | ||
return options; | ||
} | ||
}]) | ||
.factory('mdrSelect2Service', ['$http', '$q', function($http, $q){ | ||
return{ | ||
// Se obtiene la coleccion de la db | ||
find:function(url){ | ||
var deferred = $q.defer(); | ||
var options = { | ||
collection: parts[posCollection+1], | ||
track: track[1] | ||
}; | ||
return options; | ||
} | ||
}]) | ||
.factory('mdrSelect2Service', ['$http', '$q', function($http, $q){ | ||
return{ | ||
// Se obtiene la coleccion del API | ||
// Get collection from API | ||
find:function(url){ | ||
var deferred = $q.defer(); | ||
|
||
$http.get(url) | ||
.success(function(data){ | ||
deferred.resolve(data); | ||
}) | ||
.error(function(error){ | ||
deferred.reject(error); | ||
}); | ||
$http.get(url) | ||
.success(function(data){ | ||
deferred.resolve(data); | ||
}) | ||
.error(function(error){ | ||
deferred.reject(error); | ||
}); | ||
|
||
return deferred.promise; | ||
} | ||
}; | ||
}]); | ||
return deferred.promise; | ||
} | ||
}; | ||
}]); | ||
|
||
})(); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.