Last active
August 24, 2017 06:48
-
-
Save tomoguisuru/e5b38cd212dbef20249f366790f467e1 to your computer and use it in GitHub Desktop.
Power Select Custom Options
This file contains hidden or 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
import Ember from 'ember'; | |
import PSOptionsComponent from 'ember-power-select/components/power-select/options'; | |
const { | |
$, | |
assert, | |
get, | |
} = Ember; | |
const OptionsComponent = PSOptionsComponent.extend({ | |
canLoadMore: false, | |
init() { | |
this._super(...arguments); | |
// BEGIN Move to initializer | |
$.fn.checkInView = function(partial = false) { | |
const elem = this; | |
if (!elem.length) { return false; } | |
const container = elem.parent(); | |
const contHeight = container.height(); | |
const contTop = container.scrollTop(); | |
const contBottom = contTop + contHeight; | |
const elemTop = elem.offset().top - container.offset().top; | |
const elemBottom = elemTop + elem.height(); | |
const isTotal = elemTop >= 0 && elemBottom <= contHeight; | |
const isPart = (elemTop < 0 && elemBottom > 0 || elemTop > 0 && elemTop <= container.height()) && partial; | |
return isTotal || isPart; | |
}; | |
// END | |
assert('<component:infinite-options>: You must provide a closure action name `loadMore`', get(this, 'loadMore') && typeof get(this, 'loadMore') === 'function'); | |
}, | |
didInsertElement() { | |
this._super(...arguments); | |
const checkVisibility = () => { | |
if ($(this.element).find('.ember-power-select-option--load-more').checkInView(true)) { | |
const loadMore = get(this, 'loadMore'); | |
return loadMore(); | |
} | |
}; | |
$(this.element).on('scroll', () => checkVisibility()); | |
}, | |
willDestroyElement() { | |
this._super(...arguments); | |
$(this.element).off('scroll'); | |
} | |
}); | |
export default OptionsComponent; |
This file contains hidden or 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
import Ember from 'ember'; | |
import { task, timeout } from 'ember-concurrency'; | |
const { | |
Controller, | |
get, | |
set, | |
Logger: { log }, | |
} = Ember; | |
const TEST_WAIT = 1000; | |
export default Ember.Controller.extend({ | |
appName: 'Power Select Custom Options', | |
canLoadMore: true, | |
isLoading: false, | |
init() { | |
this._setColors(); | |
}, | |
colors: [], | |
_setColors() { | |
const baseColors = ["AliceBlue","AntiqueWhite","Aqua","Aquamarine","Azure","Beige","Bisque","Black","BlanchedAlmond","Blue","BlueViolet","Brown","BurlyWood","CadetBlue","Chartreuse","Chocolate","Coral","CornflowerBlue","Cornsilk","Crimson","Cyan","DarkBlue","DarkCyan","DarkGoldenRod","DarkGray","DarkGrey","DarkGreen","DarkKhaki","DarkMagenta","DarkOliveGreen","Darkorange","DarkOrchid","DarkRed","DarkSalmon","DarkSeaGreen","DarkSlateBlue","DarkSlateGray","DarkSlateGrey","DarkTurquoise","DarkViolet","DeepPink","DeepSkyBlue","DimGray","DimGrey","DodgerBlue","FireBrick","FloralWhite","ForestGreen","Fuchsia","Gainsboro"]; | |
set(this, 'colors', baseColors); | |
}, | |
task__loadMore: task(function* () { | |
set(this, 'isLoading', true); | |
log('task__loadMore'); | |
timeout(250); | |
setTimeout(() => { | |
set(this, 'canLoadMore', false); | |
set(this, 'isLoading', false); | |
get(this, 'colors').pushObjects(["GhostWhite","Gold","GoldenRod","Gray","Grey","Green","GreenYellow","HoneyDew","HotPink","IndianRed","Indigo","Ivory","Khaki","Lavender","LavenderBlush","LawnGreen","LemonChiffon","LightBlue","LightCoral","LightCyan","LightGoldenRodYellow","LightGray","LightGrey","LightGreen","LightPink","LightSalmon","LightSeaGreen","LightSkyBlue","LightSlateGray","LightSlateGrey","LightSteelBlue","LightYellow","Lime","LimeGreen","Linen","Magenta","Maroon","MediumAquaMarine","MediumBlue","MediumOrchid","MediumPurple","MediumSeaGreen","MediumSlateBlue","MediumSpringGreen","MediumTurquoise","MediumVioletRed","MidnightBlue","MintCream","MistyRose","Moccasin","NavajoWhite","Navy","OldLace","Olive","OliveDrab","Orange","OrangeRed","Orchid","PaleGoldenRod","PaleGreen","PaleTurquoise","PaleVioletRed","PapayaWhip","PeachPuff","Peru","Pink","Plum","PowderBlue","Purple","Red","RosyBrown","RoyalBlue","SaddleBrown","Salmon","SandyBrown","SeaGreen","SeaShell","Sienna","Silver","SkyBlue","SlateBlue","SlateGray","SlateGrey","Snow","SpringGreen","SteelBlue","Tan","Teal","Thistle","Tomato","Turquoise","Violet","Wheat","White","WhiteSmoke","Yellow","YellowGreen"]); | |
}, TEST_WAIT); | |
}).drop(), | |
actions: { | |
reset() { | |
this._setColors(); | |
set(this, 'canLoadMore', true); | |
}, | |
loadMore() { | |
get(this, 'task__loadMore').perform(); | |
}, | |
} | |
}); |
This file contains hidden or 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
{ | |
"version": "0.12.1", | |
"EmberENV": { | |
"FEATURES": {} | |
}, | |
"options": { | |
"use_pods": false, | |
"enable-testing": false | |
}, | |
"dependencies": { | |
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js", | |
"ember": "2.12.0", | |
"ember-template-compiler": "2.12.0", | |
"ember-testing": "2.12.0" | |
}, | |
"addons": { | |
"ember-data": "2.12.1", | |
"ember-power-select": "1.7.0", | |
"ember-concurrency": "0.8.1" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment