jquery.nouislider.all.js 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314
  1. /*! noUiSlider - 7.0.10 - 2014-12-27 14:50:46 */
  2. (function(){
  3. 'use strict';
  4. var
  5. /** @const */ FormatOptions = [
  6. 'decimals',
  7. 'thousand',
  8. 'mark',
  9. 'prefix',
  10. 'postfix',
  11. 'encoder',
  12. 'decoder',
  13. 'negativeBefore',
  14. 'negative',
  15. 'edit',
  16. 'undo'
  17. ];
  18. // General
  19. // Reverse a string
  20. function strReverse ( a ) {
  21. return a.split('').reverse().join('');
  22. }
  23. // Check if a string starts with a specified prefix.
  24. function strStartsWith ( input, match ) {
  25. return input.substring(0, match.length) === match;
  26. }
  27. // Check is a string ends in a specified postfix.
  28. function strEndsWith ( input, match ) {
  29. return input.slice(-1 * match.length) === match;
  30. }
  31. // Throw an error if formatting options are incompatible.
  32. function throwEqualError( F, a, b ) {
  33. if ( (F[a] || F[b]) && (F[a] === F[b]) ) {
  34. throw new Error(a);
  35. }
  36. }
  37. // Check if a number is finite and not NaN
  38. function isValidNumber ( input ) {
  39. return typeof input === 'number' && isFinite( input );
  40. }
  41. // Provide rounding-accurate toFixed method.
  42. function toFixed ( value, decimals ) {
  43. var scale = Math.pow(10, decimals);
  44. return ( Math.round(value * scale) / scale).toFixed( decimals );
  45. }
  46. // Formatting
  47. // Accept a number as input, output formatted string.
  48. function formatTo ( decimals, thousand, mark, prefix, postfix, encoder, decoder, negativeBefore, negative, edit, undo, input ) {
  49. var originalInput = input, inputIsNegative, inputPieces, inputBase, inputDecimals = '', output = '';
  50. // Apply user encoder to the input.
  51. // Expected outcome: number.
  52. if ( encoder ) {
  53. input = encoder(input);
  54. }
  55. // Stop if no valid number was provided, the number is infinite or NaN.
  56. if ( !isValidNumber(input) ) {
  57. return false;
  58. }
  59. // Rounding away decimals might cause a value of -0
  60. // when using very small ranges. Remove those cases.
  61. if ( decimals !== false && parseFloat(input.toFixed(decimals)) === 0 ) {
  62. input = 0;
  63. }
  64. // Formatting is done on absolute numbers,
  65. // decorated by an optional negative symbol.
  66. if ( input < 0 ) {
  67. inputIsNegative = true;
  68. input = Math.abs(input);
  69. }
  70. // Reduce the number of decimals to the specified option.
  71. if ( decimals !== false ) {
  72. input = toFixed( input, decimals );
  73. }
  74. // Transform the number into a string, so it can be split.
  75. input = input.toString();
  76. // Break the number on the decimal separator.
  77. if ( input.indexOf('.') !== -1 ) {
  78. inputPieces = input.split('.');
  79. inputBase = inputPieces[0];
  80. if ( mark ) {
  81. inputDecimals = mark + inputPieces[1];
  82. }
  83. } else {
  84. // If it isn't split, the entire number will do.
  85. inputBase = input;
  86. }
  87. // Group numbers in sets of three.
  88. if ( thousand ) {
  89. inputBase = strReverse(inputBase).match(/.{1,3}/g);
  90. inputBase = strReverse(inputBase.join( strReverse( thousand ) ));
  91. }
  92. // If the number is negative, prefix with negation symbol.
  93. if ( inputIsNegative && negativeBefore ) {
  94. output += negativeBefore;
  95. }
  96. // Prefix the number
  97. if ( prefix ) {
  98. output += prefix;
  99. }
  100. // Normal negative option comes after the prefix. Defaults to '-'.
  101. if ( inputIsNegative && negative ) {
  102. output += negative;
  103. }
  104. // Append the actual number.
  105. output += inputBase;
  106. output += inputDecimals;
  107. // Apply the postfix.
  108. if ( postfix ) {
  109. output += postfix;
  110. }
  111. // Run the output through a user-specified post-formatter.
  112. if ( edit ) {
  113. output = edit ( output, originalInput );
  114. }
  115. // All done.
  116. return output;
  117. }
  118. // Accept a sting as input, output decoded number.
  119. function formatFrom ( decimals, thousand, mark, prefix, postfix, encoder, decoder, negativeBefore, negative, edit, undo, input ) {
  120. var originalInput = input, inputIsNegative, output = '';
  121. // User defined pre-decoder. Result must be a non empty string.
  122. if ( undo ) {
  123. input = undo(input);
  124. }
  125. // Test the input. Can't be empty.
  126. if ( !input || typeof input !== 'string' ) {
  127. return false;
  128. }
  129. // If the string starts with the negativeBefore value: remove it.
  130. // Remember is was there, the number is negative.
  131. if ( negativeBefore && strStartsWith(input, negativeBefore) ) {
  132. input = input.replace(negativeBefore, '');
  133. inputIsNegative = true;
  134. }
  135. // Repeat the same procedure for the prefix.
  136. if ( prefix && strStartsWith(input, prefix) ) {
  137. input = input.replace(prefix, '');
  138. }
  139. // And again for negative.
  140. if ( negative && strStartsWith(input, negative) ) {
  141. input = input.replace(negative, '');
  142. inputIsNegative = true;
  143. }
  144. // Remove the postfix.
  145. // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/slice
  146. if ( postfix && strEndsWith(input, postfix) ) {
  147. input = input.slice(0, -1 * postfix.length);
  148. }
  149. // Remove the thousand grouping.
  150. if ( thousand ) {
  151. input = input.split(thousand).join('');
  152. }
  153. // Set the decimal separator back to period.
  154. if ( mark ) {
  155. input = input.replace(mark, '.');
  156. }
  157. // Prepend the negative symbol.
  158. if ( inputIsNegative ) {
  159. output += '-';
  160. }
  161. // Add the number
  162. output += input;
  163. // Trim all non-numeric characters (allow '.' and '-');
  164. output = output.replace(/[^0-9\.\-.]/g, '');
  165. // The value contains no parse-able number.
  166. if ( output === '' ) {
  167. return false;
  168. }
  169. // Covert to number.
  170. output = Number(output);
  171. // Run the user-specified post-decoder.
  172. if ( decoder ) {
  173. output = decoder(output);
  174. }
  175. // Check is the output is valid, otherwise: return false.
  176. if ( !isValidNumber(output) ) {
  177. return false;
  178. }
  179. return output;
  180. }
  181. // Framework
  182. // Validate formatting options
  183. function validate ( inputOptions ) {
  184. var i, optionName, optionValue,
  185. filteredOptions = {};
  186. for ( i = 0; i < FormatOptions.length; i+=1 ) {
  187. optionName = FormatOptions[i];
  188. optionValue = inputOptions[optionName];
  189. if ( optionValue === undefined ) {
  190. // Only default if negativeBefore isn't set.
  191. if ( optionName === 'negative' && !filteredOptions.negativeBefore ) {
  192. filteredOptions[optionName] = '-';
  193. // Don't set a default for mark when 'thousand' is set.
  194. } else if ( optionName === 'mark' && filteredOptions.thousand !== '.' ) {
  195. filteredOptions[optionName] = '.';
  196. } else {
  197. filteredOptions[optionName] = false;
  198. }
  199. // Floating points in JS are stable up to 7 decimals.
  200. } else if ( optionName === 'decimals' ) {
  201. if ( optionValue >= 0 && optionValue < 8 ) {
  202. filteredOptions[optionName] = optionValue;
  203. } else {
  204. throw new Error(optionName);
  205. }
  206. // These options, when provided, must be functions.
  207. } else if ( optionName === 'encoder' || optionName === 'decoder' || optionName === 'edit' || optionName === 'undo' ) {
  208. if ( typeof optionValue === 'function' ) {
  209. filteredOptions[optionName] = optionValue;
  210. } else {
  211. throw new Error(optionName);
  212. }
  213. // Other options are strings.
  214. } else {
  215. if ( typeof optionValue === 'string' ) {
  216. filteredOptions[optionName] = optionValue;
  217. } else {
  218. throw new Error(optionName);
  219. }
  220. }
  221. }
  222. // Some values can't be extracted from a
  223. // string if certain combinations are present.
  224. throwEqualError(filteredOptions, 'mark', 'thousand');
  225. throwEqualError(filteredOptions, 'prefix', 'negative');
  226. throwEqualError(filteredOptions, 'prefix', 'negativeBefore');
  227. return filteredOptions;
  228. }
  229. // Pass all options as function arguments
  230. function passAll ( options, method, input ) {
  231. var i, args = [];
  232. // Add all options in order of FormatOptions
  233. for ( i = 0; i < FormatOptions.length; i+=1 ) {
  234. args.push(options[FormatOptions[i]]);
  235. }
  236. // Append the input, then call the method, presenting all
  237. // options as arguments.
  238. args.push(input);
  239. return method.apply('', args);
  240. }
  241. /** @constructor */
  242. function wNumb ( options ) {
  243. if ( !(this instanceof wNumb) ) {
  244. return new wNumb ( options );
  245. }
  246. if ( typeof options !== "object" ) {
  247. return;
  248. }
  249. options = validate(options);
  250. // Call 'formatTo' with proper arguments.
  251. this.to = function ( input ) {
  252. return passAll(options, formatTo, input);
  253. };
  254. // Call 'formatFrom' with proper arguments.
  255. this.from = function ( input ) {
  256. return passAll(options, formatFrom, input);
  257. };
  258. }
  259. /** @export */
  260. window.wNumb = wNumb;
  261. }());
  262. /*jslint browser: true */
  263. /*jslint white: true */
  264. (function( $ ){
  265. 'use strict';
  266. // Helpers
  267. // Test in an object is an instance of jQuery or Zepto.
  268. function isInstance ( a ) {
  269. return a instanceof $ || ( $.zepto && $.zepto.isZ(a) );
  270. }
  271. // Link types
  272. function fromPrefix ( target, method ) {
  273. // If target is a string, a new hidden input will be created.
  274. if ( typeof target === 'string' && target.indexOf('-inline-') === 0 ) {
  275. // By default, use the 'html' method.
  276. this.method = method || 'html';
  277. // Use jQuery to create the element
  278. this.target = this.el = $( target.replace('-inline-', '') || '<div/>' );
  279. return true;
  280. }
  281. }
  282. function fromString ( target ) {
  283. // If the string doesn't begin with '-', which is reserved, add a new hidden input.
  284. if ( typeof target === 'string' && target.indexOf('-') !== 0 ) {
  285. this.method = 'val';
  286. var element = document.createElement('input');
  287. element.name = target;
  288. element.type = 'hidden';
  289. this.target = this.el = $(element);
  290. return true;
  291. }
  292. }
  293. function fromFunction ( target ) {
  294. // The target can also be a function, which will be called.
  295. if ( typeof target === 'function' ) {
  296. this.target = false;
  297. this.method = target;
  298. return true;
  299. }
  300. }
  301. function fromInstance ( target, method ) {
  302. if ( isInstance( target ) && !method ) {
  303. // If a jQuery/Zepto input element is provided, but no method is set,
  304. // the element can assume it needs to respond to 'change'...
  305. if ( target.is('input, select, textarea') ) {
  306. // Default to .val if this is an input element.
  307. this.method = 'val';
  308. // Fire the API changehandler when the target changes.
  309. this.target = target.on('change.liblink', this.changeHandler);
  310. } else {
  311. this.target = target;
  312. // If no method is set, and we are not auto-binding an input, default to 'html'.
  313. this.method = 'html';
  314. }
  315. return true;
  316. }
  317. }
  318. function fromInstanceMethod ( target, method ) {
  319. // The method must exist on the element.
  320. if ( isInstance( target ) &&
  321. (typeof method === 'function' ||
  322. (typeof method === 'string' && target[method]))
  323. ) {
  324. this.method = method;
  325. this.target = target;
  326. return true;
  327. }
  328. }
  329. var
  330. /** @const */
  331. creationFunctions = [fromPrefix, fromString, fromFunction, fromInstance, fromInstanceMethod];
  332. // Link Instance
  333. /** @constructor */
  334. function Link ( target, method, format ) {
  335. var that = this, valid = false;
  336. // Forward calls within scope.
  337. this.changeHandler = function ( changeEvent ) {
  338. var decodedValue = that.formatInstance.from( $(this).val() );
  339. // If the value is invalid, stop this event, as well as it's propagation.
  340. if ( decodedValue === false || isNaN(decodedValue) ) {
  341. // Reset the value.
  342. $(this).val(that.lastSetValue);
  343. return false;
  344. }
  345. that.changeHandlerMethod.call( '', changeEvent, decodedValue );
  346. };
  347. // See if this Link needs individual targets based on its usage.
  348. // If so, return the element that needs to be copied by the
  349. // implementing interface.
  350. // Default the element to false.
  351. this.el = false;
  352. // Store the formatter, or use the default.
  353. this.formatInstance = format;
  354. // Try all Link types.
  355. /*jslint unparam: true*/
  356. $.each(creationFunctions, function(i, fn){
  357. valid = fn.call(that, target, method);
  358. return !valid;
  359. });
  360. /*jslint unparam: false*/
  361. // Nothing matched, throw error.
  362. if ( !valid ) {
  363. throw new RangeError("(Link) Invalid Link.");
  364. }
  365. }
  366. // Provides external items with the object value.
  367. Link.prototype.set = function ( value ) {
  368. // Ignore the value, so only the passed-on arguments remain.
  369. var args = Array.prototype.slice.call( arguments ),
  370. additionalArgs = args.slice(1);
  371. // Store some values. The actual, numerical value,
  372. // the formatted value and the parameters for use in 'resetValue'.
  373. // Slice additionalArgs to break the relation.
  374. this.lastSetValue = this.formatInstance.to( value );
  375. // Prepend the value to the function arguments.
  376. additionalArgs.unshift(
  377. this.lastSetValue
  378. );
  379. // When target is undefined, the target was a function.
  380. // In that case, provided the object as the calling scope.
  381. // Branch between writing to a function or an object.
  382. ( typeof this.method === 'function' ?
  383. this.method :
  384. this.target[ this.method ] ).apply( this.target, additionalArgs );
  385. };
  386. // Developer API
  387. /** @constructor */
  388. function LinkAPI ( origin ) {
  389. this.items = [];
  390. this.elements = [];
  391. this.origin = origin;
  392. }
  393. LinkAPI.prototype.push = function( item, element ) {
  394. this.items.push(item);
  395. // Prevent 'false' elements
  396. if ( element ) {
  397. this.elements.push(element);
  398. }
  399. };
  400. LinkAPI.prototype.reconfirm = function ( flag ) {
  401. var i;
  402. for ( i = 0; i < this.elements.length; i += 1 ) {
  403. this.origin.LinkConfirm(flag, this.elements[i]);
  404. }
  405. };
  406. LinkAPI.prototype.remove = function ( flag ) {
  407. var i;
  408. for ( i = 0; i < this.items.length; i += 1 ) {
  409. this.items[i].target.off('.liblink');
  410. }
  411. for ( i = 0; i < this.elements.length; i += 1 ) {
  412. this.elements[i].remove();
  413. }
  414. };
  415. LinkAPI.prototype.change = function ( value ) {
  416. if ( this.origin.LinkIsEmitting ) {
  417. return false;
  418. }
  419. this.origin.LinkIsEmitting = true;
  420. var args = Array.prototype.slice.call( arguments, 1 ), i;
  421. args.unshift( value );
  422. // Write values to serialization Links.
  423. // Convert the value to the correct relative representation.
  424. for ( i = 0; i < this.items.length; i += 1 ) {
  425. this.items[i].set.apply(this.items[i], args);
  426. }
  427. this.origin.LinkIsEmitting = false;
  428. };
  429. // jQuery plugin
  430. function binder ( flag, target, method, format ){
  431. if ( flag === 0 ) {
  432. flag = this.LinkDefaultFlag;
  433. }
  434. // Create a list of API's (if it didn't exist yet);
  435. if ( !this.linkAPI ) {
  436. this.linkAPI = {};
  437. }
  438. // Add an API point.
  439. if ( !this.linkAPI[flag] ) {
  440. this.linkAPI[flag] = new LinkAPI(this);
  441. }
  442. var linkInstance = new Link ( target, method, format || this.LinkDefaultFormatter );
  443. // Default the calling scope to the linked object.
  444. if ( !linkInstance.target ) {
  445. linkInstance.target = $(this);
  446. }
  447. // If the Link requires creation of a new element,
  448. // Pass the element and request confirmation to get the changehandler.
  449. // Set the method to be called when a Link changes.
  450. linkInstance.changeHandlerMethod = this.LinkConfirm( flag, linkInstance.el );
  451. // Store the linkInstance in the flagged list.
  452. this.linkAPI[flag].push( linkInstance, linkInstance.el );
  453. // Now that Link have been connected, request an update.
  454. this.LinkUpdate( flag );
  455. }
  456. /** @export */
  457. $.fn.Link = function( flag ){
  458. var that = this;
  459. // Delete all linkAPI
  460. if ( flag === false ) {
  461. return that.each(function(){
  462. // .Link(false) can be called on elements without Links.
  463. // When that happens, the objects can't be looped.
  464. if ( !this.linkAPI ) {
  465. return;
  466. }
  467. $.map(this.linkAPI, function(api){
  468. api.remove();
  469. });
  470. delete this.linkAPI;
  471. });
  472. }
  473. if ( flag === undefined ) {
  474. flag = 0;
  475. } else if ( typeof flag !== 'string') {
  476. throw new Error("Flag must be string.");
  477. }
  478. return {
  479. to: function( a, b, c ){
  480. return that.each(function(){
  481. binder.call(this, flag, a, b, c);
  482. });
  483. }
  484. };
  485. };
  486. }( window.jQuery || window.Zepto ));
  487. /*jslint browser: true */
  488. /*jslint white: true */
  489. (function( $ ){
  490. 'use strict';
  491. // Removes duplicates from an array.
  492. function unique(array) {
  493. return $.grep(array, function(el, index) {
  494. return index === $.inArray(el, array);
  495. });
  496. }
  497. // Round a value to the closest 'to'.
  498. function closest ( value, to ) {
  499. return Math.round(value / to) * to;
  500. }
  501. // Checks whether a value is numerical.
  502. function isNumeric ( a ) {
  503. return typeof a === 'number' && !isNaN( a ) && isFinite( a );
  504. }
  505. // Rounds a number to 7 supported decimals.
  506. function accurateNumber( number ) {
  507. var p = Math.pow(10, 7);
  508. return Number((Math.round(number*p)/p).toFixed(7));
  509. }
  510. // Sets a class and removes it after [duration] ms.
  511. function addClassFor ( element, className, duration ) {
  512. element.addClass(className);
  513. setTimeout(function(){
  514. element.removeClass(className);
  515. }, duration);
  516. }
  517. // Limits a value to 0 - 100
  518. function limit ( a ) {
  519. return Math.max(Math.min(a, 100), 0);
  520. }
  521. // Wraps a variable as an array, if it isn't one yet.
  522. function asArray ( a ) {
  523. return $.isArray(a) ? a : [a];
  524. }
  525. // Counts decimals
  526. function countDecimals ( numStr ) {
  527. var pieces = numStr.split(".");
  528. return pieces.length > 1 ? pieces[1].length : 0;
  529. }
  530. var
  531. // Cache the document selector;
  532. /** @const */
  533. doc = $(document),
  534. // Make a backup of the original jQuery/Zepto .val() method.
  535. /** @const */
  536. $val = $.fn.val,
  537. // Namespace for binding and unbinding slider events;
  538. /** @const */
  539. namespace = '.nui',
  540. // Determine the events to bind. IE11 implements pointerEvents without
  541. // a prefix, which breaks compatibility with the IE10 implementation.
  542. /** @const */
  543. actions = window.navigator.pointerEnabled ? {
  544. start: 'pointerdown',
  545. move: 'pointermove',
  546. end: 'pointerup'
  547. } : window.navigator.msPointerEnabled ? {
  548. start: 'MSPointerDown',
  549. move: 'MSPointerMove',
  550. end: 'MSPointerUp'
  551. } : {
  552. start: 'mousedown touchstart',
  553. move: 'mousemove touchmove',
  554. end: 'mouseup touchend'
  555. },
  556. // Re-usable list of classes;
  557. /** @const */
  558. Classes = [
  559. /* 0 */ 'noUi-target'
  560. /* 1 */ ,'noUi-base'
  561. /* 2 */ ,'noUi-origin'
  562. /* 3 */ ,'noUi-handle'
  563. /* 4 */ ,'noUi-horizontal'
  564. /* 5 */ ,'noUi-vertical'
  565. /* 6 */ ,'noUi-background'
  566. /* 7 */ ,'noUi-connect'
  567. /* 8 */ ,'noUi-ltr'
  568. /* 9 */ ,'noUi-rtl'
  569. /* 10 */ ,'noUi-dragable'
  570. /* 11 */ ,''
  571. /* 12 */ ,'noUi-state-drag'
  572. /* 13 */ ,''
  573. /* 14 */ ,'noUi-state-tap'
  574. /* 15 */ ,'noUi-active'
  575. /* 16 */ ,''
  576. /* 17 */ ,'noUi-stacking'
  577. ];
  578. // Value calculation
  579. // Determine the size of a sub-range in relation to a full range.
  580. function subRangeRatio ( pa, pb ) {
  581. return (100 / (pb - pa));
  582. }
  583. // (percentage) How many percent is this value of this range?
  584. function fromPercentage ( range, value ) {
  585. return (value * 100) / ( range[1] - range[0] );
  586. }
  587. // (percentage) Where is this value on this range?
  588. function toPercentage ( range, value ) {
  589. return fromPercentage( range, range[0] < 0 ?
  590. value + Math.abs(range[0]) :
  591. value - range[0] );
  592. }
  593. // (value) How much is this percentage on this range?
  594. function isPercentage ( range, value ) {
  595. return ((value * ( range[1] - range[0] )) / 100) + range[0];
  596. }
  597. // Range conversion
  598. function getJ ( value, arr ) {
  599. var j = 1;
  600. while ( value >= arr[j] ){
  601. j += 1;
  602. }
  603. return j;
  604. }
  605. // (percentage) Input a value, find where, on a scale of 0-100, it applies.
  606. function toStepping ( xVal, xPct, value ) {
  607. if ( value >= xVal.slice(-1)[0] ){
  608. return 100;
  609. }
  610. var j = getJ( value, xVal ), va, vb, pa, pb;
  611. va = xVal[j-1];
  612. vb = xVal[j];
  613. pa = xPct[j-1];
  614. pb = xPct[j];
  615. return pa + (toPercentage([va, vb], value) / subRangeRatio (pa, pb));
  616. }
  617. // (value) Input a percentage, find where it is on the specified range.
  618. function fromStepping ( xVal, xPct, value ) {
  619. // There is no range group that fits 100
  620. if ( value >= 100 ){
  621. return xVal.slice(-1)[0];
  622. }
  623. var j = getJ( value, xPct ), va, vb, pa, pb;
  624. va = xVal[j-1];
  625. vb = xVal[j];
  626. pa = xPct[j-1];
  627. pb = xPct[j];
  628. return isPercentage([va, vb], (value - pa) * subRangeRatio (pa, pb));
  629. }
  630. // (percentage) Get the step that applies at a certain value.
  631. function getStep ( xPct, xSteps, snap, value ) {
  632. if ( value === 100 ) {
  633. return value;
  634. }
  635. var j = getJ( value, xPct ), a, b;
  636. // If 'snap' is set, steps are used as fixed points on the slider.
  637. if ( snap ) {
  638. a = xPct[j-1];
  639. b = xPct[j];
  640. // Find the closest position, a or b.
  641. if ((value - a) > ((b-a)/2)){
  642. return b;
  643. }
  644. return a;
  645. }
  646. if ( !xSteps[j-1] ){
  647. return value;
  648. }
  649. return xPct[j-1] + closest(
  650. value - xPct[j-1],
  651. xSteps[j-1]
  652. );
  653. }
  654. // Entry parsing
  655. function handleEntryPoint ( index, value, that ) {
  656. var percentage;
  657. // Wrap numerical input in an array.
  658. if ( typeof value === "number" ) {
  659. value = [value];
  660. }
  661. // Reject any invalid input, by testing whether value is an array.
  662. if ( Object.prototype.toString.call( value ) !== '[object Array]' ){
  663. throw new Error("noUiSlider: 'range' contains invalid value.");
  664. }
  665. // Covert min/max syntax to 0 and 100.
  666. if ( index === 'min' ) {
  667. percentage = 0;
  668. } else if ( index === 'max' ) {
  669. percentage = 100;
  670. } else {
  671. percentage = parseFloat( index );
  672. }
  673. // Check for correct input.
  674. if ( !isNumeric( percentage ) || !isNumeric( value[0] ) ) {
  675. throw new Error("noUiSlider: 'range' value isn't numeric.");
  676. }
  677. // Store values.
  678. that.xPct.push( percentage );
  679. that.xVal.push( value[0] );
  680. // NaN will evaluate to false too, but to keep
  681. // logging clear, set step explicitly. Make sure
  682. // not to override the 'step' setting with false.
  683. if ( !percentage ) {
  684. if ( !isNaN( value[1] ) ) {
  685. that.xSteps[0] = value[1];
  686. }
  687. } else {
  688. that.xSteps.push( isNaN(value[1]) ? false : value[1] );
  689. }
  690. }
  691. function handleStepPoint ( i, n, that ) {
  692. // Ignore 'false' stepping.
  693. if ( !n ) {
  694. return true;
  695. }
  696. // Factor to range ratio
  697. that.xSteps[i] = fromPercentage([
  698. that.xVal[i]
  699. ,that.xVal[i+1]
  700. ], n) / subRangeRatio (
  701. that.xPct[i],
  702. that.xPct[i+1] );
  703. }
  704. // Interface
  705. // The interface to Spectrum handles all direction-based
  706. // conversions, so the above values are unaware.
  707. function Spectrum ( entry, snap, direction, singleStep ) {
  708. this.xPct = [];
  709. this.xVal = [];
  710. this.xSteps = [ singleStep || false ];
  711. this.xNumSteps = [ false ];
  712. this.snap = snap;
  713. this.direction = direction;
  714. var index, ordered = [ /* [0, 'min'], [1, '50%'], [2, 'max'] */ ];
  715. // Map the object keys to an array.
  716. for ( index in entry ) {
  717. if ( entry.hasOwnProperty(index) ) {
  718. ordered.push([entry[index], index]);
  719. }
  720. }
  721. // Sort all entries by value (numeric sort).
  722. ordered.sort(function(a, b) { return a[0] - b[0]; });
  723. // Convert all entries to subranges.
  724. for ( index = 0; index < ordered.length; index++ ) {
  725. handleEntryPoint(ordered[index][1], ordered[index][0], this);
  726. }
  727. // Store the actual step values.
  728. // xSteps is sorted in the same order as xPct and xVal.
  729. this.xNumSteps = this.xSteps.slice(0);
  730. // Convert all numeric steps to the percentage of the subrange they represent.
  731. for ( index = 0; index < this.xNumSteps.length; index++ ) {
  732. handleStepPoint(index, this.xNumSteps[index], this);
  733. }
  734. }
  735. Spectrum.prototype.getMargin = function ( value ) {
  736. return this.xPct.length === 2 ? fromPercentage(this.xVal, value) : false;
  737. };
  738. Spectrum.prototype.toStepping = function ( value ) {
  739. value = toStepping( this.xVal, this.xPct, value );
  740. // Invert the value if this is a right-to-left slider.
  741. if ( this.direction ) {
  742. value = 100 - value;
  743. }
  744. return value;
  745. };
  746. Spectrum.prototype.fromStepping = function ( value ) {
  747. // Invert the value if this is a right-to-left slider.
  748. if ( this.direction ) {
  749. value = 100 - value;
  750. }
  751. return accurateNumber(fromStepping( this.xVal, this.xPct, value ));
  752. };
  753. Spectrum.prototype.getStep = function ( value ) {
  754. // Find the proper step for rtl sliders by search in inverse direction.
  755. // Fixes issue #262.
  756. if ( this.direction ) {
  757. value = 100 - value;
  758. }
  759. value = getStep(this.xPct, this.xSteps, this.snap, value );
  760. if ( this.direction ) {
  761. value = 100 - value;
  762. }
  763. return value;
  764. };
  765. Spectrum.prototype.getApplicableStep = function ( value ) {
  766. // If the value is 100%, return the negative step twice.
  767. var j = getJ(value, this.xPct), offset = value === 100 ? 2 : 1;
  768. return [this.xNumSteps[j-2], this.xVal[j-offset], this.xNumSteps[j-offset]];
  769. };
  770. // Outside testing
  771. Spectrum.prototype.convert = function ( value ) {
  772. return this.getStep(this.toStepping(value));
  773. };
  774. /* Every input option is tested and parsed. This'll prevent
  775. endless validation in internal methods. These tests are
  776. structured with an item for every option available. An
  777. option can be marked as required by setting the 'r' flag.
  778. The testing function is provided with three arguments:
  779. - The provided value for the option;
  780. - A reference to the options object;
  781. - The name for the option;
  782. The testing function returns false when an error is detected,
  783. or true when everything is OK. It can also modify the option
  784. object, to make sure all values can be correctly looped elsewhere. */
  785. /** @const */
  786. var defaultFormatter = { 'to': function( value ){
  787. return value.toFixed(2);
  788. }, 'from': Number };
  789. function testStep ( parsed, entry ) {
  790. if ( !isNumeric( entry ) ) {
  791. throw new Error("noUiSlider: 'step' is not numeric.");
  792. }
  793. // The step option can still be used to set stepping
  794. // for linear sliders. Overwritten if set in 'range'.
  795. parsed.singleStep = entry;
  796. }
  797. function testRange ( parsed, entry ) {
  798. // Filter incorrect input.
  799. if ( typeof entry !== 'object' || $.isArray(entry) ) {
  800. throw new Error("noUiSlider: 'range' is not an object.");
  801. }
  802. // Catch missing start or end.
  803. if ( entry.min === undefined || entry.max === undefined ) {
  804. throw new Error("noUiSlider: Missing 'min' or 'max' in 'range'.");
  805. }
  806. parsed.spectrum = new Spectrum(entry, parsed.snap, parsed.dir, parsed.singleStep);
  807. }
  808. function testStart ( parsed, entry ) {
  809. entry = asArray(entry);
  810. // Validate input. Values aren't tested, as the public .val method
  811. // will always provide a valid location.
  812. if ( !$.isArray( entry ) || !entry.length || entry.length > 2 ) {
  813. throw new Error("noUiSlider: 'start' option is incorrect.");
  814. }
  815. // Store the number of handles.
  816. parsed.handles = entry.length;
  817. // When the slider is initialized, the .val method will
  818. // be called with the start options.
  819. parsed.start = entry;
  820. }
  821. function testSnap ( parsed, entry ) {
  822. // Enforce 100% stepping within subranges.
  823. parsed.snap = entry;
  824. if ( typeof entry !== 'boolean' ){
  825. throw new Error("noUiSlider: 'snap' option must be a boolean.");
  826. }
  827. }
  828. function testAnimate ( parsed, entry ) {
  829. // Enforce 100% stepping within subranges.
  830. parsed.animate = entry;
  831. if ( typeof entry !== 'boolean' ){
  832. throw new Error("noUiSlider: 'animate' option must be a boolean.");
  833. }
  834. }
  835. function testConnect ( parsed, entry ) {
  836. if ( entry === 'lower' && parsed.handles === 1 ) {
  837. parsed.connect = 1;
  838. } else if ( entry === 'upper' && parsed.handles === 1 ) {
  839. parsed.connect = 2;
  840. } else if ( entry === true && parsed.handles === 2 ) {
  841. parsed.connect = 3;
  842. } else if ( entry === false ) {
  843. parsed.connect = 0;
  844. } else {
  845. throw new Error("noUiSlider: 'connect' option doesn't match handle count.");
  846. }
  847. }
  848. function testOrientation ( parsed, entry ) {
  849. // Set orientation to an a numerical value for easy
  850. // array selection.
  851. switch ( entry ){
  852. case 'horizontal':
  853. parsed.ort = 0;
  854. break;
  855. case 'vertical':
  856. parsed.ort = 1;
  857. break;
  858. default:
  859. throw new Error("noUiSlider: 'orientation' option is invalid.");
  860. }
  861. }
  862. function testMargin ( parsed, entry ) {
  863. if ( !isNumeric(entry) ){
  864. throw new Error("noUiSlider: 'margin' option must be numeric.");
  865. }
  866. parsed.margin = parsed.spectrum.getMargin(entry);
  867. if ( !parsed.margin ) {
  868. throw new Error("noUiSlider: 'margin' option is only supported on linear sliders.");
  869. }
  870. }
  871. function testLimit ( parsed, entry ) {
  872. if ( !isNumeric(entry) ){
  873. throw new Error("noUiSlider: 'limit' option must be numeric.");
  874. }
  875. parsed.limit = parsed.spectrum.getMargin(entry);
  876. if ( !parsed.limit ) {
  877. throw new Error("noUiSlider: 'limit' option is only supported on linear sliders.");
  878. }
  879. }
  880. function testDirection ( parsed, entry ) {
  881. // Set direction as a numerical value for easy parsing.
  882. // Invert connection for RTL sliders, so that the proper
  883. // handles get the connect/background classes.
  884. switch ( entry ) {
  885. case 'ltr':
  886. parsed.dir = 0;
  887. break;
  888. case 'rtl':
  889. parsed.dir = 1;
  890. parsed.connect = [0,2,1,3][parsed.connect];
  891. break;
  892. default:
  893. throw new Error("noUiSlider: 'direction' option was not recognized.");
  894. }
  895. }
  896. function testBehaviour ( parsed, entry ) {
  897. // Make sure the input is a string.
  898. if ( typeof entry !== 'string' ) {
  899. throw new Error("noUiSlider: 'behaviour' must be a string containing options.");
  900. }
  901. // Check if the string contains any keywords.
  902. // None are required.
  903. var tap = entry.indexOf('tap') >= 0,
  904. drag = entry.indexOf('drag') >= 0,
  905. fixed = entry.indexOf('fixed') >= 0,
  906. snap = entry.indexOf('snap') >= 0;
  907. parsed.events = {
  908. tap: tap || snap,
  909. drag: drag,
  910. fixed: fixed,
  911. snap: snap
  912. };
  913. }
  914. function testFormat ( parsed, entry ) {
  915. parsed.format = entry;
  916. // Any object with a to and from method is supported.
  917. if ( typeof entry.to === 'function' && typeof entry.from === 'function' ) {
  918. return true;
  919. }
  920. throw new Error( "noUiSlider: 'format' requires 'to' and 'from' methods.");
  921. }
  922. // Test all developer settings and parse to assumption-safe values.
  923. function testOptions ( options ) {
  924. var parsed = {
  925. margin: 0,
  926. limit: 0,
  927. animate: true,
  928. format: defaultFormatter
  929. }, tests;
  930. // Tests are executed in the order they are presented here.
  931. tests = {
  932. 'step': { r: false, t: testStep },
  933. 'start': { r: true, t: testStart },
  934. 'connect': { r: true, t: testConnect },
  935. 'direction': { r: true, t: testDirection },
  936. 'snap': { r: false, t: testSnap },
  937. 'animate': { r: false, t: testAnimate },
  938. 'range': { r: true, t: testRange },
  939. 'orientation': { r: false, t: testOrientation },
  940. 'margin': { r: false, t: testMargin },
  941. 'limit': { r: false, t: testLimit },
  942. 'behaviour': { r: true, t: testBehaviour },
  943. 'format': { r: false, t: testFormat }
  944. };
  945. // Set defaults where applicable.
  946. options = $.extend({
  947. 'connect': false,
  948. 'direction': 'ltr',
  949. 'behaviour': 'tap',
  950. 'orientation': 'horizontal'
  951. }, options);
  952. // Run all options through a testing mechanism to ensure correct
  953. // input. It should be noted that options might get modified to
  954. // be handled properly. E.g. wrapping integers in arrays.
  955. $.each( tests, function( name, test ){
  956. // If the option isn't set, but it is required, throw an error.
  957. if ( options[name] === undefined ) {
  958. if ( test.r ) {
  959. throw new Error("noUiSlider: '" + name + "' is required.");
  960. }
  961. return true;
  962. }
  963. test.t( parsed, options[name] );
  964. });
  965. // Pre-define the styles.
  966. parsed.style = parsed.ort ? 'top' : 'left';
  967. return parsed;
  968. }
  969. // Class handling
  970. // Delimit proposed values for handle positions.
  971. function getPositions ( a, b, delimit ) {
  972. // Add movement to current position.
  973. var c = a + b[0], d = a + b[1];
  974. // Only alter the other position on drag,
  975. // not on standard sliding.
  976. if ( delimit ) {
  977. if ( c < 0 ) {
  978. d += Math.abs(c);
  979. }
  980. if ( d > 100 ) {
  981. c -= ( d - 100 );
  982. }
  983. // Limit values to 0 and 100.
  984. return [limit(c), limit(d)];
  985. }
  986. return [c,d];
  987. }
  988. // Event handling
  989. // Provide a clean event with standardized offset values.
  990. function fixEvent ( e ) {
  991. // Prevent scrolling and panning on touch events, while
  992. // attempting to slide. The tap event also depends on this.
  993. e.preventDefault();
  994. // Filter the event to register the type, which can be
  995. // touch, mouse or pointer. Offset changes need to be
  996. // made on an event specific basis.
  997. var touch = e.type.indexOf('touch') === 0
  998. ,mouse = e.type.indexOf('mouse') === 0
  999. ,pointer = e.type.indexOf('pointer') === 0
  1000. ,x,y, event = e;
  1001. // IE10 implemented pointer events with a prefix;
  1002. if ( e.type.indexOf('MSPointer') === 0 ) {
  1003. pointer = true;
  1004. }
  1005. // Get the originalEvent, if the event has been wrapped
  1006. // by jQuery. Zepto doesn't wrap the event.
  1007. if ( e.originalEvent ) {
  1008. e = e.originalEvent;
  1009. }
  1010. if ( touch ) {
  1011. // noUiSlider supports one movement at a time,
  1012. // so we can select the first 'changedTouch'.
  1013. x = e.changedTouches[0].pageX;
  1014. y = e.changedTouches[0].pageY;
  1015. }
  1016. if ( mouse || pointer ) {
  1017. // Polyfill the pageXOffset and pageYOffset
  1018. // variables for IE7 and IE8;
  1019. if( !pointer && window.pageXOffset === undefined ){
  1020. window.pageXOffset = document.documentElement.scrollLeft;
  1021. window.pageYOffset = document.documentElement.scrollTop;
  1022. }
  1023. x = e.clientX + window.pageXOffset;
  1024. y = e.clientY + window.pageYOffset;
  1025. }
  1026. event.points = [x, y];
  1027. event.cursor = mouse;
  1028. return event;
  1029. }
  1030. // DOM additions
  1031. // Append a handle to the base.
  1032. function addHandle ( direction, index ) {
  1033. var handle = $('<div><div/></div>').addClass( Classes[2] ),
  1034. additions = [ '-lower', '-upper' ];
  1035. if ( direction ) {
  1036. additions.reverse();
  1037. }
  1038. handle.children().addClass(
  1039. Classes[3] + " " + Classes[3]+additions[index]
  1040. );
  1041. return handle;
  1042. }
  1043. // Add the proper connection classes.
  1044. function addConnection ( connect, target, handles ) {
  1045. // Apply the required connection classes to the elements
  1046. // that need them. Some classes are made up for several
  1047. // segments listed in the class list, to allow easy
  1048. // renaming and provide a minor compression benefit.
  1049. switch ( connect ) {
  1050. case 1: target.addClass( Classes[7] );
  1051. handles[0].addClass( Classes[6] );
  1052. break;
  1053. case 3: handles[1].addClass( Classes[6] );
  1054. /* falls through */
  1055. case 2: handles[0].addClass( Classes[7] );
  1056. /* falls through */
  1057. case 0: target.addClass(Classes[6]);
  1058. break;
  1059. }
  1060. }
  1061. // Add handles to the slider base.
  1062. function addHandles ( nrHandles, direction, base ) {
  1063. var index, handles = [];
  1064. // Append handles.
  1065. for ( index = 0; index < nrHandles; index += 1 ) {
  1066. // Keep a list of all added handles.
  1067. handles.push( addHandle( direction, index ).appendTo(base) );
  1068. }
  1069. return handles;
  1070. }
  1071. // Initialize a single slider.
  1072. function addSlider ( direction, orientation, target ) {
  1073. // Apply classes and data to the target.
  1074. target.addClass([
  1075. Classes[0],
  1076. Classes[8 + direction],
  1077. Classes[4 + orientation]
  1078. ].join(' '));
  1079. return $('<div/>').appendTo(target).addClass( Classes[1] );
  1080. }
  1081. function closure ( target, options, originalOptions ){
  1082. // Internal variables
  1083. // All variables local to 'closure' are marked $.
  1084. var $Target = $(target),
  1085. $Locations = [-1, -1],
  1086. $Base,
  1087. $Handles,
  1088. $Spectrum = options.spectrum,
  1089. $Values = [],
  1090. // libLink. For rtl sliders, 'lower' and 'upper' should not be inverted
  1091. // for one-handle sliders, so trim 'upper' it that case.
  1092. triggerPos = ['lower', 'upper'].slice(0, options.handles);
  1093. // Invert the libLink connection for rtl sliders.
  1094. if ( options.dir ) {
  1095. triggerPos.reverse();
  1096. }
  1097. // Helpers
  1098. // Shorthand for base dimensions.
  1099. function baseSize ( ) {
  1100. return $Base[['width', 'height'][options.ort]]();
  1101. }
  1102. // External event handling
  1103. function fireEvents ( events ) {
  1104. // Use the external api to get the values.
  1105. // Wrap the values in an array, as .trigger takes
  1106. // only one additional argument.
  1107. var index, values = [ $Target.val() ];
  1108. for ( index = 0; index < events.length; index += 1 ){
  1109. $Target.trigger(events[index], values);
  1110. }
  1111. }
  1112. // Returns the input array, respecting the slider direction configuration.
  1113. function inSliderOrder ( values ) {
  1114. // If only one handle is used, return a single value.
  1115. if ( values.length === 1 ){
  1116. return values[0];
  1117. }
  1118. if ( options.dir ) {
  1119. return values.reverse();
  1120. }
  1121. return values;
  1122. }
  1123. // libLink integration
  1124. // Create a new function which calls .val on input change.
  1125. function createChangeHandler ( trigger ) {
  1126. return function ( ignore, value ){
  1127. // Determine which array position to 'null' based on 'trigger'.
  1128. $Target.val( [ trigger ? null : value, trigger ? value : null ], true );
  1129. };
  1130. }
  1131. // Called by libLink when it wants a set of links updated.
  1132. function linkUpdate ( flag ) {
  1133. var trigger = $.inArray(flag, triggerPos);
  1134. // The API might not have been set yet.
  1135. if ( $Target[0].linkAPI && $Target[0].linkAPI[flag] ) {
  1136. $Target[0].linkAPI[flag].change(
  1137. $Values[trigger],
  1138. $Handles[trigger].children(),
  1139. $Target
  1140. );
  1141. }
  1142. }
  1143. // Called by libLink to append an element to the slider.
  1144. function linkConfirm ( flag, element ) {
  1145. // Find the trigger for the passed flag.
  1146. var trigger = $.inArray(flag, triggerPos);
  1147. // If set, append the element to the handle it belongs to.
  1148. if ( element ) {
  1149. element.appendTo( $Handles[trigger].children() );
  1150. }
  1151. // The public API is reversed for rtl sliders, so the changeHandler
  1152. // should not be aware of the inverted trigger positions.
  1153. // On rtl slider with one handle, 'lower' should be used.
  1154. if ( options.dir && options.handles > 1 ) {
  1155. trigger = trigger === 1 ? 0 : 1;
  1156. }
  1157. return createChangeHandler( trigger );
  1158. }
  1159. // Place elements back on the slider.
  1160. function reAppendLink ( ) {
  1161. var i, flag;
  1162. // The API keeps a list of elements: we can re-append them on rebuild.
  1163. for ( i = 0; i < triggerPos.length; i += 1 ) {
  1164. if ( this.linkAPI && this.linkAPI[(flag = triggerPos[i])] ) {
  1165. this.linkAPI[flag].reconfirm(flag);
  1166. }
  1167. }
  1168. }
  1169. target.LinkUpdate = linkUpdate;
  1170. target.LinkConfirm = linkConfirm;
  1171. target.LinkDefaultFormatter = options.format;
  1172. target.LinkDefaultFlag = 'lower';
  1173. target.reappend = reAppendLink;
  1174. // Handler for attaching events trough a proxy.
  1175. function attach ( events, element, callback, data ) {
  1176. // This function can be used to 'filter' events to the slider.
  1177. // Add the noUiSlider namespace to all events.
  1178. events = events.replace( /\s/g, namespace + ' ' ) + namespace;
  1179. // Bind a closure on the target.
  1180. return element.on( events, function( e ){
  1181. // jQuery and Zepto (1) handle unset attributes differently,
  1182. // but always falsy; #208
  1183. if ( !!$Target.attr('disabled') ) {
  1184. return false;
  1185. }
  1186. // Stop if an active 'tap' transition is taking place.
  1187. if ( $Target.hasClass( Classes[14] ) ) {
  1188. return false;
  1189. }
  1190. e = fixEvent(e);
  1191. e.calcPoint = e.points[ options.ort ];
  1192. // Call the event handler with the event [ and additional data ].
  1193. callback ( e, data );
  1194. });
  1195. }
  1196. // Handle movement on document for handle and range drag.
  1197. function move ( event, data ) {
  1198. var handles = data.handles || $Handles, positions, state = false,
  1199. proposal = ((event.calcPoint - data.start) * 100) / baseSize(),
  1200. h = handles[0][0] !== $Handles[0][0] ? 1 : 0;
  1201. // Calculate relative positions for the handles.
  1202. positions = getPositions( proposal, data.positions, handles.length > 1);
  1203. state = setHandle ( handles[0], positions[h], handles.length === 1 );
  1204. if ( handles.length > 1 ) {
  1205. state = setHandle ( handles[1], positions[h?0:1], false ) || state;
  1206. }
  1207. // Fire the 'slide' event if any handle moved.
  1208. if ( state ) {
  1209. fireEvents(['slide']);
  1210. }
  1211. }
  1212. // Unbind move events on document, call callbacks.
  1213. function end ( event ) {
  1214. // The handle is no longer active, so remove the class.
  1215. $('.' + Classes[15]).removeClass(Classes[15]);
  1216. // Remove cursor styles and text-selection events bound to the body.
  1217. if ( event.cursor ) {
  1218. $('body').css('cursor', '').off( namespace );
  1219. }
  1220. // Unbind the move and end events, which are added on 'start'.
  1221. doc.off( namespace );
  1222. // Remove dragging class.
  1223. $Target.removeClass(Classes[12]);
  1224. // Fire the change and set events.
  1225. fireEvents(['set', 'change']);
  1226. }
  1227. // Bind move events on document.
  1228. function start ( event, data ) {
  1229. // Mark the handle as 'active' so it can be styled.
  1230. if( data.handles.length === 1 ) {
  1231. data.handles[0].children().addClass(Classes[15]);
  1232. }
  1233. // A drag should never propagate up to the 'tap' event.
  1234. event.stopPropagation();
  1235. // Attach the move event.
  1236. attach ( actions.move, doc, move, {
  1237. start: event.calcPoint,
  1238. handles: data.handles,
  1239. positions: [
  1240. $Locations[0],
  1241. $Locations[$Handles.length - 1]
  1242. ]
  1243. });
  1244. // Unbind all movement when the drag ends.
  1245. attach ( actions.end, doc, end, null );
  1246. // Text selection isn't an issue on touch devices,
  1247. // so adding cursor styles can be skipped.
  1248. if ( event.cursor ) {
  1249. // Prevent the 'I' cursor and extend the range-drag cursor.
  1250. $('body').css('cursor', $(event.target).css('cursor'));
  1251. // Mark the target with a dragging state.
  1252. if ( $Handles.length > 1 ) {
  1253. $Target.addClass(Classes[12]);
  1254. }
  1255. // Prevent text selection when dragging the handles.
  1256. $('body').on('selectstart' + namespace, false);
  1257. }
  1258. }
  1259. // Move closest handle to tapped location.
  1260. function tap ( event ) {
  1261. var location = event.calcPoint, total = 0, to;
  1262. // The tap event shouldn't propagate up and cause 'edge' to run.
  1263. event.stopPropagation();
  1264. // Add up the handle offsets.
  1265. $.each( $Handles, function(){
  1266. total += this.offset()[ options.style ];
  1267. });
  1268. // Find the handle closest to the tapped position.
  1269. total = ( location < total/2 || $Handles.length === 1 ) ? 0 : 1;
  1270. location -= $Base.offset()[ options.style ];
  1271. // Calculate the new position.
  1272. to = ( location * 100 ) / baseSize();
  1273. if ( !options.events.snap ) {
  1274. // Flag the slider as it is now in a transitional state.
  1275. // Transition takes 300 ms, so re-enable the slider afterwards.
  1276. addClassFor( $Target, Classes[14], 300 );
  1277. }
  1278. // Find the closest handle and calculate the tapped point.
  1279. // The set handle to the new position.
  1280. setHandle( $Handles[total], to );
  1281. fireEvents(['slide', 'set', 'change']);
  1282. if ( options.events.snap ) {
  1283. start(event, { handles: [$Handles[total]] });
  1284. }
  1285. }
  1286. // Attach events to several slider parts.
  1287. function events ( behaviour ) {
  1288. var i, drag;
  1289. // Attach the standard drag event to the handles.
  1290. if ( !behaviour.fixed ) {
  1291. for ( i = 0; i < $Handles.length; i += 1 ) {
  1292. // These events are only bound to the visual handle
  1293. // element, not the 'real' origin element.
  1294. attach ( actions.start, $Handles[i].children(), start, {
  1295. handles: [ $Handles[i] ]
  1296. });
  1297. }
  1298. }
  1299. // Attach the tap event to the slider base.
  1300. if ( behaviour.tap ) {
  1301. attach ( actions.start, $Base, tap, {
  1302. handles: $Handles
  1303. });
  1304. }
  1305. // Make the range dragable.
  1306. if ( behaviour.drag ){
  1307. drag = $Base.find( '.' + Classes[7] ).addClass( Classes[10] );
  1308. // When the range is fixed, the entire range can
  1309. // be dragged by the handles. The handle in the first
  1310. // origin will propagate the start event upward,
  1311. // but it needs to be bound manually on the other.
  1312. if ( behaviour.fixed ) {
  1313. drag = drag.add($Base.children().not( drag ).children());
  1314. }
  1315. attach ( actions.start, drag, start, {
  1316. handles: $Handles
  1317. });
  1318. }
  1319. }
  1320. // Test suggested values and apply margin, step.
  1321. function setHandle ( handle, to, noLimitOption ) {
  1322. var trigger = handle[0] !== $Handles[0][0] ? 1 : 0,
  1323. lowerMargin = $Locations[0] + options.margin,
  1324. upperMargin = $Locations[1] - options.margin,
  1325. lowerLimit = $Locations[0] + options.limit,
  1326. upperLimit = $Locations[1] - options.limit;
  1327. // For sliders with multiple handles,
  1328. // limit movement to the other handle.
  1329. // Apply the margin option by adding it to the handle positions.
  1330. if ( $Handles.length > 1 ) {
  1331. to = trigger ? Math.max( to, lowerMargin ) : Math.min( to, upperMargin );
  1332. }
  1333. // The limit option has the opposite effect, limiting handles to a
  1334. // maximum distance from another. Limit must be > 0, as otherwise
  1335. // handles would be unmoveable. 'noLimitOption' is set to 'false'
  1336. // for the .val() method, except for pass 4/4.
  1337. if ( noLimitOption !== false && options.limit && $Handles.length > 1 ) {
  1338. to = trigger ? Math.min ( to, lowerLimit ) : Math.max( to, upperLimit );
  1339. }
  1340. // Handle the step option.
  1341. to = $Spectrum.getStep( to );
  1342. // Limit to 0/100 for .val input, trim anything beyond 7 digits, as
  1343. // JavaScript has some issues in its floating point implementation.
  1344. to = limit(parseFloat(to.toFixed(7)));
  1345. // Return false if handle can't move.
  1346. if ( to === $Locations[trigger] ) {
  1347. return false;
  1348. }
  1349. // Set the handle to the new position.
  1350. handle.css( options.style, to + '%' );
  1351. // Force proper handle stacking
  1352. if ( handle.is(':first-child') ) {
  1353. handle.toggleClass(Classes[17], to > 50 );
  1354. }
  1355. // Update locations.
  1356. $Locations[trigger] = to;
  1357. // Convert the value to the slider stepping/range.
  1358. $Values[trigger] = $Spectrum.fromStepping( to );
  1359. linkUpdate(triggerPos[trigger]);
  1360. return true;
  1361. }
  1362. // Loop values from value method and apply them.
  1363. function setValues ( count, values ) {
  1364. var i, trigger, to;
  1365. // With the limit option, we'll need another limiting pass.
  1366. if ( options.limit ) {
  1367. count += 1;
  1368. }
  1369. // If there are multiple handles to be set run the setting
  1370. // mechanism twice for the first handle, to make sure it
  1371. // can be bounced of the second one properly.
  1372. for ( i = 0; i < count; i += 1 ) {
  1373. trigger = i%2;
  1374. // Get the current argument from the array.
  1375. to = values[trigger];
  1376. // Setting with null indicates an 'ignore'.
  1377. // Inputting 'false' is invalid.
  1378. if ( to !== null && to !== false ) {
  1379. // If a formatted number was passed, attemt to decode it.
  1380. if ( typeof to === 'number' ) {
  1381. to = String(to);
  1382. }
  1383. to = options.format.from( to );
  1384. // Request an update for all links if the value was invalid.
  1385. // Do so too if setting the handle fails.
  1386. if ( to === false || isNaN(to) || setHandle( $Handles[trigger], $Spectrum.toStepping( to ), i === (3 - options.dir) ) === false ) {
  1387. linkUpdate(triggerPos[trigger]);
  1388. }
  1389. }
  1390. }
  1391. }
  1392. // Set the slider value.
  1393. function valueSet ( input ) {
  1394. // LibLink: don't accept new values when currently emitting changes.
  1395. if ( $Target[0].LinkIsEmitting ) {
  1396. return this;
  1397. }
  1398. var count, values = asArray( input );
  1399. // The RTL settings is implemented by reversing the front-end,
  1400. // internal mechanisms are the same.
  1401. if ( options.dir && options.handles > 1 ) {
  1402. values.reverse();
  1403. }
  1404. // Animation is optional.
  1405. // Make sure the initial values where set before using animated
  1406. // placement. (no report, unit testing);
  1407. if ( options.animate && $Locations[0] !== -1 ) {
  1408. addClassFor( $Target, Classes[14], 300 );
  1409. }
  1410. // Determine how often to set the handles.
  1411. count = $Handles.length > 1 ? 3 : 1;
  1412. if ( values.length === 1 ) {
  1413. count = 1;
  1414. }
  1415. setValues ( count, values );
  1416. // Fire the 'set' event. As of noUiSlider 7,
  1417. // this is no longer optional.
  1418. fireEvents(['set']);
  1419. return this;
  1420. }
  1421. // Get the slider value.
  1422. function valueGet ( ) {
  1423. var i, retour = [];
  1424. // Get the value from all handles.
  1425. for ( i = 0; i < options.handles; i += 1 ){
  1426. retour[i] = options.format.to( $Values[i] );
  1427. }
  1428. return inSliderOrder( retour );
  1429. }
  1430. // Destroy the slider and unbind all events.
  1431. function destroyTarget ( ) {
  1432. // Unbind events on the slider, remove all classes and child elements.
  1433. $(this).off(namespace)
  1434. .removeClass(Classes.join(' '))
  1435. .empty();
  1436. delete this.LinkUpdate;
  1437. delete this.LinkConfirm;
  1438. delete this.LinkDefaultFormatter;
  1439. delete this.LinkDefaultFlag;
  1440. delete this.reappend;
  1441. delete this.vGet;
  1442. delete this.vSet;
  1443. delete this.getCurrentStep;
  1444. delete this.getInfo;
  1445. delete this.destroy;
  1446. // Return the original options from the closure.
  1447. return originalOptions;
  1448. }
  1449. // Get the current step size for the slider.
  1450. function getCurrentStep ( ) {
  1451. // Check all locations, map them to their stepping point.
  1452. // Get the step point, then find it in the input list.
  1453. var retour = $.map($Locations, function( location, index ){
  1454. var step = $Spectrum.getApplicableStep( location ),
  1455. // As per #391, the comparison for the decrement step can have some rounding issues.
  1456. // Round the value to the precision used in the step.
  1457. stepDecimals = countDecimals(String(step[2])),
  1458. // Get the current numeric value
  1459. value = $Values[index],
  1460. // To move the slider 'one step up', the current step value needs to be added.
  1461. // Use null if we are at the maximum slider value.
  1462. increment = location === 100 ? null : step[2],
  1463. // Going 'one step down' might put the slider in a different sub-range, so we
  1464. // need to switch between the current or the previous step.
  1465. prev = Number((value - step[2]).toFixed(stepDecimals)),
  1466. // If the value fits the step, return the current step value. Otherwise, use the
  1467. // previous step. Return null if the slider is at its minimum value.
  1468. decrement = location === 0 ? null : (prev >= step[1]) ? step[2] : (step[0] || false);
  1469. return [[decrement, increment]];
  1470. });
  1471. // Return values in the proper order.
  1472. return inSliderOrder( retour );
  1473. }
  1474. // Get the original set of options.
  1475. function getOriginalOptions ( ) {
  1476. return originalOptions;
  1477. }
  1478. // Initialize slider
  1479. // Throw an error if the slider was already initialized.
  1480. if ( $Target.hasClass(Classes[0]) ) {
  1481. throw new Error('Slider was already initialized.');
  1482. }
  1483. // Create the base element, initialise HTML and set classes.
  1484. // Add handles and links.
  1485. $Base = addSlider( options.dir, options.ort, $Target );
  1486. $Handles = addHandles( options.handles, options.dir, $Base );
  1487. // Set the connect classes.
  1488. addConnection ( options.connect, $Target, $Handles );
  1489. // Attach user events.
  1490. events( options.events );
  1491. // Methods
  1492. target.vSet = valueSet;
  1493. target.vGet = valueGet;
  1494. target.destroy = destroyTarget;
  1495. target.getCurrentStep = getCurrentStep;
  1496. target.getOriginalOptions = getOriginalOptions;
  1497. target.getInfo = function(){
  1498. return [
  1499. $Spectrum,
  1500. options.style,
  1501. options.ort
  1502. ];
  1503. };
  1504. // Use the public value method to set the start values.
  1505. $Target.val( options.start );
  1506. }
  1507. // Run the standard initializer
  1508. function initialize ( originalOptions ) {
  1509. // Test the options once, not for every slider.
  1510. var options = testOptions( originalOptions, this );
  1511. // Loop all items, and provide a new closed-scope environment.
  1512. return this.each(function(){
  1513. closure(this, options, originalOptions);
  1514. });
  1515. }
  1516. // Destroy the slider, then re-enter initialization.
  1517. function rebuild ( options ) {
  1518. return this.each(function(){
  1519. // The rebuild flag can be used if the slider wasn't initialized yet.
  1520. if ( !this.destroy ) {
  1521. $(this).noUiSlider( options );
  1522. return;
  1523. }
  1524. // Get the current values from the slider,
  1525. // including the initialization options.
  1526. var values = $(this).val(), originalOptions = this.destroy(),
  1527. // Extend the previous options with the newly provided ones.
  1528. newOptions = $.extend( {}, originalOptions, options );
  1529. // Run the standard initializer.
  1530. $(this).noUiSlider( newOptions );
  1531. // Place Link elements back.
  1532. this.reappend();
  1533. // If the start option hasn't changed,
  1534. // reset the previous values.
  1535. if ( originalOptions.start === newOptions.start ) {
  1536. $(this).val(values);
  1537. }
  1538. });
  1539. }
  1540. // Access the internal getting and setting methods based on argument count.
  1541. function value ( ) {
  1542. return this[0][ !arguments.length ? 'vGet' : 'vSet' ].apply(this[0], arguments);
  1543. }
  1544. // Override the .val() method. Test every element. Is it a slider? Go to
  1545. // the slider value handling. No? Use the standard method.
  1546. // Note how $.fn.val expects 'this' to be an instance of $. For convenience,
  1547. // the above 'value' function does too.
  1548. $.fn.val = function ( arg ) {
  1549. // this === instanceof $
  1550. function valMethod( a ){
  1551. return a.hasClass(Classes[0]) ? value : $val;
  1552. }
  1553. // If no value is passed, this is 'get'.
  1554. if ( !arguments.length ) {
  1555. var first = $(this[0]);
  1556. return valMethod(first).call(first);
  1557. }
  1558. var isFunction = $.isFunction(arg);
  1559. // Return the set so it remains chainable. Make sure not to break
  1560. // jQuery's .val(function( index, value ){}) signature.
  1561. return this.each(function( i ){
  1562. var val = arg, $t = $(this);
  1563. if ( isFunction ) {
  1564. val = arg.call(this, i, $t.val());
  1565. }
  1566. valMethod($t).call($t, val);
  1567. });
  1568. };
  1569. // Extend jQuery/Zepto with the noUiSlider method.
  1570. $.fn.noUiSlider = function ( options, rebuildFlag ) {
  1571. switch ( options ) {
  1572. case 'step': return this[0].getCurrentStep();
  1573. case 'options': return this[0].getOriginalOptions();
  1574. }
  1575. return ( rebuildFlag ? rebuild : initialize ).call(this, options);
  1576. };
  1577. function getGroup ( $Spectrum, mode, values, stepped ) {
  1578. // Use the range.
  1579. if ( mode === 'range' || mode === 'steps' ) {
  1580. return $Spectrum.xVal;
  1581. }
  1582. if ( mode === 'count' ) {
  1583. // Divide 0 - 100 in 'count' parts.
  1584. var spread = ( 100 / (values-1) ), v, i = 0;
  1585. values = [];
  1586. // List these parts and have them handled as 'positions'.
  1587. while ((v=i++*spread) <= 100 ) {
  1588. values.push(v);
  1589. }
  1590. mode = 'positions';
  1591. }
  1592. if ( mode === 'positions' ) {
  1593. // Map all percentages to on-range values.
  1594. return $.map(values, function( value ){
  1595. return $Spectrum.fromStepping( stepped ? $Spectrum.getStep( value ) : value );
  1596. });
  1597. }
  1598. if ( mode === 'values' ) {
  1599. // If the value must be stepped, it needs to be converted to a percentage first.
  1600. if ( stepped ) {
  1601. return $.map(values, function( value ){
  1602. // Convert to percentage, apply step, return to value.
  1603. return $Spectrum.fromStepping( $Spectrum.getStep( $Spectrum.toStepping( value ) ) );
  1604. });
  1605. }
  1606. // Otherwise, we can simply use the values.
  1607. return values;
  1608. }
  1609. }
  1610. function generateSpread ( $Spectrum, density, mode, group ) {
  1611. var originalSpectrumDirection = $Spectrum.direction,
  1612. indexes = {},
  1613. firstInRange = $Spectrum.xVal[0],
  1614. lastInRange = $Spectrum.xVal[$Spectrum.xVal.length-1],
  1615. ignoreFirst = false,
  1616. ignoreLast = false,
  1617. prevPct = 0;
  1618. // This function loops the spectrum in an ltr linear fashion,
  1619. // while the toStepping method is direction aware. Trick it into
  1620. // believing it is ltr.
  1621. $Spectrum.direction = 0;
  1622. // Create a copy of the group, sort it and filter away all duplicates.
  1623. group = unique(group.slice().sort(function(a, b){ return a - b; }));
  1624. // Make sure the range starts with the first element.
  1625. if ( group[0] !== firstInRange ) {
  1626. group.unshift(firstInRange);
  1627. ignoreFirst = true;
  1628. }
  1629. // Likewise for the last one.
  1630. if ( group[group.length - 1] !== lastInRange ) {
  1631. group.push(lastInRange);
  1632. ignoreLast = true;
  1633. }
  1634. $.each(group, function ( index ) {
  1635. // Get the current step and the lower + upper positions.
  1636. var step, i, q,
  1637. low = group[index],
  1638. high = group[index+1],
  1639. newPct, pctDifference, pctPos, type,
  1640. steps, realSteps, stepsize;
  1641. // When using 'steps' mode, use the provided steps.
  1642. // Otherwise, we'll step on to the next subrange.
  1643. if ( mode === 'steps' ) {
  1644. step = $Spectrum.xNumSteps[ index ];
  1645. }
  1646. // Default to a 'full' step.
  1647. if ( !step ) {
  1648. step = high-low;
  1649. }
  1650. // Low can be 0, so test for false. If high is undefined,
  1651. // we are at the last subrange. Index 0 is already handled.
  1652. if ( low === false || high === undefined ) {
  1653. return;
  1654. }
  1655. // Find all steps in the subrange.
  1656. for ( i = low; i <= high; i += step ) {
  1657. // Get the percentage value for the current step,
  1658. // calculate the size for the subrange.
  1659. newPct = $Spectrum.toStepping( i );
  1660. pctDifference = newPct - prevPct;
  1661. steps = pctDifference / density;
  1662. realSteps = Math.round(steps);
  1663. // This ratio represents the ammount of percentage-space a point indicates.
  1664. // For a density 1 the points/percentage = 1. For density 2, that percentage needs to be re-devided.
  1665. // Round the percentage offset to an even number, then divide by two
  1666. // to spread the offset on both sides of the range.
  1667. stepsize = pctDifference/realSteps;
  1668. // Divide all points evenly, adding the correct number to this subrange.
  1669. // Run up to <= so that 100% gets a point, event if ignoreLast is set.
  1670. for ( q = 1; q <= realSteps; q += 1 ) {
  1671. // The ratio between the rounded value and the actual size might be ~1% off.
  1672. // Correct the percentage offset by the number of points
  1673. // per subrange. density = 1 will result in 100 points on the
  1674. // full range, 2 for 50, 4 for 25, etc.
  1675. pctPos = prevPct + ( q * stepsize );
  1676. indexes[pctPos.toFixed(5)] = ['x', 0];
  1677. }
  1678. // Determine the point type.
  1679. type = ($.inArray(i, group) > -1) ? 1 : ( mode === 'steps' ? 2 : 0 );
  1680. // Enforce the 'ignoreFirst' option by overwriting the type for 0.
  1681. if ( !index && ignoreFirst ) {
  1682. type = 0;
  1683. }
  1684. if ( !(i === high && ignoreLast)) {
  1685. // Mark the 'type' of this point. 0 = plain, 1 = real value, 2 = step value.
  1686. indexes[newPct.toFixed(5)] = [i, type];
  1687. }
  1688. // Update the percentage count.
  1689. prevPct = newPct;
  1690. }
  1691. });
  1692. // Reset the spectrum.
  1693. $Spectrum.direction = originalSpectrumDirection;
  1694. return indexes;
  1695. }
  1696. function addMarking ( CSSstyle, orientation, direction, spread, filterFunc, formatter ) {
  1697. var style = ['horizontal', 'vertical'][orientation],
  1698. element = $('<div/>');
  1699. element.addClass('noUi-pips noUi-pips-'+style);
  1700. function getSize( type, value ){
  1701. return [ '-normal', '-large', '-sub' ][type];
  1702. }
  1703. function getTags( offset, source, values ) {
  1704. return 'class="' + source + ' ' +
  1705. source + '-' + style + ' ' +
  1706. source + getSize(values[1], values[0]) +
  1707. '" style="' + CSSstyle + ': ' + offset + '%"';
  1708. }
  1709. function addSpread ( offset, values ){
  1710. if ( direction ) {
  1711. offset = 100 - offset;
  1712. }
  1713. // Apply the filter function, if it is set.
  1714. values[1] = (values[1] && filterFunc) ? filterFunc(values[0], values[1]) : values[1];
  1715. // Add a marker for every point
  1716. element.append('<div ' + getTags(offset, 'noUi-marker', values) + '></div>');
  1717. // Values are only appended for points marked '1' or '2'.
  1718. if ( values[1] ) {
  1719. element.append('<div '+getTags(offset, 'noUi-value', values)+'>' + formatter.to(values[0]) + '</div>');
  1720. }
  1721. }
  1722. // Append all points.
  1723. $.each(spread, addSpread);
  1724. return element;
  1725. }
  1726. $.fn.noUiSlider_pips = function ( grid ) {
  1727. var mode = grid.mode,
  1728. density = grid.density || 1,
  1729. filter = grid.filter || false,
  1730. values = grid.values || false,
  1731. format = grid.format || {
  1732. to: Math.round
  1733. },
  1734. stepped = grid.stepped || false;
  1735. return this.each(function(){
  1736. var info = this.getInfo(),
  1737. group = getGroup( info[0], mode, values, stepped ),
  1738. spread = generateSpread( info[0], density, mode, group );
  1739. return $(this).append(addMarking(
  1740. info[1],
  1741. info[2],
  1742. info[0].direction,
  1743. spread,
  1744. filter,
  1745. format
  1746. ));
  1747. });
  1748. };
  1749. }( window.jQuery || window.Zepto ));