Created
April 20, 2016 20:26
-
-
Save interactivellama/1fc443552a83f2e91e291831326d3cdc to your computer and use it in GitHub Desktop.
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
/* | |
* Fuel UX Utilities Repeater Static DataSource | |
* https://github.com/ExactTarget/fuelux-utilities | |
* | |
* Copyright (c) 2014 ExactTarget | |
* Licensed under the BSD New license. | |
*/ | |
// -- BEGIN UMD WRAPPER PREFACE -- | |
// For more information on UMD visit: | |
// https://github.com/umdjs/umd/blob/master/jqueryPlugin.js | |
(function (factory) { | |
if (typeof define === 'function' && define.amd) { | |
// if AMD loader is available, register as an anonymous module. | |
define(['jquery', 'underscore', 'bootstrap', 'fuelux'], factory); | |
} else { | |
// OR use browser globals if AMD is not present | |
factory(jQuery); | |
} | |
}(function ($, _) { | |
// -- END UMD WRAPPER PREFACE -- | |
// -- BEGIN MODULE CODE HERE -- | |
if($.fn.repeater) { | |
$.fn.repeater.staticDataSource = function(columns, data){ | |
var dataset = data; | |
var sort = function(data, sortProperty, sortDirection) { | |
var sortedData = _.sortBy(data, function(item) { | |
return item[sortProperty]; | |
}); | |
// sort direction | |
if (sortDirection === 'desc') { | |
sortedData = sortedData.reverse(); | |
} | |
return sortedData; | |
}; | |
var filter = function(data, filter) { | |
var filteredData; | |
if(filter === 'all' || filter.value === 'all') { | |
return data; | |
} else if(filter.property) { | |
// simple filter | |
filteredData = _.filter(data, function(item){ | |
return item[filter.property] === filter.value; | |
}); | |
return filteredData; | |
} else { | |
// advanced filter | |
filteredData = _.filter(data, function(item){ | |
var match = true; | |
for (var key in filter) { | |
if (item[key] !== filter[key]) { | |
match = false; | |
break; | |
} | |
} | |
return match; | |
}); | |
return filteredData; | |
} | |
}; | |
var search = function(data, search) { | |
var searchedData = []; | |
var searchTerm = search.toLowerCase(); | |
_.each(data, function(item) { | |
var values = _.values(item); | |
var found = _.find(values, function(val) { | |
if(val.toString().toLowerCase().indexOf(searchTerm) > -1) { | |
searchedData.push(item); | |
return true; | |
} | |
}); | |
}); | |
return searchedData; | |
}; | |
var delay = function() { | |
var min = 200; // 200 milliseconds | |
var max = 1000; // 1 second | |
// random delay interval | |
return Math.floor(Math.random() * (max - min + 1)) + min; | |
}; | |
this.getData = function(options, callback) { | |
var pageIndex = options.pageIndex; | |
var pageSize = options.pageSize; | |
var view = options.view; | |
var viewColumns = columns; | |
if(columns[view]) { | |
// if columns vary by view | |
viewColumns = columns[view]; | |
} | |
// sort by | |
var rows = sort(dataset, options.sortProperty, options.sortDirection); | |
// filter | |
rows = filter(rows, options.filter); | |
// search | |
if (options.search && options.search.length > 0) { | |
rows = search(rows, options.search); | |
} | |
var totalItems = rows.length; | |
var totalPages = Math.ceil(totalItems / pageSize); | |
var startIndex = (pageIndex * pageSize) + 1; | |
var endIndex = (startIndex + pageSize) - 1; | |
if(endIndex > rows.length) { | |
endIndex = rows.length; | |
} | |
rows = rows.slice(startIndex-1, endIndex); | |
var dataSource = { | |
page: pageIndex, | |
pages: totalPages, | |
count: totalItems, | |
start: startIndex, | |
end: endIndex, | |
columns: viewColumns, | |
items: rows | |
}; | |
// simulate delay | |
window.setTimeout(function () { | |
callback(dataSource); | |
}, delay()); | |
}; | |
}; | |
} | |
// -- BEGIN UMD WRAPPER AFTERWORD -- | |
})); | |
// -- END UMD WRAPPER AFTERWORD -- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment