UNPKG

landers.angular

Version:

landers.angular

445 lines (400 loc) 14.9 kB
;angular.module('Landers.angular') .factory('List', ['Flat', 'Helpers', function (Flat, Helpers) { var _class = { list:'list', head:'list-head', item:'list-item', checked:'checked', actived:'active', checker:'checker', }; /** * 列表项类(目前定位操作dom为主) * @return {[type]} [description] */ function Listitem($object) { var that = this; var check_object = function(){ if (!that.object) throw '未设置列表项对象'; }; /** * 设置列表项对象 * @param {[type]} $object [description] */ this.setObject = function($object) { if ($object) { that.object = $object; } return that; } /** * 取得列表项对象 * @return {[type]} [description] */ this.getObject = function(){ return that.object; }; /** * 通过事件触发源设置列表项对象 * @param {[type]} $event [description] */ this.setObjectByEvent = function($event) { if (!angular.isElement($event)) { $event = angular.element($event.target); } that.object = $event.closest('.' + _class.item); return that; }; this.getIndex = function() { check_object(); return that.object.index(); }; /** * 取得列表对象 * @return {[type]} [description] */ this.getListObject = function() { check_object(); return that.closest('.' + _class.list); }; /** * 根据xvar(item) * 选中列表项 * @param string checked_class * @return {[type]} [description] */ this.checked = function(checked_class) { check_object(); that.object.addClass(checked_class || _class.checked); return that; }; /** * 根据xvar(item) * 取消选中列表项 * @param string checked_class * @return {[type]} [description] */ this.unchecked = function(checked_class) { check_object(); that.object.removeClass(checked_class || _class.checked); return that; }; /** * 根据xvar(item) * 选中列表项 * @param string actived_class * @return {[type]} [description] */ this.actived = function(actived_class) { check_object(); that.object.addClass(actived_class || _class.actived); return that; }; /** * 根据xvar(item) * 取消选中列表项 * @param string actived_class * @return {[type]} [description] */ this.unactived = function(actived_class) { check_object(); that.object.removeClass(actived_class || _class.actived); return that; }; that.setObject($object); }; /** * 列表类 * @param {[type]} scope [description] * @param {[type]} datakey [description] * @param {[type]} extendMethods [description] * @return {[type]} [description] */ function list(scope, datakey, extendMethods) { var that = this; angular.extend(this, extendMethods); this.scope = scope; this.datakey = datakey; var flat = Flat.make(scope, scope); /** * 默认idKey * @type {String} */ this.idKey = 'id'; /** * 设置idKey * @param {[type]} idkey [description] */ this.setIdKey = function(idkey){ that.idKey = idkey; }; /** * 设置数据 * @param {[type]} data [description] * @return {[type]} [description] */ this.put = function(data){ if (angular.isString(data)) { data = angular.fromJson(data); } flat.set(datakey, data); return that; }; /** * 取得所有数据 * @return {[type]} [description] */ this.all = function(){ return flat.get(datakey); }; /** * 取得数量 * @return {[type]} [description] */ this.amount = this.count = function(){ var data = that.all(); return data ? data.length : 0; }; /** * 根据参数取得或设置数据 * @param {[type]} data [description] * @return {[type]} [description] */ this.data = function(data) { if (data === undefined) { return that.all(); } else { return that.put(data); } }; /** * 清空数据列表 * @return {[type]} [description] */ this.clear = function(){ return that.put([]); }; /** * 在最前面插入数据 * @param {[type]} dat [description] * @return {[type]} [description] */ this.insert = this.preAppend = function(dat){ flat.unshift(datakey, dat); return that; }; /** * 在最后压入数据 * @param {[type]} dat [description] * @return {[type]} [description] */ this.push = this.append = function(dat) { flat.push(datakey, dat); return that; }; /** * 弹掉最后一个 * @return {[type]} [description] */ this.pop = function() { flat.pop(datakey); return that; }; /** * 根据index移除 * @return {[type]} [description] */ this.removeByIndex = function(index){ flat.delete([datakey, index].join('.')); return that; } /** * 列表项操作集 * @type {Object} */ this.item = { /** * [each description] * @param {Function} callback [description] * @return {[type]} [description] */ each: function(callback) { angular.forEach(that.all(), callback); }, /** * 根据mix(id/dat) 获得最终dat * @param {[type]} mix [description] * @return {[type]} [description] */ data: function(mix){ if (!angular.isObject(mix)) { return that.all().find(function(item){ return mix === item[that.idKey]; }); } else { return mix; } }, /** * 根据指定的ids, 从列表提取相应数据集 * @param {[type]} ids [description] * @return {[type]} [description] */ mapById: function(ids) { if (!angular.isArray(ids)) ids = [ids]; return that.all().filter(function(item){ return ids.indexOf(item[that.idKey]) > -1; }); }, /** * 根据mix(id/dat) 获得其index * @param {[type]} mix [description] * @return {[type]} [description] */ mapIndex: function(mix){ var _item = this.data(mix); return that.all().findIndex(function(item){ return item[that.idKey] == _item[that.idKey]; }); }, /** * 根据指定的indexs, 从列表提取相应数据集 * @param {[type]} indexs [description] * @return {[type]} [description] */ mapByIndex: function(indexs) { if (!angular.isArray(indexs)) indexs = [indexs]; return that.all().filter(function(item, index){ return indexs.indexOf(index) > -1; }); }, /** * 根据index取得列表项 * @param {[type]} index [description] * @return {[type]} [description] */ getByIndex: function(index) { return flat.get([datakey, index].join('.')); }, /** * 交换2条数据顺序 * @param {[type]} item1 [description] * @param {[type]} item2 [description] * @return {[type]} [description] */ swap:function(item1, item2){ var data = that.all(); var item1 = angular.extend({}, this.data(item1)); var index1 = this.mapIndex(item1); var item2 = angular.extend({}, this.data(item2)); var index2 = this.mapIndex(item2); data[index1] = item2; data[index2] = item1; return data; }, /** * 更新列表项 * @param {[type]} item [description] * @param {[type]} data [description] * @param {[type]} field [description] * @return {[type]} [description] */ update: function(item, data, field) { item = this.data(item); if (field) { item[field] = data; } else { angular.extend(item, data); } }, /** * 移除列表项 * @param {[type]} item [description] * @return {[type]} [description] */ remove: function(item) { var index = this.mapIndex(item); that.removeByIndex(index); }, /** * make一个listitem实例 * @param {[type]} $event [description] * @return {[type]} [description] */ make:function($event) { var instance = new Listitem(); instance.setObjectByEvent($event); return instance; }, /** * 根据e * @param {[type]} $event [description] * @return {[type]} [description] */ getIndexByEvent: function($event) { return this.make($event).getIndex(); }, /** * 通过触发事源取得列表项 * @param {[type]} $event [description] * @return {[type]} [description] */ getByEvent: function($event) { return this.make($event).getObject(); }, /** * 根据事件触发源更新列表项 * @param {[type]} item [description] * @param {[type]} data [description] * @param {[type]} field [description] * @return {[type]} [description] */ updateByEvent: function($event, data) { var index = this.getIndexByEvent($event); var item = this.getByIndex(index); this.update(item, data); }, /** * 根据事件来源选中相应列表项 * @param {[type]} $event [description] * @return {[type]} [description] */ checkByEvent: function($event) { return this.make($event).checked(); }, /** * 根据事件来源取消选中相应列表项 * @param {[type]} $event [description] * @return {[type]} [description] */ uncheckByEvent: function($event) { return this.make($event).unchecked(); }, /** * 根据事件来源激活相应列表项 * [activeByEvent description] * @param {[type]} $event [description] * @param {[type]} actived_class [description] * @return {[type]} [description] */ activeByEvent: function($event, actived_class) { return this.make($event).actived(actived_class); }, /** * 根据事件来源取消激活相应列表项 * @param {[type]} $event [description] * @param {[type]} actived_class [description] * @return {[type]} [description] */ unactiveByEvent: function($event, actived_class) { return this.make($event).unactived(actived_class); } }; }; return { make:function(scope, datakey){ return new list(scope, datakey); }, _class:_class }; }]);