dat.gui.js 95 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660
  1. /**
  2. * dat-gui JavaScript Controller Library
  3. * http://code.google.com/p/dat-gui
  4. *
  5. * Copyright 2011 Data Arts Team, Google Creative Lab
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. */
  13. /** @namespace */
  14. var dat = dat || {};
  15. /** @namespace */
  16. dat.gui = dat.gui || {};
  17. /** @namespace */
  18. dat.utils = dat.utils || {};
  19. /** @namespace */
  20. dat.controllers = dat.controllers || {};
  21. /** @namespace */
  22. dat.dom = dat.dom || {};
  23. /** @namespace */
  24. dat.color = dat.color || {};
  25. dat.utils.css = (function () {
  26. return {
  27. load: function (url, doc) {
  28. doc = doc || document;
  29. var link = doc.createElement('link');
  30. link.type = 'text/css';
  31. link.rel = 'stylesheet';
  32. link.href = url;
  33. doc.getElementsByTagName('head')[0].appendChild(link);
  34. },
  35. inject: function(css, doc) {
  36. doc = doc || document;
  37. var injected = document.createElement('style');
  38. injected.type = 'text/css';
  39. injected.innerHTML = css;
  40. doc.getElementsByTagName('head')[0].appendChild(injected);
  41. }
  42. }
  43. })();
  44. dat.utils.common = (function () {
  45. var ARR_EACH = Array.prototype.forEach;
  46. var ARR_SLICE = Array.prototype.slice;
  47. /**
  48. * Band-aid methods for things that should be a lot easier in JavaScript.
  49. * Implementation and structure inspired by underscore.js
  50. * http://documentcloud.github.com/underscore/
  51. */
  52. return {
  53. BREAK: {},
  54. extend: function(target) {
  55. this.each(ARR_SLICE.call(arguments, 1), function(obj) {
  56. for (var key in obj)
  57. if (!this.isUndefined(obj[key]))
  58. target[key] = obj[key];
  59. }, this);
  60. return target;
  61. },
  62. defaults: function(target) {
  63. this.each(ARR_SLICE.call(arguments, 1), function(obj) {
  64. for (var key in obj)
  65. if (this.isUndefined(target[key]))
  66. target[key] = obj[key];
  67. }, this);
  68. return target;
  69. },
  70. compose: function() {
  71. var toCall = ARR_SLICE.call(arguments);
  72. return function() {
  73. var args = ARR_SLICE.call(arguments);
  74. for (var i = toCall.length -1; i >= 0; i--) {
  75. args = [toCall[i].apply(this, args)];
  76. }
  77. return args[0];
  78. }
  79. },
  80. each: function(obj, itr, scope) {
  81. if (ARR_EACH && obj.forEach === ARR_EACH) {
  82. obj.forEach(itr, scope);
  83. } else if (obj.length === obj.length + 0) { // Is number but not NaN
  84. for (var key = 0, l = obj.length; key < l; key++)
  85. if (key in obj && itr.call(scope, obj[key], key) === this.BREAK)
  86. return;
  87. } else {
  88. for (var key in obj)
  89. if (itr.call(scope, obj[key], key) === this.BREAK)
  90. return;
  91. }
  92. },
  93. defer: function(fnc) {
  94. setTimeout(fnc, 0);
  95. },
  96. toArray: function(obj) {
  97. if (obj.toArray) return obj.toArray();
  98. return ARR_SLICE.call(obj);
  99. },
  100. isUndefined: function(obj) {
  101. return obj === undefined;
  102. },
  103. isNull: function(obj) {
  104. return obj === null;
  105. },
  106. isNaN: function(obj) {
  107. return obj !== obj;
  108. },
  109. isArray: Array.isArray || function(obj) {
  110. return obj.constructor === Array;
  111. },
  112. isObject: function(obj) {
  113. return obj === Object(obj);
  114. },
  115. isNumber: function(obj) {
  116. return obj === obj+0;
  117. },
  118. isString: function(obj) {
  119. return obj === obj+'';
  120. },
  121. isBoolean: function(obj) {
  122. return obj === false || obj === true;
  123. },
  124. isFunction: function(obj) {
  125. return Object.prototype.toString.call(obj) === '[object Function]';
  126. }
  127. };
  128. })();
  129. dat.controllers.Controller = (function (common) {
  130. /**
  131. * @class An "abstract" class that represents a given property of an object.
  132. *
  133. * @param {Object} object The object to be manipulated
  134. * @param {string} property The name of the property to be manipulated
  135. *
  136. * @member dat.controllers
  137. */
  138. var Controller = function(object, property) {
  139. this.initialValue = object[property];
  140. /**
  141. * Those who extend this class will put their DOM elements in here.
  142. * @type {DOMElement}
  143. */
  144. this.domElement = document.createElement('div');
  145. /**
  146. * The object to manipulate
  147. * @type {Object}
  148. */
  149. this.object = object;
  150. /**
  151. * The name of the property to manipulate
  152. * @type {String}
  153. */
  154. this.property = property;
  155. /**
  156. * The function to be called on change.
  157. * @type {Function}
  158. * @ignore
  159. */
  160. this.__onChange = undefined;
  161. /**
  162. * The function to be called on finishing change.
  163. * @type {Function}
  164. * @ignore
  165. */
  166. this.__onFinishChange = undefined;
  167. };
  168. common.extend(
  169. Controller.prototype,
  170. /** @lends dat.controllers.Controller.prototype */
  171. {
  172. /**
  173. * Specify that a function fire every time someone changes the value with
  174. * this Controller.
  175. *
  176. * @param {Function} fnc This function will be called whenever the value
  177. * is modified via this Controller.
  178. * @returns {dat.controllers.Controller} this
  179. */
  180. onChange: function(fnc) {
  181. this.__onChange = fnc;
  182. return this;
  183. },
  184. /**
  185. * Specify that a function fire every time someone "finishes" changing
  186. * the value wih this Controller. Useful for values that change
  187. * incrementally like numbers or strings.
  188. *
  189. * @param {Function} fnc This function will be called whenever
  190. * someone "finishes" changing the value via this Controller.
  191. * @returns {dat.controllers.Controller} this
  192. */
  193. onFinishChange: function(fnc) {
  194. this.__onFinishChange = fnc;
  195. return this;
  196. },
  197. /**
  198. * Change the value of <code>object[property]</code>
  199. *
  200. * @param {Object} newValue The new value of <code>object[property]</code>
  201. */
  202. setValue: function(newValue) {
  203. this.object[this.property] = newValue;
  204. if (this.__onChange) {
  205. this.__onChange.call(this, newValue);
  206. }
  207. this.updateDisplay();
  208. return this;
  209. },
  210. /**
  211. * Gets the value of <code>object[property]</code>
  212. *
  213. * @returns {Object} The current value of <code>object[property]</code>
  214. */
  215. getValue: function() {
  216. return this.object[this.property];
  217. },
  218. /**
  219. * Refreshes the visual display of a Controller in order to keep sync
  220. * with the object's current value.
  221. * @returns {dat.controllers.Controller} this
  222. */
  223. updateDisplay: function() {
  224. return this;
  225. },
  226. /**
  227. * @returns {Boolean} true if the value has deviated from initialValue
  228. */
  229. isModified: function() {
  230. return this.initialValue !== this.getValue()
  231. }
  232. }
  233. );
  234. return Controller;
  235. })(dat.utils.common);
  236. dat.dom.dom = (function (common) {
  237. var EVENT_MAP = {
  238. 'HTMLEvents': ['change'],
  239. 'MouseEvents': ['click','mousemove','mousedown','mouseup', 'mouseover'],
  240. 'KeyboardEvents': ['keydown']
  241. };
  242. var EVENT_MAP_INV = {};
  243. common.each(EVENT_MAP, function(v, k) {
  244. common.each(v, function(e) {
  245. EVENT_MAP_INV[e] = k;
  246. });
  247. });
  248. var CSS_VALUE_PIXELS = /(\d+(\.\d+)?)px/;
  249. function cssValueToPixels(val) {
  250. if (val === '0' || common.isUndefined(val)) return 0;
  251. var match = val.match(CSS_VALUE_PIXELS);
  252. if (!common.isNull(match)) {
  253. return parseFloat(match[1]);
  254. }
  255. // TODO ...ems? %?
  256. return 0;
  257. }
  258. /**
  259. * @namespace
  260. * @member dat.dom
  261. */
  262. var dom = {
  263. /**
  264. *
  265. * @param elem
  266. * @param selectable
  267. */
  268. makeSelectable: function(elem, selectable) {
  269. if (elem === undefined || elem.style === undefined) return;
  270. elem.onselectstart = selectable ? function() {
  271. return false;
  272. } : function() {
  273. };
  274. elem.style.MozUserSelect = selectable ? 'auto' : 'none';
  275. elem.style.KhtmlUserSelect = selectable ? 'auto' : 'none';
  276. elem.unselectable = selectable ? 'on' : 'off';
  277. },
  278. /**
  279. *
  280. * @param elem
  281. * @param horizontal
  282. * @param vertical
  283. */
  284. makeFullscreen: function(elem, horizontal, vertical) {
  285. if (common.isUndefined(horizontal)) horizontal = true;
  286. if (common.isUndefined(vertical)) vertical = true;
  287. elem.style.position = 'absolute';
  288. if (horizontal) {
  289. elem.style.left = 0;
  290. elem.style.right = 0;
  291. }
  292. if (vertical) {
  293. elem.style.top = 0;
  294. elem.style.bottom = 0;
  295. }
  296. },
  297. /**
  298. *
  299. * @param elem
  300. * @param eventType
  301. * @param params
  302. */
  303. fakeEvent: function(elem, eventType, params, aux) {
  304. params = params || {};
  305. var className = EVENT_MAP_INV[eventType];
  306. if (!className) {
  307. throw new Error('Event type ' + eventType + ' not supported.');
  308. }
  309. var evt = document.createEvent(className);
  310. switch (className) {
  311. case 'MouseEvents':
  312. var clientX = params.x || params.clientX || 0;
  313. var clientY = params.y || params.clientY || 0;
  314. evt.initMouseEvent(eventType, params.bubbles || false,
  315. params.cancelable || true, window, params.clickCount || 1,
  316. 0, //screen X
  317. 0, //screen Y
  318. clientX, //client X
  319. clientY, //client Y
  320. false, false, false, false, 0, null);
  321. break;
  322. case 'KeyboardEvents':
  323. var init = evt.initKeyboardEvent || evt.initKeyEvent; // webkit || moz
  324. common.defaults(params, {
  325. cancelable: true,
  326. ctrlKey: false,
  327. altKey: false,
  328. shiftKey: false,
  329. metaKey: false,
  330. keyCode: undefined,
  331. charCode: undefined
  332. });
  333. init(eventType, params.bubbles || false,
  334. params.cancelable, window,
  335. params.ctrlKey, params.altKey,
  336. params.shiftKey, params.metaKey,
  337. params.keyCode, params.charCode);
  338. break;
  339. default:
  340. evt.initEvent(eventType, params.bubbles || false,
  341. params.cancelable || true);
  342. break;
  343. }
  344. common.defaults(evt, aux);
  345. elem.dispatchEvent(evt);
  346. },
  347. /**
  348. *
  349. * @param elem
  350. * @param event
  351. * @param func
  352. * @param bool
  353. */
  354. bind: function(elem, event, func, bool) {
  355. bool = bool || false;
  356. if (elem.addEventListener)
  357. elem.addEventListener(event, func, bool);
  358. else if (elem.attachEvent)
  359. elem.attachEvent('on' + event, func);
  360. return dom;
  361. },
  362. /**
  363. *
  364. * @param elem
  365. * @param event
  366. * @param func
  367. * @param bool
  368. */
  369. unbind: function(elem, event, func, bool) {
  370. bool = bool || false;
  371. if (elem.removeEventListener)
  372. elem.removeEventListener(event, func, bool);
  373. else if (elem.detachEvent)
  374. elem.detachEvent('on' + event, func);
  375. return dom;
  376. },
  377. /**
  378. *
  379. * @param elem
  380. * @param className
  381. */
  382. addClass: function(elem, className) {
  383. if (elem.className === undefined) {
  384. elem.className = className;
  385. } else if (elem.className !== className) {
  386. var classes = elem.className.split(/ +/);
  387. if (classes.indexOf(className) == -1) {
  388. classes.push(className);
  389. elem.className = classes.join(' ').replace(/^\s+/, '').replace(/\s+$/, '');
  390. }
  391. }
  392. return dom;
  393. },
  394. /**
  395. *
  396. * @param elem
  397. * @param className
  398. */
  399. removeClass: function(elem, className) {
  400. if (className) {
  401. if (elem.className === undefined) {
  402. // elem.className = className;
  403. } else if (elem.className === className) {
  404. elem.removeAttribute('class');
  405. } else {
  406. var classes = elem.className.split(/ +/);
  407. var index = classes.indexOf(className);
  408. if (index != -1) {
  409. classes.splice(index, 1);
  410. elem.className = classes.join(' ');
  411. }
  412. }
  413. } else {
  414. elem.className = undefined;
  415. }
  416. return dom;
  417. },
  418. hasClass: function(elem, className) {
  419. return new RegExp('(?:^|\\s+)' + className + '(?:\\s+|$)').test(elem.className) || false;
  420. },
  421. /**
  422. *
  423. * @param elem
  424. */
  425. getWidth: function(elem) {
  426. var style = getComputedStyle(elem);
  427. return cssValueToPixels(style['border-left-width']) +
  428. cssValueToPixels(style['border-right-width']) +
  429. cssValueToPixels(style['padding-left']) +
  430. cssValueToPixels(style['padding-right']) +
  431. cssValueToPixels(style['width']);
  432. },
  433. /**
  434. *
  435. * @param elem
  436. */
  437. getHeight: function(elem) {
  438. var style = getComputedStyle(elem);
  439. return cssValueToPixels(style['border-top-width']) +
  440. cssValueToPixels(style['border-bottom-width']) +
  441. cssValueToPixels(style['padding-top']) +
  442. cssValueToPixels(style['padding-bottom']) +
  443. cssValueToPixels(style['height']);
  444. },
  445. /**
  446. *
  447. * @param elem
  448. */
  449. getOffset: function(elem) {
  450. var offset = {left: 0, top:0};
  451. if (elem.offsetParent) {
  452. do {
  453. offset.left += elem.offsetLeft;
  454. offset.top += elem.offsetTop;
  455. } while (elem = elem.offsetParent);
  456. }
  457. return offset;
  458. },
  459. // http://stackoverflow.com/posts/2684561/revisions
  460. /**
  461. *
  462. * @param elem
  463. */
  464. isActive: function(elem) {
  465. return elem === document.activeElement && ( elem.type || elem.href );
  466. }
  467. };
  468. return dom;
  469. })(dat.utils.common);
  470. dat.controllers.OptionController = (function (Controller, dom, common) {
  471. /**
  472. * @class Provides a select input to alter the property of an object, using a
  473. * list of accepted values.
  474. *
  475. * @extends dat.controllers.Controller
  476. *
  477. * @param {Object} object The object to be manipulated
  478. * @param {string} property The name of the property to be manipulated
  479. * @param {Object|string[]} options A map of labels to acceptable values, or
  480. * a list of acceptable string values.
  481. *
  482. * @member dat.controllers
  483. */
  484. var OptionController = function(object, property, options) {
  485. OptionController.superclass.call(this, object, property);
  486. var _this = this;
  487. /**
  488. * The drop down menu
  489. * @ignore
  490. */
  491. this.__select = document.createElement('select');
  492. if (common.isArray(options)) {
  493. var map = {};
  494. common.each(options, function(element) {
  495. map[element] = element;
  496. });
  497. options = map;
  498. }
  499. common.each(options, function(value, key) {
  500. var opt = document.createElement('option');
  501. opt.innerHTML = key;
  502. opt.setAttribute('value', value);
  503. _this.__select.appendChild(opt);
  504. });
  505. // Acknowledge original value
  506. this.updateDisplay();
  507. dom.bind(this.__select, 'change', function() {
  508. var desiredValue = this.options[this.selectedIndex].value;
  509. _this.setValue(desiredValue);
  510. });
  511. this.domElement.appendChild(this.__select);
  512. };
  513. OptionController.superclass = Controller;
  514. common.extend(
  515. OptionController.prototype,
  516. Controller.prototype,
  517. {
  518. setValue: function(v) {
  519. var toReturn = OptionController.superclass.prototype.setValue.call(this, v);
  520. if (this.__onFinishChange) {
  521. this.__onFinishChange.call(this, this.getValue());
  522. }
  523. return toReturn;
  524. },
  525. updateDisplay: function() {
  526. this.__select.value = this.getValue();
  527. return OptionController.superclass.prototype.updateDisplay.call(this);
  528. }
  529. }
  530. );
  531. return OptionController;
  532. })(dat.controllers.Controller,
  533. dat.dom.dom,
  534. dat.utils.common);
  535. dat.controllers.NumberController = (function (Controller, common) {
  536. /**
  537. * @class Represents a given property of an object that is a number.
  538. *
  539. * @extends dat.controllers.Controller
  540. *
  541. * @param {Object} object The object to be manipulated
  542. * @param {string} property The name of the property to be manipulated
  543. * @param {Object} [params] Optional parameters
  544. * @param {Number} [params.min] Minimum allowed value
  545. * @param {Number} [params.max] Maximum allowed value
  546. * @param {Number} [params.step] Increment by which to change value
  547. *
  548. * @member dat.controllers
  549. */
  550. var NumberController = function(object, property, params) {
  551. NumberController.superclass.call(this, object, property);
  552. params = params || {};
  553. this.__min = params.min;
  554. this.__max = params.max;
  555. this.__step = params.step;
  556. if (common.isUndefined(this.__step)) {
  557. if (this.initialValue == 0) {
  558. this.__impliedStep = 1; // What are we, psychics?
  559. } else {
  560. // Hey Doug, check this out.
  561. this.__impliedStep = Math.pow(10, Math.floor(Math.log(this.initialValue)/Math.LN10))/10;
  562. }
  563. } else {
  564. this.__impliedStep = this.__step;
  565. }
  566. this.__precision = numDecimals(this.__impliedStep);
  567. };
  568. NumberController.superclass = Controller;
  569. common.extend(
  570. NumberController.prototype,
  571. Controller.prototype,
  572. /** @lends dat.controllers.NumberController.prototype */
  573. {
  574. setValue: function(v) {
  575. if (this.__min !== undefined && v < this.__min) {
  576. v = this.__min;
  577. } else if (this.__max !== undefined && v > this.__max) {
  578. v = this.__max;
  579. }
  580. if (this.__step !== undefined && v % this.__step != 0) {
  581. v = Math.round(v / this.__step) * this.__step;
  582. }
  583. return NumberController.superclass.prototype.setValue.call(this, v);
  584. },
  585. /**
  586. * Specify a minimum value for <code>object[property]</code>.
  587. *
  588. * @param {Number} minValue The minimum value for
  589. * <code>object[property]</code>
  590. * @returns {dat.controllers.NumberController} this
  591. */
  592. min: function(v) {
  593. this.__min = v;
  594. return this;
  595. },
  596. /**
  597. * Specify a maximum value for <code>object[property]</code>.
  598. *
  599. * @param {Number} maxValue The maximum value for
  600. * <code>object[property]</code>
  601. * @returns {dat.controllers.NumberController} this
  602. */
  603. max: function(v) {
  604. this.__max = v;
  605. return this;
  606. },
  607. /**
  608. * Specify a step value that dat.controllers.NumberController
  609. * increments by.
  610. *
  611. * @param {Number} stepValue The step value for
  612. * dat.controllers.NumberController
  613. * @default if minimum and maximum specified increment is 1% of the
  614. * difference otherwise stepValue is 1
  615. * @returns {dat.controllers.NumberController} this
  616. */
  617. step: function(v) {
  618. this.__step = v;
  619. return this;
  620. }
  621. }
  622. );
  623. function numDecimals(x) {
  624. x = x.toString();
  625. if (x.indexOf('.') > -1) {
  626. return x.length - x.indexOf('.') - 1;
  627. } else {
  628. return 0;
  629. }
  630. }
  631. return NumberController;
  632. })(dat.controllers.Controller,
  633. dat.utils.common);
  634. dat.controllers.NumberControllerBox = (function (NumberController, dom, common) {
  635. /**
  636. * @class Represents a given property of an object that is a number and
  637. * provides an input element with which to manipulate it.
  638. *
  639. * @extends dat.controllers.Controller
  640. * @extends dat.controllers.NumberController
  641. *
  642. * @param {Object} object The object to be manipulated
  643. * @param {string} property The name of the property to be manipulated
  644. * @param {Object} [params] Optional parameters
  645. * @param {Number} [params.min] Minimum allowed value
  646. * @param {Number} [params.max] Maximum allowed value
  647. * @param {Number} [params.step] Increment by which to change value
  648. *
  649. * @member dat.controllers
  650. */
  651. var NumberControllerBox = function(object, property, params) {
  652. this.__truncationSuspended = false;
  653. NumberControllerBox.superclass.call(this, object, property, params);
  654. var _this = this;
  655. /**
  656. * {Number} Previous mouse y position
  657. * @ignore
  658. */
  659. var prev_y;
  660. this.__input = document.createElement('input');
  661. this.__input.setAttribute('type', 'text');
  662. // Makes it so manually specified values are not truncated.
  663. dom.bind(this.__input, 'change', onChange);
  664. dom.bind(this.__input, 'blur', onBlur);
  665. dom.bind(this.__input, 'mousedown', onMouseDown);
  666. dom.bind(this.__input, 'keydown', function(e) {
  667. // When pressing entire, you can be as precise as you want.
  668. if (e.keyCode === 13) {
  669. _this.__truncationSuspended = true;
  670. this.blur();
  671. _this.__truncationSuspended = false;
  672. }
  673. });
  674. function onChange() {
  675. var attempted = parseFloat(_this.__input.value);
  676. if (!common.isNaN(attempted)) _this.setValue(attempted);
  677. }
  678. function onBlur() {
  679. onChange();
  680. if (_this.__onFinishChange) {
  681. _this.__onFinishChange.call(_this, _this.getValue());
  682. }
  683. }
  684. function onMouseDown(e) {
  685. dom.bind(window, 'mousemove', onMouseDrag);
  686. dom.bind(window, 'mouseup', onMouseUp);
  687. prev_y = e.clientY;
  688. }
  689. function onMouseDrag(e) {
  690. var diff = prev_y - e.clientY;
  691. _this.setValue(_this.getValue() + diff * _this.__impliedStep);
  692. prev_y = e.clientY;
  693. }
  694. function onMouseUp() {
  695. dom.unbind(window, 'mousemove', onMouseDrag);
  696. dom.unbind(window, 'mouseup', onMouseUp);
  697. }
  698. this.updateDisplay();
  699. this.domElement.appendChild(this.__input);
  700. };
  701. NumberControllerBox.superclass = NumberController;
  702. common.extend(
  703. NumberControllerBox.prototype,
  704. NumberController.prototype,
  705. {
  706. updateDisplay: function() {
  707. this.__input.value = this.__truncationSuspended ? this.getValue() : roundToDecimal(this.getValue(), this.__precision);
  708. return NumberControllerBox.superclass.prototype.updateDisplay.call(this);
  709. }
  710. }
  711. );
  712. function roundToDecimal(value, decimals) {
  713. var tenTo = Math.pow(10, decimals);
  714. return Math.round(value * tenTo) / tenTo;
  715. }
  716. return NumberControllerBox;
  717. })(dat.controllers.NumberController,
  718. dat.dom.dom,
  719. dat.utils.common);
  720. dat.controllers.NumberControllerSlider = (function (NumberController, dom, css, common, styleSheet) {
  721. /**
  722. * @class Represents a given property of an object that is a number, contains
  723. * a minimum and maximum, and provides a slider element with which to
  724. * manipulate it. It should be noted that the slider element is made up of
  725. * <code>&lt;div&gt;</code> tags, <strong>not</strong> the html5
  726. * <code>&lt;slider&gt;</code> element.
  727. *
  728. * @extends dat.controllers.Controller
  729. * @extends dat.controllers.NumberController
  730. *
  731. * @param {Object} object The object to be manipulated
  732. * @param {string} property The name of the property to be manipulated
  733. * @param {Number} minValue Minimum allowed value
  734. * @param {Number} maxValue Maximum allowed value
  735. * @param {Number} stepValue Increment by which to change value
  736. *
  737. * @member dat.controllers
  738. */
  739. var NumberControllerSlider = function(object, property, min, max, step) {
  740. NumberControllerSlider.superclass.call(this, object, property, { min: min, max: max, step: step });
  741. var _this = this;
  742. this.__background = document.createElement('div');
  743. this.__foreground = document.createElement('div');
  744. dom.bind(this.__background, 'mousedown', onMouseDown);
  745. dom.addClass(this.__background, 'slider');
  746. dom.addClass(this.__foreground, 'slider-fg');
  747. function onMouseDown(e) {
  748. dom.bind(window, 'mousemove', onMouseDrag);
  749. dom.bind(window, 'mouseup', onMouseUp);
  750. onMouseDrag(e);
  751. }
  752. function onMouseDrag(e) {
  753. e.preventDefault();
  754. var offset = dom.getOffset(_this.__background);
  755. var width = dom.getWidth(_this.__background);
  756. _this.setValue(
  757. map(e.clientX, offset.left, offset.left + width, _this.__min, _this.__max)
  758. );
  759. return false;
  760. }
  761. function onMouseUp() {
  762. dom.unbind(window, 'mousemove', onMouseDrag);
  763. dom.unbind(window, 'mouseup', onMouseUp);
  764. if (_this.__onFinishChange) {
  765. _this.__onFinishChange.call(_this, _this.getValue());
  766. }
  767. }
  768. this.updateDisplay();
  769. this.__background.appendChild(this.__foreground);
  770. this.domElement.appendChild(this.__background);
  771. };
  772. NumberControllerSlider.superclass = NumberController;
  773. /**
  774. * Injects default stylesheet for slider elements.
  775. */
  776. NumberControllerSlider.useDefaultStyles = function() {
  777. css.inject(styleSheet);
  778. };
  779. common.extend(
  780. NumberControllerSlider.prototype,
  781. NumberController.prototype,
  782. {
  783. updateDisplay: function() {
  784. var pct = (this.getValue() - this.__min)/(this.__max - this.__min);
  785. this.__foreground.style.width = pct*100+'%';
  786. return NumberControllerSlider.superclass.prototype.updateDisplay.call(this);
  787. }
  788. }
  789. );
  790. function map(v, i1, i2, o1, o2) {
  791. return o1 + (o2 - o1) * ((v - i1) / (i2 - i1));
  792. }
  793. return NumberControllerSlider;
  794. })(dat.controllers.NumberController,
  795. dat.dom.dom,
  796. dat.utils.css,
  797. dat.utils.common,
  798. ".slider {\n box-shadow: inset 0 2px 4px rgba(0,0,0,0.15);\n height: 1em;\n border-radius: 1em;\n background-color: #eee;\n padding: 0 0.5em;\n overflow: hidden;\n}\n\n.slider-fg {\n padding: 1px 0 2px 0;\n background-color: #aaa;\n height: 1em;\n margin-left: -0.5em;\n padding-right: 0.5em;\n border-radius: 1em 0 0 1em;\n}\n\n.slider-fg:after {\n display: inline-block;\n border-radius: 1em;\n background-color: #fff;\n border: 1px solid #aaa;\n content: '';\n float: right;\n margin-right: -1em;\n margin-top: -1px;\n height: 0.9em;\n width: 0.9em;\n}");
  799. dat.controllers.FunctionController = (function (Controller, dom, common) {
  800. /**
  801. * @class Provides a GUI interface to fire a specified method, a property of an object.
  802. *
  803. * @extends dat.controllers.Controller
  804. *
  805. * @param {Object} object The object to be manipulated
  806. * @param {string} property The name of the property to be manipulated
  807. *
  808. * @member dat.controllers
  809. */
  810. var FunctionController = function(object, property, text) {
  811. FunctionController.superclass.call(this, object, property);
  812. var _this = this;
  813. this.__button = document.createElement('div');
  814. this.__button.innerHTML = text === undefined ? 'Fire' : text;
  815. dom.bind(this.__button, 'click', function(e) {
  816. e.preventDefault();
  817. _this.fire();
  818. return false;
  819. });
  820. dom.addClass(this.__button, 'button');
  821. this.domElement.appendChild(this.__button);
  822. };
  823. FunctionController.superclass = Controller;
  824. common.extend(
  825. FunctionController.prototype,
  826. Controller.prototype,
  827. {
  828. fire: function() {
  829. if (this.__onChange) {
  830. this.__onChange.call(this);
  831. }
  832. if (this.__onFinishChange) {
  833. this.__onFinishChange.call(this, this.getValue());
  834. }
  835. this.getValue().call(this.object);
  836. }
  837. }
  838. );
  839. return FunctionController;
  840. })(dat.controllers.Controller,
  841. dat.dom.dom,
  842. dat.utils.common);
  843. dat.controllers.BooleanController = (function (Controller, dom, common) {
  844. /**
  845. * @class Provides a checkbox input to alter the boolean property of an object.
  846. * @extends dat.controllers.Controller
  847. *
  848. * @param {Object} object The object to be manipulated
  849. * @param {string} property The name of the property to be manipulated
  850. *
  851. * @member dat.controllers
  852. */
  853. var BooleanController = function(object, property) {
  854. BooleanController.superclass.call(this, object, property);
  855. var _this = this;
  856. this.__prev = this.getValue();
  857. this.__checkbox = document.createElement('input');
  858. this.__checkbox.setAttribute('type', 'checkbox');
  859. dom.bind(this.__checkbox, 'change', onChange, false);
  860. this.domElement.appendChild(this.__checkbox);
  861. // Match original value
  862. this.updateDisplay();
  863. function onChange() {
  864. _this.setValue(!_this.__prev);
  865. }
  866. };
  867. BooleanController.superclass = Controller;
  868. common.extend(
  869. BooleanController.prototype,
  870. Controller.prototype,
  871. {
  872. setValue: function(v) {
  873. var toReturn = BooleanController.superclass.prototype.setValue.call(this, v);
  874. if (this.__onFinishChange) {
  875. this.__onFinishChange.call(this, this.getValue());
  876. }
  877. this.__prev = this.getValue();
  878. return toReturn;
  879. },
  880. updateDisplay: function() {
  881. if (this.getValue() === true) {
  882. this.__checkbox.setAttribute('checked', 'checked');
  883. this.__checkbox.checked = true;
  884. } else {
  885. this.__checkbox.checked = false;
  886. }
  887. return BooleanController.superclass.prototype.updateDisplay.call(this);
  888. }
  889. }
  890. );
  891. return BooleanController;
  892. })(dat.controllers.Controller,
  893. dat.dom.dom,
  894. dat.utils.common);
  895. dat.color.toString = (function (common) {
  896. return function(color) {
  897. if (color.a == 1 || common.isUndefined(color.a)) {
  898. var s = color.hex.toString(16);
  899. while (s.length < 6) {
  900. s = '0' + s;
  901. }
  902. return '#' + s;
  903. } else {
  904. return 'rgba(' + Math.round(color.r) + ',' + Math.round(color.g) + ',' + Math.round(color.b) + ',' + color.a + ')';
  905. }
  906. }
  907. })(dat.utils.common);
  908. dat.color.interpret = (function (toString, common) {
  909. var result, toReturn;
  910. var interpret = function() {
  911. toReturn = false;
  912. var original = arguments.length > 1 ? common.toArray(arguments) : arguments[0];
  913. common.each(INTERPRETATIONS, function(family) {
  914. if (family.litmus(original)) {
  915. common.each(family.conversions, function(conversion, conversionName) {
  916. result = conversion.read(original);
  917. if (toReturn === false && result !== false) {
  918. toReturn = result;
  919. result.conversionName = conversionName;
  920. result.conversion = conversion;
  921. return common.BREAK;
  922. }
  923. });
  924. return common.BREAK;
  925. }
  926. });
  927. return toReturn;
  928. };
  929. var INTERPRETATIONS = [
  930. // Strings
  931. {
  932. litmus: common.isString,
  933. conversions: {
  934. THREE_CHAR_HEX: {
  935. read: function(original) {
  936. var test = original.match(/^#([A-F0-9])([A-F0-9])([A-F0-9])$/i);
  937. if (test === null) return false;
  938. return {
  939. space: 'HEX',
  940. hex: parseInt(
  941. '0x' +
  942. test[1].toString() + test[1].toString() +
  943. test[2].toString() + test[2].toString() +
  944. test[3].toString() + test[3].toString())
  945. };
  946. },
  947. write: toString
  948. },
  949. SIX_CHAR_HEX: {
  950. read: function(original) {
  951. var test = original.match(/^#([A-F0-9]{6})$/i);
  952. if (test === null) return false;
  953. return {
  954. space: 'HEX',
  955. hex: parseInt('0x' + test[1].toString())
  956. };
  957. },
  958. write: toString
  959. },
  960. CSS_RGB: {
  961. read: function(original) {
  962. var test = original.match(/^rgb\(\s*(.+)\s*,\s*(.+)\s*,\s*(.+)\s*\)/);
  963. if (test === null) return false;
  964. return {
  965. space: 'RGB',
  966. r: parseFloat(test[1]),
  967. g: parseFloat(test[2]),
  968. b: parseFloat(test[3])
  969. };
  970. },
  971. write: toString
  972. },
  973. CSS_RGBA: {
  974. read: function(original) {
  975. var test = original.match(/^rgba\(\s*(.+)\s*,\s*(.+)\s*,\s*(.+)\s*\,\s*(.+)\s*\)/);
  976. if (test === null) return false;
  977. return {
  978. space: 'RGB',
  979. r: parseFloat(test[1]),
  980. g: parseFloat(test[2]),
  981. b: parseFloat(test[3]),
  982. a: parseFloat(test[4])
  983. };
  984. },
  985. write: toString
  986. }
  987. }
  988. },
  989. // Numbers
  990. {
  991. litmus: common.isNumber,
  992. conversions: {
  993. HEX: {
  994. read: function(original) {
  995. return {
  996. space: 'HEX',
  997. hex: original,
  998. conversionName: 'HEX'
  999. }
  1000. },
  1001. write: function(color) {
  1002. return color.hex;
  1003. }
  1004. }
  1005. }
  1006. },
  1007. // Arrays
  1008. {
  1009. litmus: common.isArray,
  1010. conversions: {
  1011. RGB_ARRAY: {
  1012. read: function(original) {
  1013. if (original.length != 3) return false;
  1014. return {
  1015. space: 'RGB',
  1016. r: original[0],
  1017. g: original[1],
  1018. b: original[2]
  1019. };
  1020. },
  1021. write: function(color) {
  1022. return [color.r, color.g, color.b];
  1023. }
  1024. },
  1025. RGBA_ARRAY: {
  1026. read: function(original) {
  1027. if (original.length != 4) return false;
  1028. return {
  1029. space: 'RGB',
  1030. r: original[0],
  1031. g: original[1],
  1032. b: original[2],
  1033. a: original[3]
  1034. };
  1035. },
  1036. write: function(color) {
  1037. return [color.r, color.g, color.b, color.a];
  1038. }
  1039. }
  1040. }
  1041. },
  1042. // Objects
  1043. {
  1044. litmus: common.isObject,
  1045. conversions: {
  1046. RGBA_OBJ: {
  1047. read: function(original) {
  1048. if (common.isNumber(original.r) &&
  1049. common.isNumber(original.g) &&
  1050. common.isNumber(original.b) &&
  1051. common.isNumber(original.a)) {
  1052. return {
  1053. space: 'RGB',
  1054. r: original.r,
  1055. g: original.g,
  1056. b: original.b,
  1057. a: original.a
  1058. }
  1059. }
  1060. return false;
  1061. },
  1062. write: function(color) {
  1063. return {
  1064. r: color.r,
  1065. g: color.g,
  1066. b: color.b,
  1067. a: color.a
  1068. }
  1069. }
  1070. },
  1071. RGB_OBJ: {
  1072. read: function(original) {
  1073. if (common.isNumber(original.r) &&
  1074. common.isNumber(original.g) &&
  1075. common.isNumber(original.b)) {
  1076. return {
  1077. space: 'RGB',
  1078. r: original.r,
  1079. g: original.g,
  1080. b: original.b
  1081. }
  1082. }
  1083. return false;
  1084. },
  1085. write: function(color) {
  1086. return {
  1087. r: color.r,
  1088. g: color.g,
  1089. b: color.b
  1090. }
  1091. }
  1092. },
  1093. HSVA_OBJ: {
  1094. read: function(original) {
  1095. if (common.isNumber(original.h) &&
  1096. common.isNumber(original.s) &&
  1097. common.isNumber(original.v) &&
  1098. common.isNumber(original.a)) {
  1099. return {
  1100. space: 'HSV',
  1101. h: original.h,
  1102. s: original.s,
  1103. v: original.v,
  1104. a: original.a
  1105. }
  1106. }
  1107. return false;
  1108. },
  1109. write: function(color) {
  1110. return {
  1111. h: color.h,
  1112. s: color.s,
  1113. v: color.v,
  1114. a: color.a
  1115. }
  1116. }
  1117. },
  1118. HSV_OBJ: {
  1119. read: function(original) {
  1120. if (common.isNumber(original.h) &&
  1121. common.isNumber(original.s) &&
  1122. common.isNumber(original.v)) {
  1123. return {
  1124. space: 'HSV',
  1125. h: original.h,
  1126. s: original.s,
  1127. v: original.v
  1128. }
  1129. }
  1130. return false;
  1131. },
  1132. write: function(color) {
  1133. return {
  1134. h: color.h,
  1135. s: color.s,
  1136. v: color.v
  1137. }
  1138. }
  1139. }
  1140. }
  1141. }
  1142. ];
  1143. return interpret;
  1144. })(dat.color.toString,
  1145. dat.utils.common);
  1146. dat.GUI = dat.gui.GUI = (function (css, saveDialogueContents, styleSheet, controllerFactory, Controller, BooleanController, FunctionController, NumberControllerBox, NumberControllerSlider, OptionController, ColorController, requestAnimationFrame, CenteredDiv, dom, common) {
  1147. css.inject(styleSheet);
  1148. /** Outer-most className for GUI's */
  1149. var CSS_NAMESPACE = 'dg';
  1150. var HIDE_KEY_CODE = 72;
  1151. /** The only value shared between the JS and SCSS. Use caution. */
  1152. var CLOSE_BUTTON_HEIGHT = 20;
  1153. var DEFAULT_DEFAULT_PRESET_NAME = 'Default';
  1154. var SUPPORTS_LOCAL_STORAGE = (function() {
  1155. try {
  1156. return 'localStorage' in window && window['localStorage'] !== null;
  1157. } catch (e) {
  1158. return false;
  1159. }
  1160. })();
  1161. var SAVE_DIALOGUE;
  1162. /** Have we yet to create an autoPlace GUI? */
  1163. var auto_place_virgin = true;
  1164. /** Fixed position div that auto place GUI's go inside */
  1165. var auto_place_container;
  1166. /** Are we hiding the GUI's ? */
  1167. var hide = false;
  1168. /** GUI's which should be hidden */
  1169. var hideable_guis = [];
  1170. /**
  1171. * A lightweight controller library for JavaScript. It allows you to easily
  1172. * manipulate variables and fire functions on the fly.
  1173. * @class
  1174. *
  1175. * @member dat.gui
  1176. *
  1177. * @param {Object} [params]
  1178. * @param {String} [params.name] The name of this GUI.
  1179. * @param {Object} [params.load] JSON object representing the saved state of
  1180. * this GUI.
  1181. * @param {Boolean} [params.auto=true]
  1182. * @param {dat.gui.GUI} [params.parent] The GUI I'm nested in.
  1183. * @param {Boolean} [params.closed] If true, starts closed
  1184. */
  1185. var GUI = function(params) {
  1186. var _this = this;
  1187. /**
  1188. * Outermost DOM Element
  1189. * @type DOMElement
  1190. */
  1191. this.domElement = document.createElement('div');
  1192. this.__ul = document.createElement('ul');
  1193. this.domElement.appendChild(this.__ul);
  1194. dom.addClass(this.domElement, CSS_NAMESPACE);
  1195. /**
  1196. * Nested GUI's by name
  1197. * @ignore
  1198. */
  1199. this.__folders = {};
  1200. this.__controllers = [];
  1201. /**
  1202. * List of objects I'm remembering for save, only used in top level GUI
  1203. * @ignore
  1204. */
  1205. this.__rememberedObjects = [];
  1206. /**
  1207. * Maps the index of remembered objects to a map of controllers, only used
  1208. * in top level GUI.
  1209. *
  1210. * @private
  1211. * @ignore
  1212. *
  1213. * @example
  1214. * [
  1215. * {
  1216. * propertyName: Controller,
  1217. * anotherPropertyName: Controller
  1218. * },
  1219. * {
  1220. * propertyName: Controller
  1221. * }
  1222. * ]
  1223. */
  1224. this.__rememberedObjectIndecesToControllers = [];
  1225. this.__listening = [];
  1226. params = params || {};
  1227. // Default parameters
  1228. params = common.defaults(params, {
  1229. autoPlace: true,
  1230. width: GUI.DEFAULT_WIDTH
  1231. });
  1232. params = common.defaults(params, {
  1233. resizable: params.autoPlace,
  1234. hideable: params.autoPlace
  1235. });
  1236. if (!common.isUndefined(params.load)) {
  1237. // Explicit preset
  1238. if (params.preset) params.load.preset = params.preset;
  1239. } else {
  1240. params.load = { preset: DEFAULT_DEFAULT_PRESET_NAME };
  1241. }
  1242. if (common.isUndefined(params.parent) && params.hideable) {
  1243. hideable_guis.push(this);
  1244. }
  1245. // Only root level GUI's are resizable.
  1246. params.resizable = common.isUndefined(params.parent) && params.resizable;
  1247. if (params.autoPlace && common.isUndefined(params.scrollable)) {
  1248. params.scrollable = true;
  1249. }
  1250. // params.scrollable = common.isUndefined(params.parent) && params.scrollable === true;
  1251. // Not part of params because I don't want people passing this in via
  1252. // constructor. Should be a 'remembered' value.
  1253. var use_local_storage =
  1254. SUPPORTS_LOCAL_STORAGE &&
  1255. localStorage.getItem(getLocalStorageHash(this, 'isLocal')) === 'true';
  1256. Object.defineProperties(this,
  1257. /** @lends dat.gui.GUI.prototype */
  1258. {
  1259. /**
  1260. * The parent <code>GUI</code>
  1261. * @type dat.gui.GUI
  1262. */
  1263. parent: {
  1264. get: function() {
  1265. return params.parent;
  1266. }
  1267. },
  1268. scrollable: {
  1269. get: function() {
  1270. return params.scrollable;
  1271. }
  1272. },
  1273. /**
  1274. * Handles <code>GUI</code>'s element placement for you
  1275. * @type Boolean
  1276. */
  1277. autoPlace: {
  1278. get: function() {
  1279. return params.autoPlace;
  1280. }
  1281. },
  1282. /**
  1283. * The identifier for a set of saved values
  1284. * @type String
  1285. */
  1286. preset: {
  1287. get: function() {
  1288. if (_this.parent) {
  1289. return _this.getRoot().preset;
  1290. } else {
  1291. return params.load.preset;
  1292. }
  1293. },
  1294. set: function(v) {
  1295. if (_this.parent) {
  1296. _this.getRoot().preset = v;
  1297. } else {
  1298. params.load.preset = v;
  1299. }
  1300. setPresetSelectIndex(this);
  1301. _this.revert();
  1302. }
  1303. },
  1304. /**
  1305. * The width of <code>GUI</code> element
  1306. * @type Number
  1307. */
  1308. width: {
  1309. get: function() {
  1310. return params.width;
  1311. },
  1312. set: function(v) {
  1313. params.width = v;
  1314. setWidth(_this, v);
  1315. }
  1316. },
  1317. /**
  1318. * The name of <code>GUI</code>. Used for folders. i.e
  1319. * a folder's name
  1320. * @type String
  1321. */
  1322. name: {
  1323. get: function() {
  1324. return params.name;
  1325. },
  1326. set: function(v) {
  1327. // TODO Check for collisions among sibling folders
  1328. params.name = v;
  1329. if (title_row_name) {
  1330. title_row_name.innerHTML = params.name;
  1331. }
  1332. }
  1333. },
  1334. /**
  1335. * Whether the <code>GUI</code> is collapsed or not
  1336. * @type Boolean
  1337. */
  1338. closed: {
  1339. get: function() {
  1340. return params.closed;
  1341. },
  1342. set: function(v) {
  1343. params.closed = v;
  1344. if (params.closed) {
  1345. dom.addClass(_this.__ul, GUI.CLASS_CLOSED);
  1346. } else {
  1347. dom.removeClass(_this.__ul, GUI.CLASS_CLOSED);
  1348. }
  1349. // For browsers that aren't going to respect the CSS transition,
  1350. // Lets just check our height against the window height right off
  1351. // the bat.
  1352. this.onResize();
  1353. if (_this.__closeButton) {
  1354. _this.__closeButton.innerHTML = v ? GUI.TEXT_OPEN : GUI.TEXT_CLOSED;
  1355. }
  1356. }
  1357. },
  1358. /**
  1359. * Contains all presets
  1360. * @type Object
  1361. */
  1362. load: {
  1363. get: function() {
  1364. return params.load;
  1365. }
  1366. },
  1367. /**
  1368. * Determines whether or not to use <a href="https://developer.mozilla.org/en/DOM/Storage#localStorage">localStorage</a> as the means for
  1369. * <code>remember</code>ing
  1370. * @type Boolean
  1371. */
  1372. useLocalStorage: {
  1373. get: function() {
  1374. return use_local_storage;
  1375. },
  1376. set: function(bool) {
  1377. if (SUPPORTS_LOCAL_STORAGE) {
  1378. use_local_storage = bool;
  1379. if (bool) {
  1380. dom.bind(window, 'unload', saveToLocalStorage);
  1381. } else {
  1382. dom.unbind(window, 'unload', saveToLocalStorage);
  1383. }
  1384. localStorage.setItem(getLocalStorageHash(_this, 'isLocal'), bool);
  1385. }
  1386. }
  1387. }
  1388. });
  1389. // Are we a root level GUI?
  1390. if (common.isUndefined(params.parent)) {
  1391. params.closed = false;
  1392. dom.addClass(this.domElement, GUI.CLASS_MAIN);
  1393. dom.makeSelectable(this.domElement, false);
  1394. // Are we supposed to be loading locally?
  1395. if (SUPPORTS_LOCAL_STORAGE) {
  1396. if (use_local_storage) {
  1397. _this.useLocalStorage = true;
  1398. var saved_gui = localStorage.getItem(getLocalStorageHash(this, 'gui'));
  1399. if (saved_gui) {
  1400. params.load = JSON.parse(saved_gui);
  1401. }
  1402. }
  1403. }
  1404. this.__closeButton = document.createElement('div');
  1405. this.__closeButton.innerHTML = GUI.TEXT_CLOSED;
  1406. dom.addClass(this.__closeButton, GUI.CLASS_CLOSE_BUTTON);
  1407. this.domElement.appendChild(this.__closeButton);
  1408. dom.bind(this.__closeButton, 'click', function() {
  1409. _this.closed = !_this.closed;
  1410. });
  1411. // Oh, you're a nested GUI!
  1412. } else {
  1413. if (params.closed === undefined) {
  1414. params.closed = true;
  1415. }
  1416. var title_row_name = document.createTextNode(params.name);
  1417. dom.addClass(title_row_name, 'controller-name');
  1418. var title_row = addRow(_this, title_row_name);
  1419. var on_click_title = function(e) {
  1420. e.preventDefault();
  1421. _this.closed = !_this.closed;
  1422. return false;
  1423. };
  1424. dom.addClass(this.__ul, GUI.CLASS_CLOSED);
  1425. dom.addClass(title_row, 'title');
  1426. dom.bind(title_row, 'click', on_click_title);
  1427. if (!params.closed) {
  1428. this.closed = false;
  1429. }
  1430. }
  1431. if (params.autoPlace) {
  1432. if (common.isUndefined(params.parent)) {
  1433. if (auto_place_virgin) {
  1434. auto_place_container = document.createElement('div');
  1435. dom.addClass(auto_place_container, CSS_NAMESPACE);
  1436. dom.addClass(auto_place_container, GUI.CLASS_AUTO_PLACE_CONTAINER);
  1437. document.body.appendChild(auto_place_container);
  1438. auto_place_virgin = false;
  1439. }
  1440. // Put it in the dom for you.
  1441. auto_place_container.appendChild(this.domElement);
  1442. // Apply the auto styles
  1443. dom.addClass(this.domElement, GUI.CLASS_AUTO_PLACE);
  1444. }
  1445. // Make it not elastic.
  1446. if (!this.parent) setWidth(_this, params.width);
  1447. }
  1448. dom.bind(window, 'resize', function() { _this.onResize() });
  1449. dom.bind(this.__ul, 'webkitTransitionEnd', function() { _this.onResize(); });
  1450. dom.bind(this.__ul, 'transitionend', function() { _this.onResize() });
  1451. dom.bind(this.__ul, 'oTransitionEnd', function() { _this.onResize() });
  1452. this.onResize();
  1453. if (params.resizable) {
  1454. addResizeHandle(this);
  1455. }
  1456. function saveToLocalStorage() {
  1457. localStorage.setItem(getLocalStorageHash(_this, 'gui'), JSON.stringify(_this.getSaveObject()));
  1458. }
  1459. var root = _this.getRoot();
  1460. function resetWidth() {
  1461. var root = _this.getRoot();
  1462. root.width += 1;
  1463. common.defer(function() {
  1464. root.width -= 1;
  1465. });
  1466. }
  1467. if (!params.parent) {
  1468. resetWidth();
  1469. }
  1470. };
  1471. GUI.toggleHide = function() {
  1472. hide = !hide;
  1473. common.each(hideable_guis, function(gui) {
  1474. gui.domElement.style.zIndex = hide ? -999 : 999;
  1475. gui.domElement.style.opacity = hide ? 0 : 1;
  1476. });
  1477. };
  1478. GUI.CLASS_AUTO_PLACE = 'a';
  1479. GUI.CLASS_AUTO_PLACE_CONTAINER = 'ac';
  1480. GUI.CLASS_MAIN = 'main';
  1481. GUI.CLASS_CONTROLLER_ROW = 'cr';
  1482. GUI.CLASS_TOO_TALL = 'taller-than-window';
  1483. GUI.CLASS_CLOSED = 'closed';
  1484. GUI.CLASS_CLOSE_BUTTON = 'close-button';
  1485. GUI.CLASS_DRAG = 'drag';
  1486. GUI.DEFAULT_WIDTH = 245;
  1487. GUI.TEXT_CLOSED = 'Close Controls';
  1488. GUI.TEXT_OPEN = 'Open Controls';
  1489. dom.bind(window, 'keydown', function(e) {
  1490. if (document.activeElement.type !== 'text' &&
  1491. (e.which === HIDE_KEY_CODE || e.keyCode == HIDE_KEY_CODE)) {
  1492. GUI.toggleHide();
  1493. }
  1494. }, false);
  1495. common.extend(
  1496. GUI.prototype,
  1497. /** @lends dat.gui.GUI */
  1498. {
  1499. /**
  1500. * @param object
  1501. * @param property
  1502. * @returns {dat.controllers.Controller} The new controller that was added.
  1503. * @instance
  1504. */
  1505. add: function(object, property) {
  1506. return add(
  1507. this,
  1508. object,
  1509. property,
  1510. {
  1511. factoryArgs: Array.prototype.slice.call(arguments, 2)
  1512. }
  1513. );
  1514. },
  1515. /**
  1516. * @param object
  1517. * @param property
  1518. * @returns {dat.controllers.ColorController} The new controller that was added.
  1519. * @instance
  1520. */
  1521. addColor: function(object, property) {
  1522. return add(
  1523. this,
  1524. object,
  1525. property,
  1526. {
  1527. color: true
  1528. }
  1529. );
  1530. },
  1531. /**
  1532. * @param controller
  1533. * @instance
  1534. */
  1535. remove: function(controller) {
  1536. // TODO listening?
  1537. this.__ul.removeChild(controller.__li);
  1538. this.__controllers.slice(this.__controllers.indexOf(controller), 1);
  1539. var _this = this;
  1540. common.defer(function() {
  1541. _this.onResize();
  1542. });
  1543. },
  1544. destroy: function() {
  1545. if (this.autoPlace) {
  1546. auto_place_container.removeChild(this.domElement);
  1547. }
  1548. },
  1549. /**
  1550. * @param name
  1551. * @returns {dat.gui.GUI} The new folder.
  1552. * @throws {Error} if this GUI already has a folder by the specified
  1553. * name
  1554. * @instance
  1555. */
  1556. addFolder: function(name) {
  1557. // We have to prevent collisions on names in order to have a key
  1558. // by which to remember saved values
  1559. if (this.__folders[name] !== undefined) {
  1560. throw new Error('You already have a folder in this GUI by the' +
  1561. ' name "' + name + '"');
  1562. }
  1563. var new_gui_params = { name: name, parent: this };
  1564. // We need to pass down the autoPlace trait so that we can
  1565. // attach event listeners to open/close folder actions to
  1566. // ensure that a scrollbar appears if the window is too short.
  1567. new_gui_params.autoPlace = this.autoPlace;
  1568. // Do we have saved appearance data for this folder?
  1569. if (this.load && // Anything loaded?
  1570. this.load.folders && // Was my parent a dead-end?
  1571. this.load.folders[name]) { // Did daddy remember me?
  1572. // Start me closed if I was closed
  1573. new_gui_params.closed = this.load.folders[name].closed;
  1574. // Pass down the loaded data
  1575. new_gui_params.load = this.load.folders[name];
  1576. }
  1577. var gui = new GUI(new_gui_params);
  1578. this.__folders[name] = gui;
  1579. var li = addRow(this, gui.domElement);
  1580. dom.addClass(li, 'folder');
  1581. return gui;
  1582. },
  1583. open: function() {
  1584. this.closed = false;
  1585. },
  1586. close: function() {
  1587. this.closed = true;
  1588. },
  1589. onResize: function() {
  1590. var root = this.getRoot();
  1591. if (root.scrollable) {
  1592. var top = dom.getOffset(root.__ul).top;
  1593. var h = 0;
  1594. common.each(root.__ul.childNodes, function(node) {
  1595. if (! (root.autoPlace && node === root.__save_row))
  1596. h += dom.getHeight(node);
  1597. });
  1598. if (window.innerHeight - top - CLOSE_BUTTON_HEIGHT < h) {
  1599. dom.addClass(root.domElement, GUI.CLASS_TOO_TALL);
  1600. root.__ul.style.height = window.innerHeight - top - CLOSE_BUTTON_HEIGHT + 'px';
  1601. } else {
  1602. dom.removeClass(root.domElement, GUI.CLASS_TOO_TALL);
  1603. root.__ul.style.height = 'auto';
  1604. }
  1605. }
  1606. if (root.__resize_handle) {
  1607. common.defer(function() {
  1608. root.__resize_handle.style.height = root.__ul.offsetHeight + 'px';
  1609. });
  1610. }
  1611. if (root.__closeButton) {
  1612. root.__closeButton.style.width = root.width + 'px';
  1613. }
  1614. },
  1615. /**
  1616. * Mark objects for saving. The order of these objects cannot change as
  1617. * the GUI grows. When remembering new objects, append them to the end
  1618. * of the list.
  1619. *
  1620. * @param {Object...} objects
  1621. * @throws {Error} if not called on a top level GUI.
  1622. * @instance
  1623. */
  1624. remember: function() {
  1625. if (common.isUndefined(SAVE_DIALOGUE)) {
  1626. SAVE_DIALOGUE = new CenteredDiv();
  1627. SAVE_DIALOGUE.domElement.innerHTML = saveDialogueContents;
  1628. }
  1629. if (this.parent) {
  1630. throw new Error("You can only call remember on a top level GUI.");
  1631. }
  1632. var _this = this;
  1633. common.each(Array.prototype.slice.call(arguments), function(object) {
  1634. if (_this.__rememberedObjects.length == 0) {
  1635. addSaveMenu(_this);
  1636. }
  1637. if (_this.__rememberedObjects.indexOf(object) == -1) {
  1638. _this.__rememberedObjects.push(object);
  1639. }
  1640. });
  1641. if (this.autoPlace) {
  1642. // Set save row width
  1643. setWidth(this, this.width);
  1644. }
  1645. },
  1646. /**
  1647. * @returns {dat.gui.GUI} the topmost parent GUI of a nested GUI.
  1648. * @instance
  1649. */
  1650. getRoot: function() {
  1651. var gui = this;
  1652. while (gui.parent) {
  1653. gui = gui.parent;
  1654. }
  1655. return gui;
  1656. },
  1657. /**
  1658. * @returns {Object} a JSON object representing the current state of
  1659. * this GUI as well as its remembered properties.
  1660. * @instance
  1661. */
  1662. getSaveObject: function() {
  1663. var toReturn = this.load;
  1664. toReturn.closed = this.closed;
  1665. // Am I remembering any values?
  1666. if (this.__rememberedObjects.length > 0) {
  1667. toReturn.preset = this.preset;
  1668. if (!toReturn.remembered) {
  1669. toReturn.remembered = {};
  1670. }
  1671. toReturn.remembered[this.preset] = getCurrentPreset(this);
  1672. }
  1673. toReturn.folders = {};
  1674. common.each(this.__folders, function(element, key) {
  1675. toReturn.folders[key] = element.getSaveObject();
  1676. });
  1677. return toReturn;
  1678. },
  1679. save: function() {
  1680. if (!this.load.remembered) {
  1681. this.load.remembered = {};
  1682. }
  1683. this.load.remembered[this.preset] = getCurrentPreset(this);
  1684. markPresetModified(this, false);
  1685. },
  1686. saveAs: function(presetName) {
  1687. if (!this.load.remembered) {
  1688. // Retain default values upon first save
  1689. this.load.remembered = {};
  1690. this.load.remembered[DEFAULT_DEFAULT_PRESET_NAME] = getCurrentPreset(this, true);
  1691. }
  1692. this.load.remembered[presetName] = getCurrentPreset(this);
  1693. this.preset = presetName;
  1694. addPresetOption(this, presetName, true);
  1695. },
  1696. revert: function(gui) {
  1697. common.each(this.__controllers, function(controller) {
  1698. // Make revert work on Default.
  1699. if (!this.getRoot().load.remembered) {
  1700. controller.setValue(controller.initialValue);
  1701. } else {
  1702. recallSavedValue(gui || this.getRoot(), controller);
  1703. }
  1704. }, this);
  1705. common.each(this.__folders, function(folder) {
  1706. folder.revert(folder);
  1707. });
  1708. if (!gui) {
  1709. markPresetModified(this.getRoot(), false);
  1710. }
  1711. },
  1712. listen: function(controller) {
  1713. var init = this.__listening.length == 0;
  1714. this.__listening.push(controller);
  1715. if (init) updateDisplays(this.__listening);
  1716. }
  1717. }
  1718. );
  1719. function add(gui, object, property, params) {
  1720. if (object[property] === undefined) {
  1721. throw new Error("Object " + object + " has no property \"" + property + "\"");
  1722. }
  1723. var controller;
  1724. if (params.color) {
  1725. controller = new ColorController(object, property);
  1726. } else {
  1727. var factoryArgs = [object,property].concat(params.factoryArgs);
  1728. controller = controllerFactory.apply(gui, factoryArgs);
  1729. }
  1730. if (params.before instanceof Controller) {
  1731. params.before = params.before.__li;
  1732. }
  1733. recallSavedValue(gui, controller);
  1734. dom.addClass(controller.domElement, 'c');
  1735. var name = document.createElement('span');
  1736. dom.addClass(name, 'property-name');
  1737. name.innerHTML = controller.property;
  1738. var container = document.createElement('div');
  1739. container.appendChild(name);
  1740. container.appendChild(controller.domElement);
  1741. var li = addRow(gui, container, params.before);
  1742. dom.addClass(li, GUI.CLASS_CONTROLLER_ROW);
  1743. dom.addClass(li, typeof controller.getValue());
  1744. augmentController(gui, li, controller);
  1745. gui.__controllers.push(controller);
  1746. return controller;
  1747. }
  1748. /**
  1749. * Add a row to the end of the GUI or before another row.
  1750. *
  1751. * @param gui
  1752. * @param [dom] If specified, inserts the dom content in the new row
  1753. * @param [liBefore] If specified, places the new row before another row
  1754. */
  1755. function addRow(gui, dom, liBefore) {
  1756. var li = document.createElement('li');
  1757. if (dom) li.appendChild(dom);
  1758. if (liBefore) {
  1759. gui.__ul.insertBefore(li, params.before);
  1760. } else {
  1761. gui.__ul.appendChild(li);
  1762. }
  1763. gui.onResize();
  1764. return li;
  1765. }
  1766. function augmentController(gui, li, controller) {
  1767. controller.__li = li;
  1768. controller.__gui = gui;
  1769. common.extend(controller, {
  1770. options: function(options) {
  1771. if (arguments.length > 1) {
  1772. controller.remove();
  1773. return add(
  1774. gui,
  1775. controller.object,
  1776. controller.property,
  1777. {
  1778. before: controller.__li.nextElementSibling,
  1779. factoryArgs: [common.toArray(arguments)]
  1780. }
  1781. );
  1782. }
  1783. if (common.isArray(options) || common.isObject(options)) {
  1784. controller.remove();
  1785. return add(
  1786. gui,
  1787. controller.object,
  1788. controller.property,
  1789. {
  1790. before: controller.__li.nextElementSibling,
  1791. factoryArgs: [options]
  1792. }
  1793. );
  1794. }
  1795. },
  1796. name: function(v) {
  1797. controller.__li.firstElementChild.firstElementChild.innerHTML = v;
  1798. return controller;
  1799. },
  1800. listen: function() {
  1801. controller.__gui.listen(controller);
  1802. return controller;
  1803. },
  1804. remove: function() {
  1805. controller.__gui.remove(controller);
  1806. return controller;
  1807. }
  1808. });
  1809. // All sliders should be accompanied by a box.
  1810. if (controller instanceof NumberControllerSlider) {
  1811. var box = new NumberControllerBox(controller.object, controller.property,
  1812. { min: controller.__min, max: controller.__max, step: controller.__step });
  1813. common.each(['updateDisplay', 'onChange', 'onFinishChange'], function(method) {
  1814. var pc = controller[method];
  1815. var pb = box[method];
  1816. controller[method] = box[method] = function() {
  1817. var args = Array.prototype.slice.call(arguments);
  1818. pc.apply(controller, args);
  1819. return pb.apply(box, args);
  1820. }
  1821. });
  1822. dom.addClass(li, 'has-slider');
  1823. controller.domElement.insertBefore(box.domElement, controller.domElement.firstElementChild);
  1824. }
  1825. else if (controller instanceof NumberControllerBox) {
  1826. var r = function(returned) {
  1827. // Have we defined both boundaries?
  1828. if (common.isNumber(controller.__min) && common.isNumber(controller.__max)) {
  1829. // Well, then lets just replace this with a slider.
  1830. controller.remove();
  1831. return add(
  1832. gui,
  1833. controller.object,
  1834. controller.property,
  1835. {
  1836. before: controller.__li.nextElementSibling,
  1837. factoryArgs: [controller.__min, controller.__max, controller.__step]
  1838. });
  1839. }
  1840. return returned;
  1841. };
  1842. controller.min = common.compose(r, controller.min);
  1843. controller.max = common.compose(r, controller.max);
  1844. }
  1845. else if (controller instanceof BooleanController) {
  1846. dom.bind(li, 'click', function() {
  1847. dom.fakeEvent(controller.__checkbox, 'click');
  1848. });
  1849. dom.bind(controller.__checkbox, 'click', function(e) {
  1850. e.stopPropagation(); // Prevents double-toggle
  1851. })
  1852. }
  1853. else if (controller instanceof FunctionController) {
  1854. dom.bind(li, 'click', function() {
  1855. dom.fakeEvent(controller.__button, 'click');
  1856. });
  1857. dom.bind(li, 'mouseover', function() {
  1858. dom.addClass(controller.__button, 'hover');
  1859. });
  1860. dom.bind(li, 'mouseout', function() {
  1861. dom.removeClass(controller.__button, 'hover');
  1862. });
  1863. }
  1864. else if (controller instanceof ColorController) {
  1865. dom.addClass(li, 'color');
  1866. controller.updateDisplay = common.compose(function(r) {
  1867. li.style.borderLeftColor = controller.__color.toString();
  1868. return r;
  1869. }, controller.updateDisplay);
  1870. controller.updateDisplay();
  1871. }
  1872. controller.setValue = common.compose(function(r) {
  1873. if (gui.getRoot().__preset_select && controller.isModified()) {
  1874. markPresetModified(gui.getRoot(), true);
  1875. }
  1876. return r;
  1877. }, controller.setValue);
  1878. }
  1879. function recallSavedValue(gui, controller) {
  1880. // Find the topmost GUI, that's where remembered objects live.
  1881. var root = gui.getRoot();
  1882. // Does the object we're controlling match anything we've been told to
  1883. // remember?
  1884. var matched_index = root.__rememberedObjects.indexOf(controller.object);
  1885. // Why yes, it does!
  1886. if (matched_index != -1) {
  1887. // Let me fetch a map of controllers for thcommon.isObject.
  1888. var controller_map =
  1889. root.__rememberedObjectIndecesToControllers[matched_index];
  1890. // Ohp, I believe this is the first controller we've created for this
  1891. // object. Lets make the map fresh.
  1892. if (controller_map === undefined) {
  1893. controller_map = {};
  1894. root.__rememberedObjectIndecesToControllers[matched_index] =
  1895. controller_map;
  1896. }
  1897. // Keep track of this controller
  1898. controller_map[controller.property] = controller;
  1899. // Okay, now have we saved any values for this controller?
  1900. if (root.load && root.load.remembered) {
  1901. var preset_map = root.load.remembered;
  1902. // Which preset are we trying to load?
  1903. var preset;
  1904. if (preset_map[gui.preset]) {
  1905. preset = preset_map[gui.preset];
  1906. } else if (preset_map[DEFAULT_DEFAULT_PRESET_NAME]) {
  1907. // Uhh, you can have the default instead?
  1908. preset = preset_map[DEFAULT_DEFAULT_PRESET_NAME];
  1909. } else {
  1910. // Nada.
  1911. return;
  1912. }
  1913. // Did the loaded object remember thcommon.isObject?
  1914. if (preset[matched_index] &&
  1915. // Did we remember this particular property?
  1916. preset[matched_index][controller.property] !== undefined) {
  1917. // We did remember something for this guy ...
  1918. var value = preset[matched_index][controller.property];
  1919. // And that's what it is.
  1920. controller.initialValue = value;
  1921. controller.setValue(value);
  1922. }
  1923. }
  1924. }
  1925. }
  1926. function getLocalStorageHash(gui, key) {
  1927. // TODO how does this deal with multiple GUI's?
  1928. return document.location.href + '.' + key;
  1929. }
  1930. function addSaveMenu(gui) {
  1931. var div = gui.__save_row = document.createElement('li');
  1932. dom.addClass(gui.domElement, 'has-save');
  1933. gui.__ul.insertBefore(div, gui.__ul.firstChild);
  1934. dom.addClass(div, 'save-row');
  1935. var gears = document.createElement('span');
  1936. gears.innerHTML = '&nbsp;';
  1937. dom.addClass(gears, 'button gears');
  1938. // TODO replace with FunctionController
  1939. var button = document.createElement('span');
  1940. button.innerHTML = 'Save';
  1941. dom.addClass(button, 'button');
  1942. dom.addClass(button, 'save');
  1943. var button2 = document.createElement('span');
  1944. button2.innerHTML = 'New';
  1945. dom.addClass(button2, 'button');
  1946. dom.addClass(button2, 'save-as');
  1947. var button3 = document.createElement('span');
  1948. button3.innerHTML = 'Revert';
  1949. dom.addClass(button3, 'button');
  1950. dom.addClass(button3, 'revert');
  1951. var select = gui.__preset_select = document.createElement('select');
  1952. if (gui.load && gui.load.remembered) {
  1953. common.each(gui.load.remembered, function(value, key) {
  1954. addPresetOption(gui, key, key == gui.preset);
  1955. });
  1956. } else {
  1957. addPresetOption(gui, DEFAULT_DEFAULT_PRESET_NAME, false);
  1958. }
  1959. dom.bind(select, 'change', function() {
  1960. for (var index = 0; index < gui.__preset_select.length; index++) {
  1961. gui.__preset_select[index].innerHTML = gui.__preset_select[index].value;
  1962. }
  1963. gui.preset = this.value;
  1964. });
  1965. div.appendChild(select);
  1966. div.appendChild(gears);
  1967. div.appendChild(button);
  1968. div.appendChild(button2);
  1969. div.appendChild(button3);
  1970. if (SUPPORTS_LOCAL_STORAGE) {
  1971. var saveLocally = document.getElementById('dg-save-locally');
  1972. var explain = document.getElementById('dg-local-explain');
  1973. saveLocally.style.display = 'block';
  1974. var localStorageCheckBox = document.getElementById('dg-local-storage');
  1975. if (localStorage.getItem(getLocalStorageHash(gui, 'isLocal')) === 'true') {
  1976. localStorageCheckBox.setAttribute('checked', 'checked');
  1977. }
  1978. function showHideExplain() {
  1979. explain.style.display = gui.useLocalStorage ? 'block' : 'none';
  1980. }
  1981. showHideExplain();
  1982. // TODO: Use a boolean controller, fool!
  1983. dom.bind(localStorageCheckBox, 'change', function() {
  1984. gui.useLocalStorage = !gui.useLocalStorage;
  1985. showHideExplain();
  1986. });
  1987. }
  1988. var newConstructorTextArea = document.getElementById('dg-new-constructor');
  1989. dom.bind(newConstructorTextArea, 'keydown', function(e) {
  1990. if (e.metaKey && (e.which === 67 || e.keyCode == 67)) {
  1991. SAVE_DIALOGUE.hide();
  1992. }
  1993. });
  1994. dom.bind(gears, 'click', function() {
  1995. newConstructorTextArea.innerHTML = JSON.stringify(gui.getSaveObject(), undefined, 2);
  1996. SAVE_DIALOGUE.show();
  1997. newConstructorTextArea.focus();
  1998. newConstructorTextArea.select();
  1999. });
  2000. dom.bind(button, 'click', function() {
  2001. gui.save();
  2002. });
  2003. dom.bind(button2, 'click', function() {
  2004. var presetName = prompt('Enter a new preset name.');
  2005. if (presetName) gui.saveAs(presetName);
  2006. });
  2007. dom.bind(button3, 'click', function() {
  2008. gui.revert();
  2009. });
  2010. // div.appendChild(button2);
  2011. }
  2012. function addResizeHandle(gui) {
  2013. gui.__resize_handle = document.createElement('div');
  2014. common.extend(gui.__resize_handle.style, {
  2015. width: '6px',
  2016. marginLeft: '-3px',
  2017. height: '200px',
  2018. cursor: 'ew-resize',
  2019. position: 'absolute'
  2020. // border: '1px solid blue'
  2021. });
  2022. var pmouseX;
  2023. dom.bind(gui.__resize_handle, 'mousedown', dragStart);
  2024. dom.bind(gui.__closeButton, 'mousedown', dragStart);
  2025. gui.domElement.insertBefore(gui.__resize_handle, gui.domElement.firstElementChild);
  2026. function dragStart(e) {
  2027. e.preventDefault();
  2028. pmouseX = e.clientX;
  2029. dom.addClass(gui.__closeButton, GUI.CLASS_DRAG);
  2030. dom.bind(window, 'mousemove', drag);
  2031. dom.bind(window, 'mouseup', dragStop);
  2032. return false;
  2033. }
  2034. function drag(e) {
  2035. e.preventDefault();
  2036. gui.width += pmouseX - e.clientX;
  2037. gui.onResize();
  2038. pmouseX = e.clientX;
  2039. return false;
  2040. }
  2041. function dragStop() {
  2042. dom.removeClass(gui.__closeButton, GUI.CLASS_DRAG);
  2043. dom.unbind(window, 'mousemove', drag);
  2044. dom.unbind(window, 'mouseup', dragStop);
  2045. }
  2046. }
  2047. function setWidth(gui, w) {
  2048. gui.domElement.style.width = w + 'px';
  2049. // Auto placed save-rows are position fixed, so we have to
  2050. // set the width manually if we want it to bleed to the edge
  2051. if (gui.__save_row && gui.autoPlace) {
  2052. gui.__save_row.style.width = w + 'px';
  2053. }if (gui.__closeButton) {
  2054. gui.__closeButton.style.width = w + 'px';
  2055. }
  2056. }
  2057. function getCurrentPreset(gui, useInitialValues) {
  2058. var toReturn = {};
  2059. // For each object I'm remembering
  2060. common.each(gui.__rememberedObjects, function(val, index) {
  2061. var saved_values = {};
  2062. // The controllers I've made for thcommon.isObject by property
  2063. var controller_map =
  2064. gui.__rememberedObjectIndecesToControllers[index];
  2065. // Remember each value for each property
  2066. common.each(controller_map, function(controller, property) {
  2067. saved_values[property] = useInitialValues ? controller.initialValue : controller.getValue();
  2068. });
  2069. // Save the values for thcommon.isObject
  2070. toReturn[index] = saved_values;
  2071. });
  2072. return toReturn;
  2073. }
  2074. function addPresetOption(gui, name, setSelected) {
  2075. var opt = document.createElement('option');
  2076. opt.innerHTML = name;
  2077. opt.value = name;
  2078. gui.__preset_select.appendChild(opt);
  2079. if (setSelected) {
  2080. gui.__preset_select.selectedIndex = gui.__preset_select.length - 1;
  2081. }
  2082. }
  2083. function setPresetSelectIndex(gui) {
  2084. for (var index = 0; index < gui.__preset_select.length; index++) {
  2085. if (gui.__preset_select[index].value == gui.preset) {
  2086. gui.__preset_select.selectedIndex = index;
  2087. }
  2088. }
  2089. }
  2090. function markPresetModified(gui, modified) {
  2091. var opt = gui.__preset_select[gui.__preset_select.selectedIndex];
  2092. // console.log('mark', modified, opt);
  2093. if (modified) {
  2094. opt.innerHTML = opt.value + "*";
  2095. } else {
  2096. opt.innerHTML = opt.value;
  2097. }
  2098. }
  2099. function updateDisplays(controllerArray) {
  2100. if (controllerArray.length != 0) {
  2101. requestAnimationFrame(function() {
  2102. updateDisplays(controllerArray);
  2103. });
  2104. }
  2105. common.each(controllerArray, function(c) {
  2106. c.updateDisplay();
  2107. });
  2108. }
  2109. return GUI;
  2110. })(dat.utils.css,
  2111. "<div id=\"dg-save\" class=\"dg dialogue\">\n\n Here's the new load parameter for your <code>GUI</code>'s constructor:\n\n <textarea id=\"dg-new-constructor\"></textarea>\n\n <div id=\"dg-save-locally\">\n\n <input id=\"dg-local-storage\" type=\"checkbox\"/> Automatically save\n values to <code>localStorage</code> on exit.\n\n <div id=\"dg-local-explain\">The values saved to <code>localStorage</code> will\n override those passed to <code>dat.GUI</code>'s constructor. This makes it\n easier to work incrementally, but <code>localStorage</code> is fragile,\n and your friends may not see the same values you do.\n \n </div>\n \n </div>\n\n</div>",
  2112. ".dg ul{list-style:none;margin:0;padding:0;width:100%;clear:both}.dg.ac{position:fixed;top:0;left:0;right:0;height:0;z-index:0}.dg:not(.ac) .main{overflow:hidden}.dg.main{-webkit-transition:opacity 0.1s linear;-o-transition:opacity 0.1s linear;-moz-transition:opacity 0.1s linear;transition:opacity 0.1s linear}.dg.main.taller-than-window{overflow-y:auto}.dg.main.taller-than-window .close-button{opacity:1;margin-top:-1px;border-top:1px solid #2c2c2c}.dg.main ul.closed .close-button{opacity:1 !important}.dg.main:hover .close-button,.dg.main .close-button.drag{opacity:1}.dg.main .close-button{-webkit-transition:opacity 0.1s linear;-o-transition:opacity 0.1s linear;-moz-transition:opacity 0.1s linear;transition:opacity 0.1s linear;border:0;position:absolute;line-height:19px;height:20px;cursor:pointer;text-align:center;background-color:#000}.dg.main .close-button:hover{background-color:#111}.dg.a{float:right;margin-right:15px;overflow-x:hidden}.dg.a.has-save ul{margin-top:27px}.dg.a.has-save ul.closed{margin-top:0}.dg.a .save-row{position:fixed;top:0;z-index:1002}.dg li{-webkit-transition:height 0.1s ease-out;-o-transition:height 0.1s ease-out;-moz-transition:height 0.1s ease-out;transition:height 0.1s ease-out}.dg li:not(.folder){cursor:auto;height:27px;line-height:27px;overflow:hidden;padding:0 4px 0 5px}.dg li.folder{padding:0;border-left:4px solid rgba(0,0,0,0)}.dg li.title{cursor:pointer;margin-left:-4px}.dg .closed li:not(.title),.dg .closed ul li,.dg .closed ul li > *{height:0;overflow:hidden;border:0}.dg .cr{clear:both;padding-left:3px;height:27px}.dg .property-name{cursor:default;float:left;clear:left;width:40%;overflow:hidden;text-overflow:ellipsis}.dg .c{float:left;width:60%}.dg .c input[type=text]{border:0;margin-top:4px;padding:3px;width:100%;float:right}.dg .has-slider input[type=text]{width:30%;margin-left:0}.dg .slider{float:left;width:66%;margin-left:-5px;margin-right:0;height:19px;margin-top:4px}.dg .slider-fg{height:100%}.dg .c input[type=checkbox]{margin-top:9px}.dg .c select{margin-top:5px}.dg .cr.function,.dg .cr.function .property-name,.dg .cr.function *,.dg .cr.boolean,.dg .cr.boolean *{cursor:pointer}.dg .selector{display:none;position:absolute;margin-left:-9px;margin-top:23px;z-index:10}.dg .c:hover .selector,.dg .selector.drag{display:block}.dg li.save-row{padding:0}.dg li.save-row .button{display:inline-block;padding:0px 6px}.dg.dialogue{background-color:#222;width:460px;padding:15px;font-size:13px;line-height:15px}#dg-new-constructor{padding:10px;color:#222;font-family:Monaco, monospace;font-size:10px;border:0;resize:none;box-shadow:inset 1px 1px 1px #888;word-wrap:break-word;margin:12px 0;display:block;width:440px;overflow-y:scroll;height:100px;position:relative}#dg-local-explain{display:none;font-size:11px;line-height:17px;border-radius:3px;background-color:#333;padding:8px;margin-top:10px}#dg-local-explain code{font-size:10px}#dat-gui-save-locally{display:none}.dg{color:#eee;font:11px 'Lucida Grande', sans-serif;text-shadow:0 -1px 0 #111}.dg.main::-webkit-scrollbar{width:5px;background:#1a1a1a}.dg.main::-webkit-scrollbar-corner{height:0;display:none}.dg.main::-webkit-scrollbar-thumb{border-radius:5px;background:#676767}.dg li:not(.folder){background:#1a1a1a;border-bottom:1px solid #2c2c2c}.dg li.save-row{line-height:25px;background:#dad5cb;border:0}.dg li.save-row select{margin-left:5px;width:108px}.dg li.save-row .button{margin-left:5px;margin-top:1px;border-radius:2px;font-size:9px;line-height:7px;padding:4px 4px 5px 4px;background:#c5bdad;color:#fff;text-shadow:0 1px 0 #b0a58f;box-shadow:0 -1px 0 #b0a58f;cursor:pointer}.dg li.save-row .button.gears{background:#c5bdad url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAsAAAANCAYAAAB/9ZQ7AAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAQJJREFUeNpiYKAU/P//PwGIC/ApCABiBSAW+I8AClAcgKxQ4T9hoMAEUrxx2QSGN6+egDX+/vWT4e7N82AMYoPAx/evwWoYoSYbACX2s7KxCxzcsezDh3evFoDEBYTEEqycggWAzA9AuUSQQgeYPa9fPv6/YWm/Acx5IPb7ty/fw+QZblw67vDs8R0YHyQhgObx+yAJkBqmG5dPPDh1aPOGR/eugW0G4vlIoTIfyFcA+QekhhHJhPdQxbiAIguMBTQZrPD7108M6roWYDFQiIAAv6Aow/1bFwXgis+f2LUAynwoIaNcz8XNx3Dl7MEJUDGQpx9gtQ8YCueB+D26OECAAQDadt7e46D42QAAAABJRU5ErkJggg==) 2px 1px no-repeat;height:7px;width:8px}.dg li.save-row .button:hover{background-color:#bab19e;box-shadow:0 -1px 0 #b0a58f}.dg li.folder{border-bottom:0}.dg li.title{padding-left:16px;background:#000 url(data:image/gif;base64,R0lGODlhBQAFAJEAAP////Pz8////////yH5BAEAAAIALAAAAAAFAAUAAAIIlI+hKgFxoCgAOw==) 6px 10px no-repeat;cursor:pointer;border-bottom:1px solid rgba(255,255,255,0.2)}.dg .closed li.title{background-image:url(data:image/gif;base64,R0lGODlhBQAFAJEAAP////Pz8////////yH5BAEAAAIALAAAAAAFAAUAAAIIlGIWqMCbWAEAOw==)}.dg .cr.boolean{border-left:3px solid #806787}.dg .cr.function{border-left:3px solid #e61d5f}.dg .cr.number{border-left:3px solid #2fa1d6}.dg .cr.number input[type=text]{color:#2fa1d6}.dg .cr.string{border-left:3px solid #1ed36f}.dg .cr.string input[type=text]{color:#1ed36f}.dg .cr.function:hover,.dg .cr.boolean:hover{background:#111}.dg .c input[type=text]{background:#303030;outline:none}.dg .c input[type=text]:hover{background:#3c3c3c}.dg .c input[type=text]:focus{background:#494949;color:#fff}.dg .c .slider{background:#303030;cursor:ew-resize}.dg .c .slider-fg{background:#2fa1d6}.dg .c .slider:hover{background:#3c3c3c}.dg .c .slider:hover .slider-fg{background:#44abda}\n",
  2113. dat.controllers.factory = (function (OptionController, NumberControllerBox, NumberControllerSlider, StringController, FunctionController, BooleanController, common) {
  2114. return function(object, property) {
  2115. var initialValue = object[property];
  2116. // Providing options?
  2117. if (common.isArray(arguments[2]) || common.isObject(arguments[2])) {
  2118. return new OptionController(object, property, arguments[2]);
  2119. }
  2120. // Providing a map?
  2121. if (common.isNumber(initialValue)) {
  2122. if (common.isNumber(arguments[2]) && common.isNumber(arguments[3])) {
  2123. // Has min and max.
  2124. return new NumberControllerSlider(object, property, arguments[2], arguments[3]);
  2125. } else {
  2126. return new NumberControllerBox(object, property, { min: arguments[2], max: arguments[3] });
  2127. }
  2128. }
  2129. if (common.isString(initialValue)) {
  2130. return new StringController(object, property);
  2131. }
  2132. if (common.isFunction(initialValue)) {
  2133. return new FunctionController(object, property, '');
  2134. }
  2135. if (common.isBoolean(initialValue)) {
  2136. return new BooleanController(object, property);
  2137. }
  2138. }
  2139. })(dat.controllers.OptionController,
  2140. dat.controllers.NumberControllerBox,
  2141. dat.controllers.NumberControllerSlider,
  2142. dat.controllers.StringController = (function (Controller, dom, common) {
  2143. /**
  2144. * @class Provides a text input to alter the string property of an object.
  2145. *
  2146. * @extends dat.controllers.Controller
  2147. *
  2148. * @param {Object} object The object to be manipulated
  2149. * @param {string} property The name of the property to be manipulated
  2150. *
  2151. * @member dat.controllers
  2152. */
  2153. var StringController = function(object, property) {
  2154. StringController.superclass.call(this, object, property);
  2155. var _this = this;
  2156. this.__input = document.createElement('input');
  2157. this.__input.setAttribute('type', 'text');
  2158. dom.bind(this.__input, 'keyup', onChange);
  2159. dom.bind(this.__input, 'change', onChange);
  2160. dom.bind(this.__input, 'blur', onBlur);
  2161. dom.bind(this.__input, 'keydown', function(e) {
  2162. if (e.keyCode === 13) {
  2163. this.blur();
  2164. }
  2165. });
  2166. function onChange() {
  2167. _this.setValue(_this.__input.value);
  2168. }
  2169. function onBlur() {
  2170. if (_this.__onFinishChange) {
  2171. _this.__onFinishChange.call(_this, _this.getValue());
  2172. }
  2173. }
  2174. this.updateDisplay();
  2175. this.domElement.appendChild(this.__input);
  2176. };
  2177. StringController.superclass = Controller;
  2178. common.extend(
  2179. StringController.prototype,
  2180. Controller.prototype,
  2181. {
  2182. updateDisplay: function() {
  2183. // Stops the caret from moving on account of:
  2184. // keyup -> setValue -> updateDisplay
  2185. if (!dom.isActive(this.__input)) {
  2186. this.__input.value = this.getValue();
  2187. }
  2188. return StringController.superclass.prototype.updateDisplay.call(this);
  2189. }
  2190. }
  2191. );
  2192. return StringController;
  2193. })(dat.controllers.Controller,
  2194. dat.dom.dom,
  2195. dat.utils.common),
  2196. dat.controllers.FunctionController,
  2197. dat.controllers.BooleanController,
  2198. dat.utils.common),
  2199. dat.controllers.Controller,
  2200. dat.controllers.BooleanController,
  2201. dat.controllers.FunctionController,
  2202. dat.controllers.NumberControllerBox,
  2203. dat.controllers.NumberControllerSlider,
  2204. dat.controllers.OptionController,
  2205. dat.controllers.ColorController = (function (Controller, dom, Color, interpret, common) {
  2206. var ColorController = function(object, property) {
  2207. ColorController.superclass.call(this, object, property);
  2208. this.__color = new Color(this.getValue());
  2209. this.__temp = new Color(0);
  2210. var _this = this;
  2211. this.domElement = document.createElement('div');
  2212. dom.makeSelectable(this.domElement, false);
  2213. this.__selector = document.createElement('div');
  2214. this.__selector.className = 'selector';
  2215. this.__saturation_field = document.createElement('div');
  2216. this.__saturation_field.className = 'saturation-field';
  2217. this.__field_knob = document.createElement('div');
  2218. this.__field_knob.className = 'field-knob';
  2219. this.__field_knob_border = '2px solid ';
  2220. this.__hue_knob = document.createElement('div');
  2221. this.__hue_knob.className = 'hue-knob';
  2222. this.__hue_field = document.createElement('div');
  2223. this.__hue_field.className = 'hue-field';
  2224. this.__input = document.createElement('input');
  2225. this.__input.type = 'text';
  2226. this.__input_textShadow = '0 1px 1px ';
  2227. dom.bind(this.__input, 'keydown', function(e) {
  2228. if (e.keyCode === 13) { // on enter
  2229. onBlur.call(this);
  2230. }
  2231. });
  2232. dom.bind(this.__input, 'blur', onBlur);
  2233. dom.bind(this.__selector, 'mousedown', function(e) {
  2234. dom
  2235. .addClass(this, 'drag')
  2236. .bind(window, 'mouseup', function(e) {
  2237. dom.removeClass(_this.__selector, 'drag');
  2238. });
  2239. });
  2240. var value_field = document.createElement('div');
  2241. common.extend(this.__selector.style, {
  2242. width: '122px',
  2243. height: '102px',
  2244. padding: '3px',
  2245. backgroundColor: '#222',
  2246. boxShadow: '0px 1px 3px rgba(0,0,0,0.3)'
  2247. });
  2248. common.extend(this.__field_knob.style, {
  2249. position: 'absolute',
  2250. width: '12px',
  2251. height: '12px',
  2252. border: this.__field_knob_border + (this.__color.v < .5 ? '#fff' : '#000'),
  2253. boxShadow: '0px 1px 3px rgba(0,0,0,0.5)',
  2254. borderRadius: '12px',
  2255. zIndex: 1
  2256. });
  2257. common.extend(this.__hue_knob.style, {
  2258. position: 'absolute',
  2259. width: '15px',
  2260. height: '2px',
  2261. borderRight: '4px solid #fff',
  2262. zIndex: 1
  2263. });
  2264. common.extend(this.__saturation_field.style, {
  2265. width: '100px',
  2266. height: '100px',
  2267. border: '1px solid #555',
  2268. marginRight: '3px',
  2269. display: 'inline-block',
  2270. cursor: 'pointer'
  2271. });
  2272. common.extend(value_field.style, {
  2273. width: '100%',
  2274. height: '100%',
  2275. background: 'none'
  2276. });
  2277. linearGradient(value_field, 'top', 'rgba(0,0,0,0)', '#000');
  2278. common.extend(this.__hue_field.style, {
  2279. width: '15px',
  2280. height: '100px',
  2281. display: 'inline-block',
  2282. border: '1px solid #555',
  2283. cursor: 'ns-resize'
  2284. });
  2285. hueGradient(this.__hue_field);
  2286. common.extend(this.__input.style, {
  2287. outline: 'none',
  2288. // width: '120px',
  2289. textAlign: 'center',
  2290. // padding: '4px',
  2291. // marginBottom: '6px',
  2292. color: '#fff',
  2293. border: 0,
  2294. fontWeight: 'bold',
  2295. textShadow: this.__input_textShadow + 'rgba(0,0,0,0.7)'
  2296. });
  2297. dom.bind(this.__saturation_field, 'mousedown', fieldDown);
  2298. dom.bind(this.__field_knob, 'mousedown', fieldDown);
  2299. dom.bind(this.__hue_field, 'mousedown', function(e) {
  2300. setH(e);
  2301. dom.bind(window, 'mousemove', setH);
  2302. dom.bind(window, 'mouseup', unbindH);
  2303. });
  2304. function fieldDown(e) {
  2305. setSV(e);
  2306. // document.body.style.cursor = 'none';
  2307. dom.bind(window, 'mousemove', setSV);
  2308. dom.bind(window, 'mouseup', unbindSV);
  2309. }
  2310. function unbindSV() {
  2311. dom.unbind(window, 'mousemove', setSV);
  2312. dom.unbind(window, 'mouseup', unbindSV);
  2313. // document.body.style.cursor = 'default';
  2314. }
  2315. function onBlur() {
  2316. var i = interpret(this.value);
  2317. if (i !== false) {
  2318. _this.__color.__state = i;
  2319. _this.setValue(_this.__color.toOriginal());
  2320. } else {
  2321. this.value = _this.__color.toString();
  2322. }
  2323. }
  2324. function unbindH() {
  2325. dom.unbind(window, 'mousemove', setH);
  2326. dom.unbind(window, 'mouseup', unbindH);
  2327. }
  2328. this.__saturation_field.appendChild(value_field);
  2329. this.__selector.appendChild(this.__field_knob);
  2330. this.__selector.appendChild(this.__saturation_field);
  2331. this.__selector.appendChild(this.__hue_field);
  2332. this.__hue_field.appendChild(this.__hue_knob);
  2333. this.domElement.appendChild(this.__input);
  2334. this.domElement.appendChild(this.__selector);
  2335. this.updateDisplay();
  2336. function setSV(e) {
  2337. e.preventDefault();
  2338. var w = dom.getWidth(_this.__saturation_field);
  2339. var o = dom.getOffset(_this.__saturation_field);
  2340. var s = (e.clientX - o.left + document.body.scrollLeft) / w;
  2341. var v = 1 - (e.clientY - o.top + document.body.scrollTop) / w;
  2342. if (v > 1) v = 1;
  2343. else if (v < 0) v = 0;
  2344. if (s > 1) s = 1;
  2345. else if (s < 0) s = 0;
  2346. _this.__color.v = v;
  2347. _this.__color.s = s;
  2348. _this.setValue(_this.__color.toOriginal());
  2349. return false;
  2350. }
  2351. function setH(e) {
  2352. e.preventDefault();
  2353. var s = dom.getHeight(_this.__hue_field);
  2354. var o = dom.getOffset(_this.__hue_field);
  2355. var h = 1 - (e.clientY - o.top + document.body.scrollTop) / s;
  2356. if (h > 1) h = 1;
  2357. else if (h < 0) h = 0;
  2358. _this.__color.h = h * 360;
  2359. _this.setValue(_this.__color.toOriginal());
  2360. return false;
  2361. }
  2362. };
  2363. ColorController.superclass = Controller;
  2364. common.extend(
  2365. ColorController.prototype,
  2366. Controller.prototype,
  2367. {
  2368. updateDisplay: function() {
  2369. var i = interpret(this.getValue());
  2370. if (i !== false) {
  2371. var mismatch = false;
  2372. // Check for mismatch on the interpreted value.
  2373. common.each(Color.COMPONENTS, function(component) {
  2374. if (!common.isUndefined(i[component]) &&
  2375. !common.isUndefined(this.__color.__state[component]) &&
  2376. i[component] !== this.__color.__state[component]) {
  2377. mismatch = true;
  2378. return {}; // break
  2379. }
  2380. }, this);
  2381. // If nothing diverges, we keep our previous values
  2382. // for statefulness, otherwise we recalculate fresh
  2383. if (mismatch) {
  2384. common.extend(this.__color.__state, i);
  2385. }
  2386. }
  2387. common.extend(this.__temp.__state, this.__color.__state);
  2388. this.__temp.a = 1;
  2389. var flip = (this.__color.v < .5 || this.__color.s > .5) ? 255 : 0;
  2390. var _flip = 255 - flip;
  2391. common.extend(this.__field_knob.style, {
  2392. marginLeft: 100 * this.__color.s - 7 + 'px',
  2393. marginTop: 100 * (1 - this.__color.v) - 7 + 'px',
  2394. backgroundColor: this.__temp.toString(),
  2395. border: this.__field_knob_border + 'rgb(' + flip + ',' + flip + ',' + flip +')'
  2396. });
  2397. this.__hue_knob.style.marginTop = (1 - this.__color.h / 360) * 100 + 'px'
  2398. this.__temp.s = 1;
  2399. this.__temp.v = 1;
  2400. linearGradient(this.__saturation_field, 'left', '#fff', this.__temp.toString());
  2401. common.extend(this.__input.style, {
  2402. backgroundColor: this.__input.value = this.__color.toString(),
  2403. color: 'rgb(' + flip + ',' + flip + ',' + flip +')',
  2404. textShadow: this.__input_textShadow + 'rgba(' + _flip + ',' + _flip + ',' + _flip +',.7)'
  2405. });
  2406. }
  2407. }
  2408. );
  2409. var vendors = ['-moz-','-o-','-webkit-','-ms-',''];
  2410. function linearGradient(elem, x, a, b) {
  2411. elem.style.background = '';
  2412. common.each(vendors, function(vendor) {
  2413. elem.style.cssText += 'background: ' + vendor + 'linear-gradient('+x+', '+a+' 0%, ' + b + ' 100%); ';
  2414. });
  2415. }
  2416. function hueGradient(elem) {
  2417. elem.style.background = '';
  2418. elem.style.cssText += 'background: -moz-linear-gradient(top, #ff0000 0%, #ff00ff 17%, #0000ff 34%, #00ffff 50%, #00ff00 67%, #ffff00 84%, #ff0000 100%);'
  2419. elem.style.cssText += 'background: -webkit-linear-gradient(top, #ff0000 0%,#ff00ff 17%,#0000ff 34%,#00ffff 50%,#00ff00 67%,#ffff00 84%,#ff0000 100%);'
  2420. elem.style.cssText += 'background: -o-linear-gradient(top, #ff0000 0%,#ff00ff 17%,#0000ff 34%,#00ffff 50%,#00ff00 67%,#ffff00 84%,#ff0000 100%);'
  2421. elem.style.cssText += 'background: -ms-linear-gradient(top, #ff0000 0%,#ff00ff 17%,#0000ff 34%,#00ffff 50%,#00ff00 67%,#ffff00 84%,#ff0000 100%);'
  2422. elem.style.cssText += 'background: linear-gradient(top, #ff0000 0%,#ff00ff 17%,#0000ff 34%,#00ffff 50%,#00ff00 67%,#ffff00 84%,#ff0000 100%);'
  2423. }
  2424. return ColorController;
  2425. })(dat.controllers.Controller,
  2426. dat.dom.dom,
  2427. dat.color.Color = (function (interpret, math, toString, common) {
  2428. var Color = function() {
  2429. this.__state = interpret.apply(this, arguments);
  2430. if (this.__state === false) {
  2431. throw 'Failed to interpret color arguments';
  2432. }
  2433. this.__state.a = this.__state.a || 1;
  2434. };
  2435. Color.COMPONENTS = ['r','g','b','h','s','v','hex','a'];
  2436. common.extend(Color.prototype, {
  2437. toString: function() {
  2438. return toString(this);
  2439. },
  2440. toOriginal: function() {
  2441. return this.__state.conversion.write(this);
  2442. }
  2443. });
  2444. defineRGBComponent(Color.prototype, 'r', 2);
  2445. defineRGBComponent(Color.prototype, 'g', 1);
  2446. defineRGBComponent(Color.prototype, 'b', 0);
  2447. defineHSVComponent(Color.prototype, 'h');
  2448. defineHSVComponent(Color.prototype, 's');
  2449. defineHSVComponent(Color.prototype, 'v');
  2450. Object.defineProperty(Color.prototype, 'a', {
  2451. get: function() {
  2452. return this.__state.a;
  2453. },
  2454. set: function(v) {
  2455. this.__state.a = v;
  2456. }
  2457. });
  2458. Object.defineProperty(Color.prototype, 'hex', {
  2459. get: function() {
  2460. if (!this.__state.space !== 'HEX') {
  2461. this.__state.hex = math.rgb_to_hex(this.r, this.g, this.b);
  2462. }
  2463. return this.__state.hex;
  2464. },
  2465. set: function(v) {
  2466. this.__state.space = 'HEX';
  2467. this.__state.hex = v;
  2468. }
  2469. });
  2470. function defineRGBComponent(target, component, componentHexIndex) {
  2471. Object.defineProperty(target, component, {
  2472. get: function() {
  2473. if (this.__state.space === 'RGB') {
  2474. return this.__state[component];
  2475. }
  2476. recalculateRGB(this, component, componentHexIndex);
  2477. return this.__state[component];
  2478. },
  2479. set: function(v) {
  2480. if (this.__state.space !== 'RGB') {
  2481. recalculateRGB(this, component, componentHexIndex);
  2482. this.__state.space = 'RGB';
  2483. }
  2484. this.__state[component] = v;
  2485. }
  2486. });
  2487. }
  2488. function defineHSVComponent(target, component) {
  2489. Object.defineProperty(target, component, {
  2490. get: function() {
  2491. if (this.__state.space === 'HSV')
  2492. return this.__state[component];
  2493. recalculateHSV(this);
  2494. return this.__state[component];
  2495. },
  2496. set: function(v) {
  2497. if (this.__state.space !== 'HSV') {
  2498. recalculateHSV(this);
  2499. this.__state.space = 'HSV';
  2500. }
  2501. this.__state[component] = v;
  2502. }
  2503. });
  2504. }
  2505. function recalculateRGB(color, component, componentHexIndex) {
  2506. if (color.__state.space === 'HEX') {
  2507. color.__state[component] = math.component_from_hex(color.__state.hex, componentHexIndex);
  2508. } else if (color.__state.space === 'HSV') {
  2509. common.extend(color.__state, math.hsv_to_rgb(color.__state.h, color.__state.s, color.__state.v));
  2510. } else {
  2511. throw 'Corrupted color state';
  2512. }
  2513. }
  2514. function recalculateHSV(color) {
  2515. var result = math.rgb_to_hsv(color.r, color.g, color.b);
  2516. common.extend(color.__state,
  2517. {
  2518. s: result.s,
  2519. v: result.v
  2520. }
  2521. );
  2522. if (!common.isNaN(result.h)) {
  2523. color.__state.h = result.h;
  2524. } else if (common.isUndefined(color.__state.h)) {
  2525. color.__state.h = 0;
  2526. }
  2527. }
  2528. return Color;
  2529. })(dat.color.interpret,
  2530. dat.color.math = (function () {
  2531. var tmpComponent;
  2532. return {
  2533. hsv_to_rgb: function(h, s, v) {
  2534. var hi = Math.floor(h / 60) % 6;
  2535. var f = h / 60 - Math.floor(h / 60);
  2536. var p = v * (1.0 - s);
  2537. var q = v * (1.0 - (f * s));
  2538. var t = v * (1.0 - ((1.0 - f) * s));
  2539. var c = [
  2540. [v, t, p],
  2541. [q, v, p],
  2542. [p, v, t],
  2543. [p, q, v],
  2544. [t, p, v],
  2545. [v, p, q]
  2546. ][hi];
  2547. return {
  2548. r: c[0] * 255,
  2549. g: c[1] * 255,
  2550. b: c[2] * 255
  2551. };
  2552. },
  2553. rgb_to_hsv: function(r, g, b) {
  2554. var min = Math.min(r, g, b),
  2555. max = Math.max(r, g, b),
  2556. delta = max - min,
  2557. h, s;
  2558. if (max != 0) {
  2559. s = delta / max;
  2560. } else {
  2561. return {
  2562. h: NaN,
  2563. s: 0,
  2564. v: 0
  2565. };
  2566. }
  2567. if (r == max) {
  2568. h = (g - b) / delta;
  2569. } else if (g == max) {
  2570. h = 2 + (b - r) / delta;
  2571. } else {
  2572. h = 4 + (r - g) / delta;
  2573. }
  2574. h /= 6;
  2575. if (h < 0) {
  2576. h += 1;
  2577. }
  2578. return {
  2579. h: h * 360,
  2580. s: s,
  2581. v: max / 255
  2582. };
  2583. },
  2584. rgb_to_hex: function(r, g, b) {
  2585. var hex = this.hex_with_component(0, 2, r);
  2586. hex = this.hex_with_component(hex, 1, g);
  2587. hex = this.hex_with_component(hex, 0, b);
  2588. return hex;
  2589. },
  2590. component_from_hex: function(hex, componentIndex) {
  2591. return (hex >> (componentIndex * 8)) & 0xFF;
  2592. },
  2593. hex_with_component: function(hex, componentIndex, value) {
  2594. return value << (tmpComponent = componentIndex * 8) | (hex & ~ (0xFF << tmpComponent));
  2595. }
  2596. }
  2597. })(),
  2598. dat.color.toString,
  2599. dat.utils.common),
  2600. dat.color.interpret,
  2601. dat.utils.common),
  2602. dat.utils.requestAnimationFrame = (function () {
  2603. /**
  2604. * requirejs version of Paul Irish's RequestAnimationFrame
  2605. * http://paulirish.com/2011/requestanimationframe-for-smart-animating/
  2606. */
  2607. return window.webkitRequestAnimationFrame ||
  2608. window.mozRequestAnimationFrame ||
  2609. window.oRequestAnimationFrame ||
  2610. window.msRequestAnimationFrame ||
  2611. function(callback, element) {
  2612. window.setTimeout(callback, 1000 / 60);
  2613. };
  2614. })(),
  2615. dat.dom.CenteredDiv = (function (dom, common) {
  2616. var CenteredDiv = function() {
  2617. this.backgroundElement = document.createElement('div');
  2618. common.extend(this.backgroundElement.style, {
  2619. backgroundColor: 'rgba(0,0,0,0.8)',
  2620. top: 0,
  2621. left: 0,
  2622. display: 'none',
  2623. zIndex: '1000',
  2624. opacity: 0,
  2625. WebkitTransition: 'opacity 0.2s linear'
  2626. });
  2627. dom.makeFullscreen(this.backgroundElement);
  2628. this.backgroundElement.style.position = 'fixed';
  2629. this.domElement = document.createElement('div');
  2630. common.extend(this.domElement.style, {
  2631. position: 'fixed',
  2632. display: 'none',
  2633. zIndex: '1001',
  2634. opacity: 0,
  2635. WebkitTransition: '-webkit-transform 0.2s ease-out, opacity 0.2s linear'
  2636. });
  2637. document.body.appendChild(this.backgroundElement);
  2638. document.body.appendChild(this.domElement);
  2639. var _this = this;
  2640. dom.bind(this.backgroundElement, 'click', function() {
  2641. _this.hide();
  2642. });
  2643. };
  2644. CenteredDiv.prototype.show = function() {
  2645. var _this = this;
  2646. this.backgroundElement.style.display = 'block';
  2647. this.domElement.style.display = 'block';
  2648. this.domElement.style.opacity = 0;
  2649. // this.domElement.style.top = '52%';
  2650. this.domElement.style.webkitTransform = 'scale(1.1)';
  2651. this.layout();
  2652. common.defer(function() {
  2653. _this.backgroundElement.style.opacity = 1;
  2654. _this.domElement.style.opacity = 1;
  2655. _this.domElement.style.webkitTransform = 'scale(1)';
  2656. });
  2657. };
  2658. CenteredDiv.prototype.hide = function() {
  2659. var _this = this;
  2660. var hide = function() {
  2661. _this.domElement.style.display = 'none';
  2662. _this.backgroundElement.style.display = 'none';
  2663. dom.unbind(_this.domElement, 'webkitTransitionEnd', hide);
  2664. dom.unbind(_this.domElement, 'transitionend', hide);
  2665. dom.unbind(_this.domElement, 'oTransitionEnd', hide);
  2666. };
  2667. dom.bind(this.domElement, 'webkitTransitionEnd', hide);
  2668. dom.bind(this.domElement, 'transitionend', hide);
  2669. dom.bind(this.domElement, 'oTransitionEnd', hide);
  2670. this.backgroundElement.style.opacity = 0;
  2671. // this.domElement.style.top = '48%';
  2672. this.domElement.style.opacity = 0;
  2673. this.domElement.style.webkitTransform = 'scale(1.1)';
  2674. };
  2675. CenteredDiv.prototype.layout = function() {
  2676. this.domElement.style.left = window.innerWidth/2 - dom.getWidth(this.domElement) / 2 + 'px';
  2677. this.domElement.style.top = window.innerHeight/2 - dom.getHeight(this.domElement) / 2 + 'px';
  2678. };
  2679. function lockScroll(e) {
  2680. console.log(e);
  2681. }
  2682. return CenteredDiv;
  2683. })(dat.dom.dom,
  2684. dat.utils.common),
  2685. dat.dom.dom,
  2686. dat.utils.common);