Skip to content

Commit

Permalink
clean and upgrade dev dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzofox3 committed Jun 12, 2017
1 parent 3edc0f8 commit d46e48b
Show file tree
Hide file tree
Showing 9 changed files with 3,307 additions and 184 deletions.
149 changes: 84 additions & 65 deletions dist/smart-table.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
/**
* @version 2.1.8
* @license MIT
*/
(function (ng, undefined){
'use strict';

Expand Down Expand Up @@ -39,8 +35,12 @@ ng.module('smart-table')
delay: 100 //ms
}
});
ng.module('smart-table')
.controller('stTableController', ['$scope', '$parse', '$filter', '$attrs', function StTableController ($scope, $parse, $filter, $attrs) {
ng.module('smart-table').controller('stTableController', [
'$scope',
'$parse',
'$filter',
'$attrs',
function StTableController($scope, $parse, $filter, $attrs) {
var propertyName = $attrs.stTable;
var displayGetter = $parse(propertyName);
var displaySetter = displayGetter.assign;
Expand All @@ -51,33 +51,30 @@ ng.module('smart-table')
var tableState = {
sort: {},
search: {},
pagination: {
start: 0,
totalItemCount: 0
}
pagination: { start: 0, totalItemCount: 0 }
};
var filtered;
var pipeAfterSafeCopy = true;
var ctrl = this;
var lastSelected;

function copyRefs (src) {
function copyRefs(src) {
return src ? [].concat(src) : [];
}

function updateSafeCopy () {
function updateSafeCopy() {
safeCopy = copyRefs(safeGetter($scope));
if (pipeAfterSafeCopy === true) {
ctrl.pipe();
}
}

function deepDelete (object, path) {
function deepDelete(object, path) {
if (path.indexOf('.') != -1) {
var partials = path.split('.');
var key = partials.pop();
var parentPath = partials.join('.');
var parentObject = $parse(parentPath)(object)
var parentObject = $parse(parentPath)(object);
delete parentObject[key];
if (Object.keys(parentObject).length == 0) {
deepDelete(object, parentPath);
Expand All @@ -89,38 +86,47 @@ ng.module('smart-table')

if ($attrs.stSafeSrc) {
safeGetter = $parse($attrs.stSafeSrc);
$scope.$watch(function () {
var safeSrc = safeGetter($scope);
return safeSrc && safeSrc.length ? safeSrc[0] : undefined;
}, function (newValue, oldValue) {
if (newValue !== oldValue) {
updateSafeCopy();
$scope.$watch(
function() {
var safeSrc = safeGetter($scope);
return safeSrc && safeSrc.length ? safeSrc[0] : undefined;
},
function(newValue, oldValue) {
if (newValue !== oldValue) {
updateSafeCopy();
}
}
});
$scope.$watch(function () {
var safeSrc = safeGetter($scope);
return safeSrc ? safeSrc.length : 0;
}, function (newValue, oldValue) {
if (newValue !== safeCopy.length) {
updateSafeCopy();
);
$scope.$watch(
function() {
var safeSrc = safeGetter($scope);
return safeSrc ? safeSrc.length : 0;
},
function(newValue, oldValue) {
if (newValue !== safeCopy.length) {
updateSafeCopy();
}
}
});
$scope.$watch(function () {
return safeGetter($scope);
}, function (newValue, oldValue) {
if (newValue !== oldValue) {
tableState.pagination.start = 0;
updateSafeCopy();
);
$scope.$watch(
function() {
return safeGetter($scope);
},
function(newValue, oldValue) {
if (newValue !== oldValue) {
tableState.pagination.start = 0;
updateSafeCopy();
}
}
});
);
}

/**
* sort the rows
* @param {Function | String} predicate - function or string which will be used as predicate for the sorting
* @param [reverse] - if you want to reverse the order
*/
this.sortBy = function sortBy (predicate, reverse) {
this.sortBy = function sortBy(predicate, reverse) {
tableState.sort.predicate = predicate;
tableState.sort.reverse = reverse === true;

Expand All @@ -138,8 +144,9 @@ ng.module('smart-table')
* search matching rows
* @param {String} input - the input string
* @param {String} [predicate] - the property name against you want to check the match, otherwise it will search on all properties
* @param {String | Function } [comparator] - a comparator to pass to the filter for the (pass true for stric mode)
*/
this.search = function search (input, predicate) {
this.search = function search(input, predicate, comparator) {
var predicateObject = tableState.search.predicateObject || {};
var prop = predicate ? predicate : '$';

Expand All @@ -157,18 +164,31 @@ ng.module('smart-table')
/**
* this will chain the operations of sorting and filtering based on the current table state (sort options, filtering, ect)
*/
this.pipe = function pipe () {
this.pipe = function pipe() {
var pagination = tableState.pagination;
var output;
filtered = tableState.search.predicateObject ? filter(safeCopy, tableState.search.predicateObject) : safeCopy;
filtered = tableState.search.predicateObject
? filter(safeCopy, tableState.search.predicateObject)
: safeCopy;
if (tableState.sort.predicate) {
filtered = orderBy(filtered, tableState.sort.predicate, tableState.sort.reverse);
filtered = orderBy(
filtered,
tableState.sort.predicate,
tableState.sort.reverse
);
}
pagination.totalItemCount = filtered.length;
if (pagination.number !== undefined) {
pagination.numberOfPages = filtered.length > 0 ? Math.ceil(filtered.length / pagination.number) : 1;
pagination.start = pagination.start >= filtered.length ? (pagination.numberOfPages - 1) * pagination.number : pagination.start;
output = filtered.slice(pagination.start, pagination.start + parseInt(pagination.number));
pagination.numberOfPages = filtered.length > 0
? Math.ceil(filtered.length / pagination.number)
: 1;
pagination.start = pagination.start >= filtered.length
? (pagination.numberOfPages - 1) * pagination.number
: pagination.start;
output = filtered.slice(
pagination.start,
pagination.start + parseInt(pagination.number)
);
}
displaySetter($scope, output || filtered);
};
Expand All @@ -178,7 +198,7 @@ ng.module('smart-table')
* @param {Object} row - the row to select
* @param {String} [mode] - "single" or "multiple" (multiple by default)
*/
this.select = function select (row, mode) {
this.select = function select(row, mode) {
var rows = copyRefs(displayGetter($scope));
var index = rows.indexOf(row);
if (index !== -1) {
Expand All @@ -200,7 +220,7 @@ ng.module('smart-table')
* @param {Number} start - start index of the slice
* @param {Number} number - the number of item in the slice
*/
this.slice = function splice (start, number) {
this.slice = function splice(start, number) {
tableState.pagination.start = start;
tableState.pagination.number = number;
return this.pipe();
Expand All @@ -210,54 +230,53 @@ ng.module('smart-table')
* return the current state of the table
* @returns {{sort: {}, search: {}, pagination: {start: number}}}
*/
this.tableState = function getTableState () {
this.tableState = function getTableState() {
return tableState;
};

this.getFilteredCollection = function getFilteredCollection () {
this.getFilteredCollection = function getFilteredCollection() {
return filtered || safeCopy;
};

/**
* Use a different filter function than the angular FilterFilter
* @param filterName the name under which the custom filter is registered
*/
this.setFilterFunction = function setFilterFunction (filterName) {
this.setFilterFunction = function setFilterFunction(filterName) {
filter = $filter(filterName);
};

/**
* Use a different function than the angular orderBy
* @param sortFunctionName the name under which the custom order function is registered
*/
this.setSortFunction = function setSortFunction (sortFunctionName) {
this.setSortFunction = function setSortFunction(sortFunctionName) {
orderBy = $filter(sortFunctionName);
};

/**
* Usually when the safe copy is updated the pipe function is called.
* Calling this method will prevent it, which is something required when using a custom pipe function
*/
this.preventPipeOnWatch = function preventPipe () {
this.preventPipeOnWatch = function preventPipe() {
pipeAfterSafeCopy = false;
};
}])
.directive('stTable', function () {
return {
restrict: 'A',
controller: 'stTableController',
link: function (scope, element, attr, ctrl) {

if (attr.stSetFilter) {
ctrl.setFilterFunction(attr.stSetFilter);
}
}
]).directive('stTable', function() {
return {
restrict: 'A',
controller: 'stTableController',
link: function(scope, element, attr, ctrl) {
if (attr.stSetFilter) {
ctrl.setFilterFunction(attr.stSetFilter);
}

if (attr.stSetSort) {
ctrl.setSortFunction(attr.stSetSort);
}
if (attr.stSetSort) {
ctrl.setSortFunction(attr.stSetSort);
}
};
});
}
};
});

ng.module('smart-table')
.directive('stSearch', ['stConfig', '$timeout','$parse', function (stConfig, $timeout, $parse) {
Expand Down
Loading

0 comments on commit d46e48b

Please sign in to comment.