fhem-tablet-ui.js 94 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506
  1. /* FHEM tablet ui */
  2. /* FHEM tablet ui */
  3. /**
  4. * UI builder framework for FHEM
  5. *
  6. * Version: 2.7.2
  7. *
  8. * Copyright (c) 2015-2018 Mario Stephan <mstephan@shared-files.de>
  9. * Under MIT License (http://www.opensource.org/licenses/mit-license.php)
  10. * https://github.com/knowthelist/fhem-tablet-ui
  11. */
  12. /* global Framework7:true, jQuery:true, Dom7:true */
  13. "use strict";
  14. // -------- Framework7---------
  15. // https://framework7.io/docs/
  16. if (typeof Framework7 === 'function') {
  17. var f7 = {
  18. ftui: new Framework7({
  19. animateNavBackIcon: true
  20. }),
  21. options: {
  22. dynamicNavbar: true,
  23. domCache: true
  24. },
  25. views: []
  26. };
  27. Dom7('.view').each(function (index) {
  28. var view = f7.ftui.addView('#' + Dom7(this)[0].id, {
  29. dynamicNavbar: true
  30. });
  31. f7.ftui.views.push(view);
  32. });
  33. f7.ftui.onPageInit('*', function (page) {
  34. ftui.log(1, 'f7: ' + page.name + ' initialized');
  35. ftui.initWidgets('.page[data-page="' + page.name + '"]');
  36. });
  37. }
  38. // -------- Widget Base---------
  39. var Modul_widget = function () {
  40. var subscriptions = {};
  41. var elements = [];
  42. function update_lock(dev, par) {
  43. $.each(['lock', 'lock-on', 'lock-off'], function (index, key) {
  44. me.elements.filterDeviceReading(key, dev, par)
  45. .each(function (idx) {
  46. var elem = $(this);
  47. var value = elem.getReading('lock').val;
  48. if (elem.matchingState('lock', value) === 'on') {
  49. elem.addClass('lock');
  50. }
  51. if (elem.matchingState('lock', value) === 'off') {
  52. elem.removeClass('lock');
  53. }
  54. });
  55. });
  56. }
  57. function update_hide(dev, par) {
  58. $.each(['hide', 'hide-on', 'hide-off'], function (index, key) {
  59. me.elements.filterDeviceReading(key, dev, par)
  60. .each(function (idx) {
  61. var elem = $(this);
  62. var value = elem.getReading('hide').val;
  63. if (elem.matchingState('hide', value) === 'on') {
  64. if (ftui.isValid(elem.data('hideparents'))) {
  65. elem.parents(elem.data('hideparents')).addClass('hide');
  66. } else {
  67. elem.addClass('hide');
  68. }
  69. }
  70. if (elem.matchingState('hide', value) === 'off') {
  71. if (ftui.isValid(elem.data('hideparents'))) {
  72. elem.parents(elem.data('hideparents')).removeClass('hide');
  73. } else {
  74. elem.removeClass('hide');
  75. }
  76. }
  77. });
  78. });
  79. }
  80. function updateHide(elem, dev, par) {
  81. $.each(['hide', 'hide-on', 'hide-off'], function (index, key) {
  82. if (elem.matchDeviceReading(key, dev, par)) {
  83. var value = elem.getReading('hide').val;
  84. if (elem.matchingState('hide', value) === 'on') {
  85. if (ftui.isValid(elem.data('hideparents'))) {
  86. elem.parents(elem.data('hideparents')).addClass('hide');
  87. } else {
  88. elem.addClass('hide');
  89. }
  90. }
  91. if (elem.matchingState('hide', value) === 'off') {
  92. if (ftui.isValid(elem.data('hideparents'))) {
  93. elem.parents(elem.data('hideparents')).removeClass('hide');
  94. } else {
  95. elem.removeClass('hide');
  96. }
  97. }
  98. }
  99. });
  100. }
  101. function updateLock(elem, dev, par) {
  102. $.each(['lock', 'lock-on', 'lock-off'], function (index, key) {
  103. if (elem.matchDeviceReading(key, dev, par)) {
  104. var value = elem.getReading('lock').val;
  105. if (elem.matchingState('lock', value) === 'on') {
  106. elem.addClass('lock');
  107. }
  108. if (elem.matchingState('lock', value) === 'off') {
  109. elem.removeClass('lock');
  110. }
  111. }
  112. });
  113. }
  114. function updateReachable(elem, dev, par) {
  115. $.each(['reachable', 'reachable-on', 'reachable-off'], function (index, key) {
  116. if (elem.matchDeviceReading(key, dev, par)) {
  117. var value = elem.getReading('reachable').val;
  118. if (elem.matchingState('reachable', value) === 'on') {
  119. elem.removeClass('unreachable');
  120. }
  121. if (elem.matchingState('reachable', value) === 'off') {
  122. elem.addClass('unreachable');
  123. }
  124. }
  125. });
  126. }
  127. function update_reachable(dev, par) {
  128. $.each(['reachable', 'reachable-on', 'reachable-off'], function (index, key) {
  129. me.elements.filterDeviceReading(key, dev, par)
  130. .each(function (idx) {
  131. var elem = $(this);
  132. var value = elem.getReading('reachable').val;
  133. if (elem.matchingState('reachable', value) === 'on') {
  134. elem.removeClass('unreachable');
  135. }
  136. if (elem.matchingState('reachable', value) === 'off') {
  137. elem.addClass('unreachable');
  138. }
  139. });
  140. });
  141. }
  142. function substitution(value, subst) {
  143. if (ftui.isValid(subst) && ftui.isValid(value)) {
  144. if ($.isArray(subst)) {
  145. for (var i = 0, len = subst.length; i < len; i += 2) {
  146. if (i + 1 < len) {
  147. value = value.replace(new RegExp(String(subst[i]), "g"), String(subst[i + 1]));
  148. }
  149. }
  150. } else if (subst.match(/^s/)) {
  151. var f = subst.substr(1, 1);
  152. var sub = subst.split(f);
  153. return (value) ? value.replace(new RegExp(sub[1], sub[3]), sub[2]) : '';
  154. } else if (subst.match(/weekdayshort/))
  155. return ftui.dateFromString(value).ee();
  156. else if (subst.match(/.*\(\)/))
  157. return eval('value.' + subst);
  158. }
  159. return value;
  160. }
  161. function round(value, precision) {
  162. return ($.isNumeric(value) && precision) ? ftui.round(Number(value), precision) : value;
  163. }
  164. function fix(value, len) {
  165. return ($.isNumeric(value) && len >= 0) ? Number(value).toFixed(len) : value;
  166. }
  167. function factor(value, fac) {
  168. return ($.isNumeric(value) && fac >= 0) ? Number(value) * fac : value;
  169. }
  170. function map(mapObj, readval, defaultVal) {
  171. if ((typeof mapObj === 'object') && (mapObj !== null)) {
  172. for (var key in mapObj) {
  173. if (readval === key || readval.match(new RegExp('^' + key + '$'))) {
  174. return mapObj[key];
  175. }
  176. }
  177. }
  178. return defaultVal;
  179. }
  180. function init_attr(elem) {
  181. elem.initData('get', 'STATE');
  182. var get = elem.data('get');
  183. elem.initData('set', (get !== 'STATE') ? get : '');
  184. elem.initData('cmd', 'set');
  185. elem.initData('get-on', '(true|1|on|open|ON)');
  186. elem.initData('get-off', '!on');
  187. me.addReading(elem, 'get');
  188. if (elem.isDeviceReading('get-on')) {
  189. me.addReading(elem, 'get-on');
  190. }
  191. if (elem.isDeviceReading('get-off')) {
  192. me.addReading(elem, 'get-off');
  193. }
  194. // reachable parameter
  195. elem.initData('reachable-on', '!off');
  196. elem.initData('reachable-off', '(false|0)');
  197. me.addReading(elem, 'reachable');
  198. // if hide reading is defined, set defaults for comparison
  199. if (elem.isValidData('hide')) {
  200. elem.initData('hide-on', '(true|1|on)');
  201. }
  202. elem.initData('hide', 'STATE');
  203. if (elem.isValidData('hide-on')) {
  204. elem.initData('hide-off', '!on');
  205. }
  206. me.addReading(elem, 'hide');
  207. // if lock reading is defined, set defaults for comparison
  208. if (elem.isValidData('lock')) {
  209. elem.initData('lock-on', '(true|1|on)');
  210. }
  211. elem.initData('lock', elem.data('get'));
  212. if (elem.isValidData('lock-on')) {
  213. elem.initData('lock-off', '!on');
  214. }
  215. me.addReading(elem, 'lock');
  216. }
  217. function init_ui(elem) {
  218. elem.text(me.widgetname);
  219. }
  220. function reinit() {}
  221. function init() {
  222. ftui.log(1, "init widget: name=" + me.widgetname + " area=" + me.area);
  223. me.elements = $('[data-type="' + me.widgetname + '"]:not([data-ready])', me.area);
  224. me.elements.each(function (index) {
  225. var elem = $(this);
  226. elem.attr("data-ready", "");
  227. me.init_attr(elem);
  228. elem = me.init_ui(elem);
  229. });
  230. }
  231. function addReading(elem, key) {
  232. var data = elem.data(key);
  233. if (ftui.isValid(data)) {
  234. if ($.isArray(data) || !data.toString().match(/^[#\.\[][^:]*$/)) {
  235. var device = elem.data('device');
  236. if (!$.isArray(data)) {
  237. data = new Array(data.toString());
  238. }
  239. for (var i = data.length - 1; i >= 0; i -= 1) {
  240. var reading = data[i];
  241. // fully qualified readings => DEVICE:READING
  242. if (reading.match(/:/)) {
  243. var fqreading = reading.split(':');
  244. device = fqreading[0].replace('[', '');
  245. reading = fqreading[1].replace(']', '');
  246. }
  247. // fill objects for mapping from FHEMWEB paramid to device + reading
  248. if (ftui.isValid(device) && ftui.isValid(reading) &&
  249. device !== '' && reading !== '' &&
  250. device !== ' ' && reading !== ' ') {
  251. device = device.toString();
  252. var paramid = (reading === 'STATE') ? device : [device, reading].join('-');
  253. subscriptions[paramid] = {};
  254. subscriptions[paramid].device = device;
  255. subscriptions[paramid].reading = reading;
  256. }
  257. }
  258. }
  259. }
  260. }
  261. function extractReadings(elem, key) {
  262. var data = elem.data(key);
  263. if (ftui.isValid(data)) {
  264. if ($.isArray(data) || !data.toString().match(/^[#\.\[][^:]*$/)) {
  265. if (!$.isArray(data)) {
  266. data = new Array(data.toString());
  267. }
  268. for (var i = data.length - 1; i >= 0; i -= 1) {
  269. var device, reading, item = data[i];
  270. // only fully qualified readings => DEVICE:READING
  271. if (item.match(/:/)) {
  272. var fqreading = item.split(':');
  273. device = fqreading[0].replace('[', '');
  274. reading = fqreading[1].replace(']', '');
  275. }
  276. // fill objects for mapping from FHEMWEB paramid to device + reading
  277. if (ftui.isValid(device) && ftui.isValid(reading) &&
  278. device !== '' && reading !== '' &&
  279. device !== ' ' && reading !== ' ') {
  280. device = device.toString();
  281. var paramid = (reading === 'STATE') ? device : [device, reading].join('-');
  282. subscriptions[paramid] = {};
  283. subscriptions[paramid].device = device;
  284. subscriptions[paramid].reading = reading;
  285. }
  286. }
  287. }
  288. }
  289. }
  290. function update(dev, par) {
  291. ftui.log(1, 'warning: ' + me.widgetname + ' has not implemented update function');
  292. }
  293. var me = {
  294. widgetname: 'widget',
  295. area: '',
  296. init: init,
  297. reinit: reinit,
  298. init_attr: init_attr,
  299. init_ui: init_ui,
  300. update: update,
  301. update_lock: update_lock,
  302. update_reachable: update_reachable,
  303. update_hide: update_hide,
  304. updateHide: updateHide,
  305. updateLock: updateLock,
  306. updateReachable: updateReachable,
  307. substitution: substitution,
  308. fix: fix,
  309. factor: factor,
  310. round: round,
  311. map: map,
  312. addReading: addReading,
  313. extractReadings: extractReadings,
  314. subscriptions: subscriptions,
  315. elements: elements
  316. };
  317. return me;
  318. };
  319. // ------- Plugins --------
  320. var plugins = {
  321. modules: [],
  322. addModule: function (module) {
  323. this.modules.push(module);
  324. },
  325. removeArea: function (area) {
  326. for (var i = this.modules.length - 1; i >= 0; i -= 1) {
  327. if (this.modules[i].area === area) {
  328. this.modules.splice(i, 1);
  329. }
  330. }
  331. },
  332. updateParameters: function () {
  333. ftui.subscriptions = {};
  334. ftui.subscriptionTs = {};
  335. ftui.devs = [ftui.config.webDevice];
  336. ftui.reads = ['STATE', 'longpoll'];
  337. for (var i = this.modules.length - 1; i >= 0; i -= 1) {
  338. var module = this.modules[i];
  339. for (var key in module.subscriptions) {
  340. ftui.subscriptions[key] = module.subscriptions[key];
  341. ftui.subscriptionTs[key + '-ts'] = module.subscriptions[key];
  342. var d = ftui.subscriptions[key].device;
  343. if (ftui.devs.indexOf(d) < 0) {
  344. ftui.devs.push(d);
  345. }
  346. var r = ftui.subscriptions[key].reading;
  347. if (ftui.reads.indexOf(r) < 0) {
  348. ftui.reads.push(r);
  349. }
  350. }
  351. }
  352. // build filter
  353. var devicelist = (ftui.devs.length > 0) ? $.map(ftui.devs, $.trim).join() : '.*';
  354. var readinglist = (ftui.reads.length > 0) ? $.map(ftui.reads, $.trim).join(' ') : '';
  355. if (!ftui.config.longPollFilter) {
  356. ftui.poll.long.filter = devicelist + ', ' + readinglist;
  357. } else {
  358. ftui.poll.long.filter = ftui.config.longPollFilter;
  359. }
  360. if (!ftui.config.shortPollFilter) {
  361. ftui.poll.short.filter = devicelist + ' ' + readinglist;
  362. } else {
  363. ftui.poll.short.filter = ftui.config.shortPollFilter;
  364. }
  365. // force shortpoll
  366. ftui.states.lastShortpoll = 0;
  367. },
  368. load: function (name, area) {
  369. ftui.log(1, 'Load plugin "' + name + '" for area "' + area + '"');
  370. return ftui.loadPlugin(name, area);
  371. },
  372. reinit: function () {
  373. $.each(this.modules, function (index, module) {
  374. // Iterate each module and run update function if module is available
  375. if (typeof module === 'object') {
  376. module.reinit();
  377. }
  378. });
  379. },
  380. update: function (dev, par) {
  381. $.each(this.modules, function (index, module) {
  382. // Iterate each module and run update function if module is available
  383. if (typeof module === 'object') {
  384. module.update(dev, par);
  385. }
  386. });
  387. // update data-bind elements
  388. ftui.updateBindElements('ftui.deviceStates');
  389. ftui.log(1, 'call "plugins.update" done for "' + dev + ':' + par + '"');
  390. }
  391. };
  392. // -------- FTUI ----------
  393. var ftui = {
  394. version: '2.7.2',
  395. config: {
  396. DEBUG: false,
  397. DEMO: false,
  398. ICONDEMO: false,
  399. dir: '',
  400. filename: '',
  401. basedir: '',
  402. fhemDir: '',
  403. debuglevel: 0,
  404. doLongPoll: true,
  405. lang: 'de',
  406. toastPosition: 'bottom-left',
  407. shortpollInterval: 0,
  408. styleCollection: {},
  409. stdColors: ["green", "orange", "red", "ligthblue", "blue", "gray", "white", "mint"]
  410. },
  411. poll: {
  412. short: {
  413. lastTimestamp: new Date(),
  414. timer: null,
  415. request: null,
  416. result: null,
  417. lastErrorToast: null
  418. },
  419. long: {
  420. xhr: null,
  421. currLine: 0,
  422. lastUpdateTimestamp: new Date(),
  423. lastEventTimestamp: new Date(),
  424. timer: null,
  425. result: null,
  426. lastErrorToast: null
  427. }
  428. },
  429. states: {
  430. width: 0,
  431. lastSetOnline: 0,
  432. lastShortpoll: 0,
  433. longPollRestart: false,
  434. inits: []
  435. },
  436. deviceStates: {},
  437. paramIdMap: {},
  438. timestampMap: {},
  439. subscriptions: {},
  440. subscriptionTs: {},
  441. scripts: [],
  442. gridster: {
  443. instances: {},
  444. instance: null,
  445. baseX: 0,
  446. baseY: 0,
  447. margins: 5,
  448. mincols: 0,
  449. cols: 0,
  450. rows: 0
  451. },
  452. init: function () {
  453. ftui.hideWidgets();
  454. ftui.paramIdMap = {};
  455. ftui.timestampMap = {};
  456. ftui.config.longPollType = $("meta[name='longpoll_type']").attr("content") || 'websocket';
  457. var longpoll = $("meta[name='longpoll']").attr("content") || '1';
  458. ftui.config.doLongPoll = (longpoll != '0');
  459. ftui.config.shortPollFilter = $("meta[name='shortpoll_filter']").attr("content");
  460. ftui.config.longPollFilter = $("meta[name='longpoll_filter']").attr("content");
  461. ftui.config.DEMO = ($("meta[name='demo']").attr("content") == '1');
  462. ftui.config.ICONDEMO = ($("meta[name='icondemo']").attr("content") == '1');
  463. ftui.config.debuglevel = $("meta[name='debug']").attr("content") || 0;
  464. ftui.config.fadeTime = $("meta[name='fade_time']").attr("content") || 200;
  465. if (ftui.config.fadeTime === '0') {
  466. ftui.log(1, 'fadeTime=0 => disable jQuery animation');
  467. jQuery.fx.off = true;
  468. }
  469. ftui.config.webDevice = $("meta[name='web_device']").attr("content") || $.trim($('body').data('webname')) || 'WEB';
  470. ftui.config.maxLongpollAge = $("meta[name='longpoll_maxage']").attr("content") || 240;
  471. ftui.config.DEBUG = (ftui.config.debuglevel > 0);
  472. ftui.config.TOAST = $("meta[name='toast']").attr("content") || 5; //1,2,3...= n Toast-Messages, 0: No Toast-Messages
  473. ftui.config.toastPosition = $("meta[name='toast_position']").attr("content") || 'bottom-left';
  474. ftui.config.shortpollInterval = $("meta[name='shortpoll_only_interval']").attr("content") || 30;
  475. ftui.config.shortPollDelay = $("meta[name='shortpoll_restart_delay']").attr("content") || 3000;
  476. //self path
  477. var url = window.location.pathname;
  478. ftui.config.filename = url.substring(url.lastIndexOf('/') + 1);
  479. ftui.log(1, 'Filename: ' + ftui.config.filename);
  480. var fhemUrl = $("meta[name='fhemweb_url']").attr("content");
  481. ftui.config.fhemDir = fhemUrl || location.origin + "/fhem/";
  482. if (fhemUrl && new RegExp("^((?!http:\/\/|https:\/\/).)*$").test(fhemUrl)) {
  483. ftui.config.fhemDir = location.origin + "/" + fhemUrl + "/";
  484. }
  485. ftui.config.fhemDir = ftui.config.fhemDir.replace('///', '//');
  486. ftui.log(1, 'FHEM dir: ' + ftui.config.fhemDir);
  487. // lang
  488. var userLang = navigator.language || navigator.userLanguage;
  489. ftui.config.lang = $("meta[name='lang']").attr("content") || ((ftui.isValid(userLang)) ? userLang.split('-')[0] : 'de');
  490. // credentials
  491. ftui.config.username = $("meta[name='username']").attr("content");
  492. ftui.config.password = $("meta[name='password']").attr("content");
  493. // subscriptions
  494. ftui.devs = [ftui.config.webDevice];
  495. ftui.reads = ['STATE'];
  496. // Get CSFS Token
  497. ftui.getCSrf();
  498. // init Toast
  499. function configureToast() {
  500. if ($.toast && !$('link[href$="lib/jquery.toast.min.css"]').length)
  501. $('head').append('<link rel="stylesheet" href="' + ftui.config.basedir +
  502. 'lib/jquery.toast.min.css" type="text/css" />');
  503. }
  504. if (!$.fn.toast) {
  505. ftui.dynamicload(ftui.config.basedir + "lib/jquery.toast.min.js", false).done(function () {
  506. configureToast();
  507. });
  508. } else {
  509. configureToast();
  510. }
  511. // after the page became visible, check server connection
  512. $(document).on('visibilitychange', function () {
  513. if (document.visibilityState === 'hidden') {
  514. // page is hidden
  515. } else {
  516. // page is visible
  517. ftui.log(1, 'Page became visible again -> start healthCheck in 3 secondes ');
  518. setTimeout(function () {
  519. ftui.healthCheck();
  520. }, 3000);
  521. }
  522. });
  523. try {
  524. // try to use localStorage
  525. localStorage.setItem('ftui_version', ftui.version);
  526. localStorage.removeItem('ftui_version');
  527. } catch (e) {
  528. // there was an error so...
  529. ftui.toast('You are in Privacy Mode<br>Please deactivate Privacy Mode and then reload the page.', 'error');
  530. }
  531. // detect clickEventType
  532. var android = ftui.getAndroidVersion();
  533. var iOS = /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream;
  534. var onlyTouch = ((android && parseFloat(android) < 5) || iOS);
  535. ftui.config.clickEventType = (onlyTouch) ? 'touchstart' : 'touchstart mousedown';
  536. ftui.config.moveEventType = ((onlyTouch) ? 'touchmove' : 'touchmove mousemove');
  537. ftui.config.releaseEventType = ((onlyTouch) ? 'touchend' : 'touchend mouseup');
  538. ftui.config.leaveEventType = ((onlyTouch) ? 'touchleave' : 'touchleave mouseout');
  539. ftui.config.enterEventType = ((onlyTouch) ? 'touchenter' : 'touchenter mouseenter');
  540. // add background for modal dialogs
  541. $("<div id='shade' />").prependTo('body').hide();
  542. $('#shade').on(ftui.config.clickEventType, function (e) {
  543. $(document).trigger("shadeClicked");
  544. });
  545. //remove old storages
  546. var arr = [];
  547. for (var i = 0; i < localStorage.length; i++) {
  548. if (localStorage.key(i).match(/[0-9abcdef]{4}-[0-9abcdef]{4}-[0-9abcdef]{4}/)) {
  549. arr.push(localStorage.key(i));
  550. }
  551. }
  552. for (i = 0; i < arr.length; i++) {
  553. localStorage.removeItem(arr[i]);
  554. }
  555. ftui.readStatesLocal();
  556. // init FTUI CSS if not already loaded
  557. if ($('link[href$="css/fhem-tablet-ui.css"]').length === 0 &&
  558. $('link[href$="css/fhem-tablet-ui.min.css"]').length === 0 &&
  559. !f7) {
  560. var cssUrl = ftui.config.basedir + 'css/fhem-tablet-ui.css';
  561. $.when($.get(cssUrl, function () {
  562. $('<link>', {
  563. rel: 'stylesheet',
  564. type: 'text/css',
  565. 'href': cssUrl
  566. }).prependTo('head');
  567. })).then(function () {
  568. var ii = 0;
  569. var cssListener = setInterval(function () {
  570. ftui.log(1, 'fhem-tablet-ui.css dynamically loaded. Waiting until it is ready to use...');
  571. if ($("body").css("text-align") === "center") {
  572. ftui.log(1, 'fhem-tablet-ui.css is ready to use.');
  573. clearInterval(cssListener);
  574. ftui.loadStyleSchema();
  575. ftui.initPage();
  576. }
  577. ii++;
  578. if (ii > 120) {
  579. clearInterval(cssListener);
  580. ftui.toast("fhem-tablet-ui.css not ready to use", 'error');
  581. }
  582. }, 50);
  583. });
  584. } else {
  585. ftui.loadStyleSchema();
  586. ftui.initPage();
  587. }
  588. $(document).on("changedSelection", function () {
  589. $(
  590. '.gridster li > header ~ .hbox:only-of-type, ' +
  591. '.dialog > header ~ .hbox:first-of-type:nth-last-of-type(1), ' +
  592. '.gridster li > header ~ .center:not([data-type]):only-of-type, ' +
  593. '.card > header ~ div:not([data-type]):only-of-type, ' +
  594. '.gridster li header ~ div:first-of-type:nth-last-of-type(1)'
  595. ).each(function (index) {
  596. var heightHeader = $(this).siblings('header').outerHeight();
  597. if (heightHeader > 0) {
  598. $(this).css({
  599. 'height': 'calc(100% - ' + $(this).siblings('header').outerHeight() + 'px)'
  600. });
  601. }
  602. });
  603. });
  604. $(document).on("initWidgetsDone", function () {
  605. // restart longpoll
  606. ftui.states.longPollRestart = true;
  607. ftui.restartLongPoll();
  608. ftui.initHeaderLinks();
  609. ftui.saveStatesLocal("subscriptions");
  610. ftui.saveStatesLocal("modules");
  611. // calculate full line height
  612. $(".line-full").each(function () {
  613. $(this).css({
  614. 'line-height': $(this).parent().height() + 'px'
  615. });
  616. });
  617. // start shortpoll delayed
  618. ftui.startShortPollInterval(500);
  619. // trigger refreshs
  620. $(document).trigger('changedSelection');
  621. if (!ftui.config.ICONDEMO) {
  622. ftui.disableSelection();
  623. }
  624. });
  625. if (!f7) {
  626. // dont show focus frame
  627. $("*:not(select):not(textarea)").focus(function () {
  628. $(this).blur();
  629. });
  630. }
  631. ftui.saveStatesLocal("version");
  632. ftui.saveStatesLocal("config");
  633. setInterval(function () {
  634. ftui.healthCheck();
  635. }, 60000);
  636. },
  637. initGridster: function (area) {
  638. ftui.gridster.minX = parseInt($("meta[name='widget_min_width'],meta[name='gridster_min_width']").attr("content") || 0);
  639. ftui.gridster.minY = parseInt($("meta[name='widget_min_height'],meta[name='gridster_min_height']").attr("content") || 0);
  640. ftui.gridster.baseX = parseInt($("meta[name='widget_base_width'],meta[name='gridster_base_width']").attr("content") || 0);
  641. ftui.gridster.baseY = parseInt($("meta[name='widget_base_height'],meta[name='gridster_base_height']").attr("content") || 0);
  642. ftui.gridster.cols = parseInt($("meta[name='gridster_cols']").attr("content") || 0);
  643. ftui.gridster.rows = parseInt($("meta[name='gridster_rows']").attr("content") || 0);
  644. ftui.gridster.resize = parseInt($("meta[name='gridster_resize']").attr("content") || (ftui.gridster.baseX + ftui.gridster.baseY) >
  645. 0 ? 0 : 1);
  646. if ($("meta[name='widget_margin'],meta[name='gridster_margin']").attr("content"))
  647. ftui.gridster.margins = parseInt($("meta[name='widget_margin'],meta[name='gridster_margin']").attr("content"));
  648. function configureGridster() {
  649. var highestCol = -1;
  650. var highestRow = -1;
  651. var baseX = 0;
  652. var baseY = 0;
  653. var cols = 0;
  654. var rows = 0;
  655. $(".gridster > ul > li").each(function () {
  656. var colVal = $(this).data("col") + $(this).data("sizex") - 1;
  657. if (colVal > highestCol)
  658. highestCol = colVal;
  659. var rowVal = $(this).data("row") + $(this).data("sizey") - 1;
  660. if (rowVal > highestRow)
  661. highestRow = rowVal;
  662. });
  663. //console.log(ftui.gridster.cols, ftui.gridster.rows, ftui.gridster.baseX, ftui.gridster.baseY);
  664. cols = (ftui.gridster.cols > 0) ? ftui.gridster.cols : highestCol;
  665. rows = (ftui.gridster.rows > 0) ? ftui.gridster.rows : highestRow;
  666. var colMargins = 2 * cols * ftui.gridster.margins;
  667. var rowMargins = 2 * rows * ftui.gridster.margins;
  668. baseX = (ftui.gridster.baseX > 0) ? ftui.gridster.baseX : (window.innerWidth - colMargins) / cols;
  669. baseY = (ftui.gridster.baseY > 0) ? ftui.gridster.baseY : (window.innerHeight - rowMargins) / rows;
  670. if (baseX < ftui.gridster.minX) {
  671. baseX = ftui.gridster.minX;
  672. }
  673. if (baseY < ftui.gridster.minY) {
  674. baseY = ftui.gridster.minY;
  675. }
  676. ftui.gridster.mincols = parseInt($("meta[name='widget_min_cols'],meta[name='gridster_min_cols']").attr("content") ||
  677. cols);
  678. if (ftui.gridster.instances[area])
  679. ftui.gridster.instances[area].destroy();
  680. ftui.gridster.instances[area] = $(".gridster > ul", area).gridster({
  681. widget_base_dimensions: [baseX, baseY],
  682. widget_margins: [ftui.gridster.margins, ftui.gridster.margins],
  683. draggable: {
  684. handle: '.gridster li > header'
  685. },
  686. min_cols: parseInt(ftui.gridster.mincols),
  687. }).data('gridster');
  688. if (ftui.gridster.instances[area]) {
  689. if ($("meta[name='gridster_disable']").attr("content") == '1') {
  690. ftui.gridster.instances[area].disable();
  691. }
  692. if ($("meta[name='gridster_starthidden']").attr("content") == '1') {
  693. $('.gridster').hide();
  694. }
  695. }
  696. // corrections for gridster in gridster element
  697. $('.gridster > ul > li:has(* .gridster)').each(function () {
  698. var gridgrid = $(this);
  699. gridgrid.css({
  700. 'background-color': 'transparent',
  701. 'margin': '-' + ftui.gridster.margins + 'px',
  702. // 'width': gridgrid.parent().width() - gridgrid.position().left,
  703. // 'height': '100%'
  704. });
  705. });
  706. $('.gridster > ul > li >.center', area).parent().addClass('has_center');
  707. // max height for inner boxes
  708. $('.gridster > ul > li > .vbox', area).parent().addClass('has_vbox');
  709. }
  710. if ($('.gridster', area).length > 0) {
  711. if (!$('link[href$="lib/jquery.gridster.min.css"]').length)
  712. $('head').append('<link rel="stylesheet" href="' + ftui.config.basedir +
  713. 'lib/jquery.gridster.min.css" type="text/css" />');
  714. if (!$.fn.gridster) {
  715. ftui.dynamicload(ftui.config.basedir + "lib/jquery.gridster.min.js", false).done(function () {
  716. configureGridster();
  717. });
  718. } else {
  719. configureGridster();
  720. }
  721. if (ftui.gridster.resize) {
  722. $(window).on('resize', function () {
  723. if (ftui.states.width !== window.innerWidth) {
  724. clearTimeout(ftui.states.delayResize);
  725. ftui.states.delayResize = setTimeout(configureGridster, 500);
  726. ftui.states.width = window.innerWidth;
  727. }
  728. });
  729. }
  730. }
  731. },
  732. initPage: function (area) {
  733. // hideWidgets
  734. ftui.hideWidgets(area);
  735. // init gridster
  736. area = (ftui.isValid(area)) ? area : 'html';
  737. console.time('initPage-' + area);
  738. ftui.states.startTime = new Date();
  739. ftui.log(2, 'initPage - area=' + area);
  740. ftui.initGridster(area);
  741. // include extern html code
  742. var deferredArr = $.map($('[data-template]', area), function (templ, i) {
  743. var templElem = $(templ);
  744. return $.get(
  745. templElem.data('template'), {},
  746. function (data) {
  747. var parValues = templElem.data('parameter');
  748. for (var key in parValues) {
  749. data = data.replace(new RegExp(key, 'g'), parValues[key]);
  750. }
  751. templElem.html(data);
  752. }
  753. );
  754. });
  755. // get current values of readings not before all widgets are loaded
  756. $.when.apply(this, deferredArr).then(function () {
  757. //continue after loading the includes
  758. ftui.log(1, 'init templates - Done');
  759. ftui.initWidgets(area).done(function () {
  760. console.timeEnd('initPage-' + area);
  761. var dur = 'initPage (' + area + '): in ' + (new Date() - ftui.states.startTime) + 'ms';
  762. if (ftui.config.debuglevel > 1) ftui.toast(dur);
  763. ftui.log(1, dur);
  764. });
  765. });
  766. },
  767. initWidgets: function (area) {
  768. var defer = new $.Deferred();
  769. area = (ftui.isValid(area)) ? area : 'html';
  770. var types = [];
  771. ftui.log(3, plugins);
  772. plugins.removeArea(area);
  773. ftui.log(3, plugins);
  774. ftui.log(2, 'initWidgets - area=' + area);
  775. // collect required widgets types
  776. $('[data-type] ', area).each(function (index) {
  777. var type = $(this).data("type");
  778. if (types.indexOf(type) < 0) {
  779. types.push(type);
  780. }
  781. });
  782. // init widgets
  783. var deferredArr = $.map(types, function (type, i) {
  784. return plugins.load(type, area);
  785. });
  786. // get current values of readings not before all widgets are loaded
  787. $.when.apply(this, deferredArr).then(function () {
  788. plugins.updateParameters();
  789. ftui.log(1, 'initWidgets - Done');
  790. $(document).trigger("initWidgetsDone", [area]);
  791. defer.resolve();
  792. });
  793. return defer.promise();
  794. },
  795. initHeaderLinks: function () {
  796. if (($('[class*=fa-]').length > 0 ||
  797. $('[data-type="select"]').length > 0 ||
  798. $('[data-type="homestatus"]').length > 0) &&
  799. !$('link[href$="lib/font-awesome.min.css"]').length
  800. )
  801. $('head').append('<link rel="stylesheet" href="' + ftui.config.basedir + 'lib/font-awesome.min.css" type="text/css" />');
  802. if ($('[class*=oa-]').length > 0 && !$('link[href$="lib/openautomation.css"]').length)
  803. $('head').append('<link rel="stylesheet" href="' + ftui.config.basedir + 'lib/openautomation.css" type="text/css" />');
  804. if ($('[class*=fs-]').length > 0 && !$('link[href$="lib/fhemSVG.css"]').length)
  805. $('head').append('<link rel="stylesheet" href="' + ftui.config.basedir + 'lib/fhemSVG.css" type="text/css" />');
  806. if ($('[class*=mi-]').length > 0 && !$('link[href$="lib/material-icons.min.css"]').length)
  807. $('head').append('<link rel="stylesheet" href="' + ftui.config.basedir +
  808. 'lib/material-icons.min.css" type="text/css" />');
  809. if ($('[class*=wi-]').length > 0 && !$('link[href$="lib/weather-icons.min.css"]').length)
  810. $('head').append('<link rel="stylesheet" href="' + ftui.config.basedir +
  811. 'lib/weather-icons.min.css" type="text/css" />');
  812. if ($('[class*=wi-wind]').length > 0 && !$('link[href$="lib/weather-icons-wind.min.css"]').length)
  813. $('head').append('<link rel="stylesheet" href="' + ftui.config.basedir +
  814. 'lib/weather-icons-wind.min.css" type="text/css" />');
  815. },
  816. startLongpoll: function () {
  817. ftui.log(2, 'startLongpoll: ' + ftui.config.doLongPoll);
  818. ftui.poll.long.lastEventTimestamp = new Date();
  819. if (ftui.config.doLongPoll) {
  820. ftui.config.shortpollInterval = $("meta[name='shortpoll_interval']").attr("content") || 15 * 60; // 15 minutes
  821. ftui.poll.long.timer = setTimeout(function () {
  822. ftui.longPoll();
  823. }, 0);
  824. }
  825. },
  826. stopLongpoll: function () {
  827. ftui.log(2, 'stopLongpoll');
  828. clearInterval(ftui.poll.long.timer);
  829. if (ftui.poll.long.request)
  830. ftui.poll.long.request.abort();
  831. if (ftui.poll.long.websocket) {
  832. //ftui.poll.long.websocket.send('bye');
  833. ftui.poll.long.websocket.close();
  834. ftui.poll.long.websocket = undefined;
  835. ftui.log(2, 'stopped websocket');
  836. }
  837. },
  838. restartLongPoll: function (msg, error) {
  839. ftui.log(2, 'restartLongpoll');
  840. var delay;
  841. clearTimeout(ftui.poll.long.timer);
  842. if (msg) {
  843. ftui.toast("Disconnected from FHEM<br>" + msg, error);
  844. }
  845. ftui.stopLongpoll();
  846. if (ftui.states.longPollRestart) {
  847. delay = 0;
  848. } else {
  849. ftui.toast("Retry to connect in 10 seconds");
  850. delay = 10000;
  851. }
  852. ftui.poll.long.timer = setTimeout(function () {
  853. ftui.startLongpoll();
  854. }, delay);
  855. },
  856. forceRefresh: function () {
  857. ftui.states.lastShortpoll = 0;
  858. ftui.shortPoll();
  859. },
  860. startShortPollInterval: function (delay) {
  861. ftui.log(1, 'shortpoll: start in (ms):' + (delay || ftui.config.shortpollInterval * 1000));
  862. clearInterval(ftui.poll.short.timer);
  863. ftui.poll.short.timer = setTimeout(function () {
  864. // get current values of readings every x seconds
  865. ftui.shortPoll();
  866. ftui.startShortPollInterval();
  867. }, (delay || ftui.config.shortpollInterval * 1000));
  868. },
  869. shortPoll: function (silent) {
  870. var ltime = new Date().getTime() / 1000;
  871. if ((ltime - ftui.states.lastShortpoll) < ftui.config.shortpollInterval)
  872. return;
  873. ftui.log(1, 'start shortpoll');
  874. var startTime = new Date();
  875. // invalidate all readings for detection of outdated ones
  876. for (var i = 0, len = ftui.devs.length; i < len; i++) {
  877. var params = ftui.deviceStates[ftui.devs[i]];
  878. for (var reading in params) {
  879. params[reading].valid = false;
  880. }
  881. }
  882. console.time('get jsonlist2');
  883. ftui.poll.short.request =
  884. ftui.sendFhemCommand('jsonlist2 ' + ftui.poll.short.filter)
  885. .done(function (fhemJSON) {
  886. console.timeEnd('get jsonlist2');
  887. console.time('read jsonlist2');
  888. ftui.log(3, 'fhemJSON: 0=' + Object.keys(fhemJSON)[0] + ' 1=' + Object.keys(fhemJSON)[1]);
  889. // function to import data
  890. function checkReading(device, section) {
  891. for (var reading in section) {
  892. var isUpdated = false;
  893. var paramid = (reading === 'STATE') ? device : [device, reading].join('-');
  894. var newParam = section[reading];
  895. if (typeof newParam !== 'object') {
  896. //ftui.log(5,'paramid='+paramid+' newParam='+newParam);
  897. newParam = {
  898. "Value": newParam,
  899. "Time": ""
  900. };
  901. }
  902. // is there a subscription, then check and update widgets
  903. if (ftui.subscriptions[paramid]) {
  904. var oldParam = ftui.getDeviceParameter(device, reading);
  905. isUpdated = (!oldParam || oldParam.val !== newParam.Value || oldParam.date !== newParam.Time);
  906. ftui.log(5, 'isUpdated=' + isUpdated);
  907. }
  908. // write into internal cache object
  909. var params = ftui.deviceStates[device] || {};
  910. var param = params[reading] || {};
  911. param.date = newParam.Time;
  912. param.val = newParam.Value;
  913. // console.log('*****',device);
  914. param.valid = true;
  915. params[reading] = param;
  916. ftui.deviceStates[device] = params;
  917. ftui.paramIdMap[paramid] = {};
  918. ftui.paramIdMap[paramid].device = device;
  919. ftui.paramIdMap[paramid].reading = reading;
  920. ftui.timestampMap[paramid + '-ts'] = {};
  921. ftui.timestampMap[paramid + '-ts'].device = device;
  922. ftui.timestampMap[paramid + '-ts'].reading = reading;
  923. // update widgets only if necessary
  924. if (isUpdated) {
  925. ftui.log(5, '[shortPoll] do update for ' + device + ',' + reading);
  926. plugins.update(device, reading);
  927. }
  928. }
  929. }
  930. // import the whole fhemJSON
  931. if (fhemJSON && fhemJSON.Results) {
  932. var len = fhemJSON.Results.length;
  933. ftui.log(2, 'shortpoll: fhemJSON.Results.length=' + len);
  934. var results = fhemJSON.Results;
  935. for (var i = len - 1; i >= 0; i -= 1) {
  936. var res = results[i];
  937. var devName = res.Name;
  938. if (devName.indexOf('FHEMWEB') < 0 && !devName.match(/WEB_\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}_\d{5}/)) {
  939. checkReading(devName, res.Internals);
  940. checkReading(devName, res.Attributes);
  941. checkReading(devName, res.Readings);
  942. }
  943. }
  944. // finished
  945. var duration = ftui.diffSeconds(startTime, new Date());
  946. if (ftui.config.debuglevel > 1) {
  947. var paramCount = Object.keys(ftui.paramIdMap).length;
  948. ftui.toast("Full refresh done in " +
  949. duration + "s for " +
  950. paramCount + " parameter(s)");
  951. }
  952. ftui.log(1, 'shortPoll: Done');
  953. if (ftui.poll.short.lastErrorToast) { ftui.poll.short.lastErrorToast.reset(); }
  954. ftui.states.lastShortpoll = ltime;
  955. ftui.poll.short.duration = duration * 1000;
  956. ftui.poll.short.lastTimestamp = new Date();
  957. ftui.poll.short.result = 'ok';
  958. ftui.saveStatesLocal("deviceStates");
  959. ftui.saveStatesLocal("shortPoll");
  960. ftui.updateBindElements('ftui.');
  961. if (!silent) {
  962. ftui.onUpdateDone();
  963. }
  964. } else {
  965. var err = "request failed: Result is null";
  966. ftui.log(1, "shortPoll: " + err);
  967. ftui.poll.short.result = err;
  968. ftui.toast("<u>ShortPoll " + err + " </u><br>", 'error');
  969. ftui.saveStatesLocal("shortPoll");
  970. }
  971. console.timeEnd('read jsonlist2');
  972. })
  973. .fail(function (jqxhr, textStatus, error) {
  974. var err = textStatus + ", " + error;
  975. ftui.log(1, "shortPoll: request failed: " + err);
  976. ftui.poll.short.result = err;
  977. ftui.states.lastSetOnline = 0;
  978. ftui.states.lastShortpoll = 0;
  979. ftui.saveStatesLocal("deviceStates");
  980. ftui.saveStatesLocal("shortPoll");
  981. if (textStatus.indexOf('parsererror') < 0) {
  982. ftui.poll.short.lastErrorToast = ftui.toast("<u>ShortPoll Request Failed, will retry in " + ftui.config.shortPollDelay / 1000 + "s</u><br>" +
  983. err, 'error');
  984. ftui.getCSrf();
  985. ftui.startShortPollInterval(ftui.config.shortPollDelay);
  986. } else {
  987. ftui.toast("<u>ShortPoll Request Failed</u><br>" + err, 'error');
  988. }
  989. });
  990. },
  991. longPoll: function () {
  992. if (ftui.config.DEMO) {
  993. console.log('DEMO-Mode: no longpoll');
  994. return;
  995. }
  996. if ('WebSocket' in window &&
  997. ftui.config.longPollType === 'websocket' &&
  998. ftui.deviceStates[ftui.config.webDevice] &&
  999. ftui.deviceStates[ftui.config.webDevice].longpoll &&
  1000. ftui.deviceStates[ftui.config.webDevice].longpoll.val &&
  1001. ftui.deviceStates[ftui.config.webDevice].longpoll.val === 'websocket') {
  1002. if (ftui.poll.long.websocket) {
  1003. ftui.log(3, 'valid ftui.poll.long.websocket found');
  1004. return;
  1005. }
  1006. if (ftui.poll.long.lastErrorToast) { ftui.poll.long.lastErrorToast.reset(); }
  1007. if (ftui.config.debuglevel > 1) {
  1008. ftui.toast("Longpoll (WebSocket) started");
  1009. }
  1010. ftui.poll.long.URL = ftui.config.fhemDir.replace(/^http/i, "ws") + "?XHR=1&inform=type=status;filter=" +
  1011. ftui.poll.long.filter + ";since=" + ftui.poll.long.lastEventTimestamp.getTime() + ";fmt=JSON" +
  1012. "&timestamp=" + new Date().getTime();
  1013. //"&fwcsrf=" + ftui.config.csrf;
  1014. ftui.log(1, 'websockets URL=' + ftui.poll.long.URL);
  1015. ftui.states.longPollRestart = false;
  1016. ftui.poll.long.websocket = new WebSocket(ftui.poll.long.URL);
  1017. ftui.poll.long.websocket.onclose = function (event) {
  1018. var reason;
  1019. if (event.code == 1000)
  1020. reason =
  1021. "Normal closure, meaning that the purpose for which the connection was established has been fulfilled.";
  1022. else if (event.code == 1001)
  1023. reason =
  1024. "An endpoint is \"going away\", such as a server going down or a browser having navigated away from a page.";
  1025. else if (event.code == 1002)
  1026. reason = "An endpoint is terminating the connection due to a protocol error";
  1027. else if (event.code == 1003)
  1028. reason =
  1029. "An endpoint is terminating the connection because it has received a type of data it cannot accept (e.g., an endpoint that understands only text data MAY send this if it receives a binary message).";
  1030. else if (event.code == 1004)
  1031. reason = "Reserved. The specific meaning might be defined in the future.";
  1032. else if (event.code == 1005)
  1033. reason = "No status code was actually present.";
  1034. else if (event.code == 1006)
  1035. reason = "The connection was closed abnormally, e.g., without sending or receiving a Close control frame";
  1036. else if (event.code == 1007)
  1037. reason =
  1038. "An endpoint is terminating the connection because it has received data within a message that was not consistent with the type of the message (e.g., non-UTF-8 [http://tools.ietf.org/html/rfc3629] data within a text message).";
  1039. else if (event.code == 1008)
  1040. reason =
  1041. "An endpoint is terminating the connection because it has received a message that \"violates its policy\". This reason is given either if there is no other sutible reason, or if there is a need to hide specific details about the policy.";
  1042. else if (event.code == 1009)
  1043. reason =
  1044. "An endpoint is terminating the connection because it has received a message that is too big for it to process.";
  1045. else if (event.code == 1010) // Note that this status code is not used by the server, because it can fail the WebSocket handshake instead.
  1046. reason =
  1047. "An endpoint (client) is terminating the connection because it has expected the server to negotiate one or more extension, but the server didn't return them in the response message of the WebSocket handshake. <br /> Specifically, the extensions that are needed are: " +
  1048. event.reason;
  1049. else if (event.code == 1011)
  1050. reason =
  1051. "A server is terminating the connection because it encountered an unexpected condition that prevented it from fulfilling the request.";
  1052. else if (event.code == 1015)
  1053. reason =
  1054. "The connection was closed due to a failure to perform a TLS handshake (e.g., the server certificate can't be verified).";
  1055. else
  1056. reason = "Unknown reason";
  1057. ftui.log(1, "websocket (url=" + event.target.url + ") closed! reason=" + reason);
  1058. // if current socket closes then restart websocket
  1059. if (event.target.url === ftui.poll.long.URL) {
  1060. ftui.restartLongPoll(reason);
  1061. }
  1062. };
  1063. ftui.poll.long.websocket.onerror = function (event) {
  1064. ftui.log(1, "Error while longpoll: " + event.data);
  1065. if (ftui.config.debuglevel > 1 && event.target.url === ftui.poll.long.URL) {
  1066. ftui.poll.long.lastErrorToast = ftui.toast("Error while longpoll (websocket)", 'error');
  1067. }
  1068. if (ftui.config.DEBUG) ftui.saveStatesLocal("longPoll");
  1069. };
  1070. ftui.poll.long.websocket.onmessage = function (msg) {
  1071. ftui.handleUpdates(msg.data);
  1072. };
  1073. } else {
  1074. ftui.log(1, 'longpoll: websockets not supportetd or not activated > fall back to AJAX');
  1075. if (ftui.poll.long.xhr) {
  1076. ftui.log(3, 'longpoll: valid ftui.poll.long.xhr found');
  1077. return;
  1078. }
  1079. if (ftui.poll.long.request) {
  1080. ftui.log(3, 'longpoll: valid ftui.poll.long.request found');
  1081. return;
  1082. }
  1083. ftui.poll.long.currLine = 0;
  1084. if (ftui.poll.long.lastErrorToast) { ftui.poll.long.lastErrorToast.reset(); }
  1085. if (ftui.config.DEBUG) {
  1086. if (ftui.states.longPollRestart)
  1087. ftui.toast("Longpoll (AJAX) re-started");
  1088. else
  1089. ftui.toast("Longpoll (AJAX) started");
  1090. }
  1091. ftui.log(1, (ftui.states.longPollRestart) ? "longpoll: re-started" : "longpoll: started");
  1092. ftui.states.longPollRestart = false;
  1093. ftui.poll.long.request = $.ajax({
  1094. url: ftui.config.fhemDir,
  1095. cache: false,
  1096. async: true,
  1097. method: 'GET',
  1098. data: {
  1099. XHR: 1,
  1100. inform: "type=status;filter=" + ftui.poll.long.filter + ";since=" +
  1101. ftui.poll.long.lastEventTimestamp.getTime() + ";fmt=JSON",
  1102. fwcsrf: ftui.config.csrf
  1103. },
  1104. username: ftui.config.username,
  1105. password: ftui.config.password,
  1106. xhr: function () {
  1107. ftui.poll.long.xhr = new window.XMLHttpRequest();
  1108. ftui.poll.long.xhr.addEventListener("readystatechange", function (e) {
  1109. var data = e.target.responseText;
  1110. if (e.target.readyState === 4) {
  1111. return;
  1112. }
  1113. if (e.target.readyState === 3) {
  1114. ftui.handleUpdates(data);
  1115. }
  1116. }, false);
  1117. ftui.log(1, 'longpoll: ajax responseURL=' + ftui.poll.long.xhr.responseURL);
  1118. ftui.log(1, 'longpoll: ajax statusText=' + ftui.poll.long.xhr.statusText);
  1119. return ftui.poll.long.xhr;
  1120. }
  1121. })
  1122. .done(function (data) {
  1123. if (ftui.poll.long.xhr) {
  1124. ftui.poll.long.xhr.abort();
  1125. ftui.poll.long.xhr = null;
  1126. }
  1127. ftui.poll.long.request = null;
  1128. if (ftui.states.longPollRestart) {
  1129. ftui.longPoll();
  1130. } else {
  1131. ftui.log(1, "longpoll: Disconnected from FHEM - poll done - " + data);
  1132. ftui.restartLongPoll(data);
  1133. }
  1134. })
  1135. .fail(function (jqXHR, textStatus, errorThrown) {
  1136. if (ftui.poll.long.xhr) {
  1137. ftui.poll.long.xhr.abort();
  1138. ftui.poll.long.xhr = null;
  1139. }
  1140. ftui.poll.long.request = null;
  1141. if (ftui.states.longPollRestart) {
  1142. ftui.longPoll();
  1143. } else {
  1144. ftui.log(1, "longpoll: Error - text=" + textStatus + ": " + errorThrown);
  1145. if (ftui.config.debuglevel > 1) {
  1146. ftui.poll.long.lastErrorToast = ftui.toast("Error while longpoll (ajax)<br>" + textStatus + ": " + errorThrown, 'error');
  1147. }
  1148. ftui.restartLongPoll(textStatus + ": " + errorThrown);
  1149. }
  1150. });
  1151. }
  1152. },
  1153. handleUpdates: function (data) {
  1154. var lines = data.split(/\n/);
  1155. for (var i = ftui.poll.long.currLine, len = lines.length; i < len; i++) {
  1156. ftui.log(5, lines[i]);
  1157. ftui.poll.long.lastLine = lines[i];
  1158. var lastChar = lines[i].slice(-1);
  1159. if (ftui.isValid(lines[i]) && lines[i] !== '' && lastChar === "]") {
  1160. try {
  1161. var dataJSON = JSON.parse(lines[i]);
  1162. var params = null;
  1163. var param = null;
  1164. var isSTATE = (dataJSON[1] !== dataJSON[2]);
  1165. var isTrigger = (dataJSON[1] === '' && dataJSON[2] === '');
  1166. ftui.log(4, dataJSON);
  1167. var pmap = ftui.paramIdMap[dataJSON[0]];
  1168. var tmap = ftui.timestampMap[dataJSON[0]];
  1169. var subscription = ftui.subscriptions[dataJSON[0]];
  1170. // update for a parameter
  1171. if (pmap) {
  1172. if (isSTATE) {
  1173. pmap.reading = 'STATE';
  1174. }
  1175. params = ftui.deviceStates[pmap.device] || {};
  1176. param = params[pmap.reading] || {};
  1177. param.val = dataJSON[1];
  1178. param.valid = true;
  1179. params[pmap.reading] = param;
  1180. ftui.deviceStates[pmap.device] = params;
  1181. // dont wait for timestamp for STATE paramters
  1182. if (isSTATE && ftui.subscriptions[dataJSON[0]]) {
  1183. ftui.poll.long.lastDevice = pmap.device;
  1184. ftui.poll.long.lastReading = pmap.reading;
  1185. ftui.poll.long.lastValue = param.val;
  1186. plugins.update(pmap.device, pmap.reading);
  1187. }
  1188. }
  1189. // update for a timestamp
  1190. // STATE updates has no timestamp
  1191. if (tmap && !isSTATE) {
  1192. params = ftui.deviceStates[tmap.device] || {};
  1193. param = params[tmap.reading] || {};
  1194. param.date = dataJSON[1];
  1195. params[tmap.reading] = param;
  1196. ftui.poll.long.lastUpdateTimestamp = param.date.toDate();
  1197. ftui.deviceStates[tmap.device] = params;
  1198. // paramter + timestamp update now completed -> update widgets
  1199. if (ftui.subscriptionTs[dataJSON[0]]) {
  1200. ftui.poll.long.lastDevice = tmap.device;
  1201. ftui.poll.long.lastReading = tmap.reading;
  1202. ftui.poll.long.lastValue = param.val;
  1203. plugins.update(tmap.device, tmap.reading);
  1204. }
  1205. }
  1206. // it is just a trigger
  1207. if (isTrigger && subscription) {
  1208. plugins.update(subscription.device, subscription.reading);
  1209. }
  1210. } catch (err) {
  1211. ftui.poll.long.lastError = err;
  1212. ftui.log(1, "longpoll: Error=" + err);
  1213. ftui.log(1, "longpoll: Bad line=" + lines[i]);
  1214. }
  1215. }
  1216. }
  1217. ftui.poll.long.lastEventTimestamp = new Date();
  1218. if (ftui.config.DEBUG) {
  1219. ftui.saveStatesLocal("longPoll");
  1220. ftui.saveStatesLocal("deviceStates");
  1221. }
  1222. ftui.updateBindElements('ftui.poll');
  1223. if (!ftui.poll.long.websocket) {
  1224. // Ajax longpoll
  1225. // cumulative data -> remember last line
  1226. // restart after 9999 lines to avoid overflow
  1227. ftui.poll.long.currLine = lines.length - 1;
  1228. if (ftui.poll.long.currLine > 9999) {
  1229. ftui.states.longPollRestart = true;
  1230. ftui.poll.long.request.abort();
  1231. }
  1232. }
  1233. },
  1234. setFhemStatus: function (cmdline) {
  1235. if (ftui.config.DEMO) {
  1236. console.log('DEMO-Mode: no setFhemStatus');
  1237. return;
  1238. }
  1239. // postpone update
  1240. ftui.startShortPollInterval();
  1241. ftui.sendFhemCommand(cmdline);
  1242. },
  1243. sendFhemCommand: function (cmdline) {
  1244. cmdline = cmdline.replace(' ', ' ');
  1245. var dataType = (cmdline.substr(0, 8) === 'jsonlist') ? 'json' : 'text';
  1246. ftui.log(1, 'send to FHEM: ' + cmdline);
  1247. return $.ajax({
  1248. async: true,
  1249. cache: false,
  1250. method: 'GET',
  1251. dataType: dataType,
  1252. url: ftui.config.fhemDir,
  1253. username: ftui.config.username,
  1254. password: ftui.config.password,
  1255. data: {
  1256. cmd: cmdline,
  1257. fwcsrf: ftui.config.csrf,
  1258. XHR: "1"
  1259. },
  1260. error: function (jqXHR, textStatus, errorThrown) {
  1261. ftui.toast("<u>FHEM Command failed</u><br>" + textStatus + ": " + errorThrown, 'error');
  1262. }
  1263. });
  1264. },
  1265. loadStyleSchema: function () {
  1266. $.each($('link[href$="-ui.css"],link[href$="-ui.min.css"]'), function (index, thisSheet) {
  1267. if (!thisSheet || !thisSheet.sheet || !thisSheet.sheet.cssRules || thisSheet.getAttribute('disabled')) return;
  1268. var rules = thisSheet.sheet.cssRules;
  1269. for (var r in rules) {
  1270. if (rules[r].style) {
  1271. var styles = rules[r].style.cssText.split(';');
  1272. styles.pop();
  1273. var elmName = rules[r].selectorText;
  1274. var params = {};
  1275. for (var s in styles) {
  1276. var param = styles[s].toString().split(':');
  1277. if (param[0].match(/color/)) {
  1278. params[$.trim(param[0])] = ftui.rgbToHex($.trim(param[1]).replace('! important', '').replace(
  1279. '!important', ''));
  1280. }
  1281. }
  1282. if (Object.keys(params).length > 0)
  1283. ftui.config.styleCollection[elmName] = params;
  1284. }
  1285. }
  1286. });
  1287. },
  1288. onUpdateDone: function () {
  1289. $(document).trigger("updateDone");
  1290. ftui.checkInvalidElements();
  1291. ftui.updateBindElements();
  1292. },
  1293. checkInvalidElements: function () {
  1294. $('.autohide[data-get]').each(function (index) {
  1295. var elem = $(this);
  1296. var valid = elem.getReading('get').valid;
  1297. if (valid && valid === true)
  1298. elem.removeClass('invalid');
  1299. else
  1300. elem.addClass('invalid');
  1301. });
  1302. },
  1303. updateBindElements: function (filter) {
  1304. $('[data-bind*="' + filter + '"]').each(function (index) {
  1305. var elem = $(this);
  1306. var variable = elem.data('bind');
  1307. if (variable) {
  1308. elem.text(eval(variable));
  1309. }
  1310. });
  1311. },
  1312. setOnline: function () {
  1313. var ltime = new Date().getTime() / 1000;
  1314. if ((ltime - ftui.states.lastSetOnline) > 60) {
  1315. if (ftui.config.DEBUG) ftui.toast("FHEM connected");
  1316. ftui.states.lastSetOnline = ltime;
  1317. // force shortpoll
  1318. ftui.states.lastShortpoll = 0;
  1319. ftui.startShortPollInterval(1000);
  1320. if (!ftui.config.doLongPoll) {
  1321. var longpoll = $("meta[name='longpoll']").attr("content") || '1';
  1322. ftui.config.doLongPoll = (longpoll != '0');
  1323. ftui.states.longPollRestart = false;
  1324. if (ftui.config.doLongPoll) {
  1325. ftui.startLongpoll();
  1326. }
  1327. }
  1328. ftui.log(1, 'FTUI is online');
  1329. }
  1330. },
  1331. setOffline: function () {
  1332. if (ftui.config.DEBUG) ftui.toast("Lost connection to FHEM");
  1333. ftui.config.doLongPoll = false;
  1334. ftui.states.longPollRestart = true;
  1335. clearInterval(ftui.poll.short.timer);
  1336. ftui.stopLongpoll();
  1337. ftui.log(1, 'FTUI is offline');
  1338. },
  1339. readStatesLocal: function () {
  1340. if (!ftui.config.DEMO)
  1341. ftui.deviceStates = JSON.parse(localStorage.getItem('ftui.deviceStates')) || {};
  1342. else {
  1343. $.ajax({
  1344. async: false,
  1345. method: 'GET',
  1346. url: "/fhem/tablet/data/" + ftui.config.filename.replace(".html", ".dat"),
  1347. })
  1348. .done(function (data) {
  1349. ftui.deviceStates = JSON.parse(data) || {};
  1350. });
  1351. }
  1352. },
  1353. saveStatesLocal: function (key) {
  1354. //save variables into localStorage
  1355. try {
  1356. switch (key) {
  1357. case "deviceStates":
  1358. localStorage.setItem("ftui.deviceStates", JSON.stringify(ftui.deviceStates));
  1359. break;
  1360. case "shortPoll":
  1361. localStorage.setItem("ftui.poll.short", JSON.stringify(ftui.poll.short));
  1362. break;
  1363. case "longPoll":
  1364. localStorage.setItem("ftui.poll.long", JSON.stringify(ftui.poll.long));
  1365. break;
  1366. case "subscriptions":
  1367. localStorage.setItem("ftui.subscriptions", JSON.stringify(ftui.subscriptions));
  1368. break;
  1369. case "config":
  1370. localStorage.setItem("ftui.config", JSON.stringify(ftui.config));
  1371. break;
  1372. case "version":
  1373. localStorage.setItem("ftui.version", JSON.stringify(ftui.version));
  1374. break;
  1375. case "modules":
  1376. var checkModule = [];
  1377. for (var i = 0, len = plugins.modules.length; i < len; i++) {
  1378. var name = plugins.modules[i].widgetname,
  1379. area = plugins.modules[i].area;
  1380. checkModule.push({
  1381. name: name,
  1382. area: area
  1383. });
  1384. }
  1385. localStorage.setItem("modules", JSON.stringify(checkModule));
  1386. break;
  1387. }
  1388. } catch (e) {}
  1389. },
  1390. getDeviceParameter: function (devname, paraname) {
  1391. if (devname && devname.length > 0) {
  1392. var params = ftui.deviceStates[devname];
  1393. return (params && params[paraname]) ? params[paraname] : null;
  1394. }
  1395. return null;
  1396. },
  1397. loadPlugin: function (name, area) {
  1398. var deferredLoad = new $.Deferred();
  1399. ftui.log(2, 'Start load plugin "' + name + '" for area "' + area + '"');
  1400. //ftui.toast(name);
  1401. // get the plugin
  1402. ftui.dynamicload(ftui.config.basedir + "js/widget_" + name + ".js", true).done(function () {
  1403. // get all dependencies of this plugin
  1404. var depsPromises = [];
  1405. var getDependencies = window["depends_" + name];
  1406. // load all dependencies recursive before
  1407. if ($.isFunction(getDependencies)) {
  1408. var deps = getDependencies();
  1409. if (deps) {
  1410. deps = ($.isArray(deps)) ? deps : [deps];
  1411. $.map(deps, function (dep, i) {
  1412. if (dep.match(new RegExp('^.*\.(js|css)$'))) {
  1413. depsPromises.push(ftui.dynamicload(dep, false));
  1414. } else {
  1415. depsPromises.push(ftui.loadPlugin(dep));
  1416. }
  1417. });
  1418. }
  1419. } else {
  1420. ftui.log(2, "function depends_" + name + " not found (maybe ok)");
  1421. }
  1422. $.when.apply(this, depsPromises).always(function () {
  1423. var module = (window["Modul_" + name]) ? new window["Modul_" + name]() : null;
  1424. if (module) {
  1425. if (area !== void 0) {
  1426. // add only real widgets not dependencies
  1427. plugins.addModule(module);
  1428. if (ftui.isValid(area))
  1429. module.area = area;
  1430. ftui.log(1, 'Try to init plugin: ' + name);
  1431. module.init();
  1432. // update all what we have until now
  1433. for (var key in module.subscriptions) {
  1434. module.update(module.subscriptions[key].device, module.subscriptions[key].reading);
  1435. }
  1436. }
  1437. ftui.log(1, 'Finished load plugin "' + name + '" for area "' + area + '"');
  1438. $('[data-type="' + name + '"]', area).removeClass('widget-hide');
  1439. } else {
  1440. ftui.log(1, 'Failed to load plugin "' + name + '" for area "' + area + '"');
  1441. }
  1442. deferredLoad.resolve();
  1443. });
  1444. })
  1445. .fail(function () {
  1446. ftui.toast('Failed to load plugin : ' + name);
  1447. ftui.log(1, 'Failed to load plugin : ' + name + ' - add <script src="js/widget_' + name +
  1448. '.js" defer></script> do your page, to see more informations about this failure');
  1449. deferredLoad.resolve();
  1450. });
  1451. // return with promise to deliver the plugin deferred
  1452. return deferredLoad.promise();
  1453. },
  1454. dynamicload: function (url, async) {
  1455. ftui.log(3, 'dynamic load file:' + url + ' / async:' + async);
  1456. var deferred = new $.Deferred();
  1457. var isAdded = false;
  1458. // check if it is already included
  1459. for (var i = 0, len = ftui.scripts.length; i < len; i++) {
  1460. if (ftui.scripts[i].url === url) {
  1461. isAdded = true;
  1462. break;
  1463. }
  1464. }
  1465. if (!isAdded) {
  1466. // not yet -> load
  1467. if (url.match(new RegExp('^.*\.(js)$'))) {
  1468. var script = document.createElement("script");
  1469. script.type = "text/javascript";
  1470. script.async = (async) ? true : false;
  1471. script.src = url;
  1472. script.onload = function () {
  1473. ftui.log(3, 'dynamidynamic load done:' + url);
  1474. deferred.resolve();
  1475. };
  1476. document.getElementsByTagName('head')[0].appendChild(script);
  1477. } else {
  1478. var link = document.createElement('link');
  1479. link.rel = 'stylesheet';
  1480. link.type = 'text/css';
  1481. link.href = url;
  1482. link.media = 'all';
  1483. deferred.resolve();
  1484. document.getElementsByTagName('head')[0].appendChild(link);
  1485. }
  1486. var scriptObject = {};
  1487. scriptObject.deferred = deferred;
  1488. scriptObject.url = url;
  1489. ftui.scripts.push(scriptObject);
  1490. } else {
  1491. // already loaded
  1492. ftui.log(3, 'dynamic load not neccesary for:' + url);
  1493. deferred = ftui.scripts[i].deferred;
  1494. }
  1495. return deferred.promise();
  1496. },
  1497. getCSrf: function () {
  1498. $.ajax({
  1499. 'url': ftui.config.fhemDir,
  1500. 'type': 'GET',
  1501. cache: false,
  1502. username: ftui.config.username,
  1503. password: ftui.config.password,
  1504. data: {
  1505. XHR: "1"
  1506. },
  1507. 'success': function (data, textStatus, jqXHR) {
  1508. ftui.config.csrf = jqXHR.getResponseHeader('X-FHEM-csrfToken');
  1509. ftui.log(1, 'Got csrf from FHEM:' + ftui.config.csrf);
  1510. }
  1511. }).fail(function (jqXHR, textStatus, errorThrown) {
  1512. ftui.log(1, "Failed to get csrfToken: " + textStatus + ": " + errorThrown);
  1513. ftui.config.shortPollDelay = 30000;
  1514. });
  1515. },
  1516. healthCheck: function () {
  1517. var timeDiff = new Date() - ftui.poll.long.lastEventTimestamp;
  1518. if (timeDiff / 1000 > ftui.config.maxLongpollAge &&
  1519. ftui.config.maxLongpollAge > 0 &&
  1520. !ftui.config.DEMO &&
  1521. ftui.config.doLongPoll) {
  1522. ftui.log(1, 'No longpoll event since ' + timeDiff / 1000 + 'secondes -> restart polling');
  1523. ftui.setOnline();
  1524. ftui.restartLongPoll();
  1525. }
  1526. },
  1527. FS20: {
  1528. 'dimmerArray': [0, 6, 12, 18, 25, 31, 37, 43, 50, 56, 62, 68, 75, 81, 87, 93, 100],
  1529. 'dimmerValue': function (value) {
  1530. var idx = ftui.indexOfNumeric(this.dimmerArray, value);
  1531. return (idx > -1) ? this.dimmerArray[idx] : 0;
  1532. }
  1533. },
  1534. rgbToHsl: function (rgb) {
  1535. var r = parseInt(rgb.substring(0, 2), 16);
  1536. var g = parseInt(rgb.substring(2, 4), 16);
  1537. var b = parseInt(rgb.substring(4, 6), 16);
  1538. r /= 255;
  1539. g /= 255;
  1540. b /= 255;
  1541. var max = Math.max(r, g, b),
  1542. min = Math.min(r, g, b);
  1543. var h, s, l = (max + min) / 2;
  1544. if (max == min) {
  1545. h = s = 0; // achromatic
  1546. } else {
  1547. var d = max - min;
  1548. s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
  1549. switch (max) {
  1550. case r:
  1551. h = (g - b) / d + (g < b ? 6 : 0);
  1552. break;
  1553. case g:
  1554. h = (b - r) / d + 2;
  1555. break;
  1556. case b:
  1557. h = (r - g) / d + 4;
  1558. break;
  1559. }
  1560. h /= 6;
  1561. }
  1562. return [h, s, l];
  1563. },
  1564. hslToRgb: function (h, s, l) {
  1565. var r, g, b;
  1566. var hex = function (x) {
  1567. return ("0" + parseInt(x).toString(16)).slice(-2);
  1568. };
  1569. var hue2rgb;
  1570. if (s === 0) {
  1571. r = g = b = l; // achromatic
  1572. } else {
  1573. hue2rgb = function (p, q, t) {
  1574. if (t < 0) t += 1;
  1575. if (t > 1) t -= 1;
  1576. if (t < 1 / 6) return p + (q - p) * 6 * t;
  1577. if (t < 1 / 2) return q;
  1578. if (t < 2 / 3) return p + (q - p) * (2 / 3 - t) * 6;
  1579. return p;
  1580. };
  1581. var q = l < 0.5 ? l * (1 + s) : l + s - l * s;
  1582. var p = 2 * l - q;
  1583. r = hue2rgb(p, q, h + 1 / 3);
  1584. g = hue2rgb(p, q, h);
  1585. b = hue2rgb(p, q, h - 1 / 3);
  1586. }
  1587. return [hex(Math.round(r * 255)), hex(Math.round(g * 255)), hex(Math.round(b * 255))].join('');
  1588. },
  1589. rgbToHex: function (rgb) {
  1590. var tokens = rgb.match(/^rgba?[\s+]?\([\s+]?(\d+)[\s+]?,[\s+]?(\d+)[\s+]?,[\s+]?(\d+)[\s+]?/i);
  1591. return (tokens && tokens.length === 4) ? "#" +
  1592. ("0" + parseInt(tokens[1], 10).toString(16)).slice(-2) +
  1593. ("0" + parseInt(tokens[2], 10).toString(16)).slice(-2) +
  1594. ("0" + parseInt(tokens[3], 10).toString(16)).slice(-2) : rgb;
  1595. },
  1596. getGradientColor: function (start_color, end_color, percent) {
  1597. // strip the leading # if it's there
  1598. start_color = this.rgbToHex(start_color).replace(/^\s*#|\s*$/g, '');
  1599. end_color = this.rgbToHex(end_color).replace(/^\s*#|\s*$/g, '');
  1600. // convert 3 char codes --> 6, e.g. `E0F` --> `EE00FF`
  1601. if (start_color.length == 3) {
  1602. start_color = start_color.replace(/(.)/g, '$1$1');
  1603. }
  1604. if (end_color.length == 3) {
  1605. end_color = end_color.replace(/(.)/g, '$1$1');
  1606. }
  1607. // get colors
  1608. var start_red = parseInt(start_color.substr(0, 2), 16),
  1609. start_green = parseInt(start_color.substr(2, 2), 16),
  1610. start_blue = parseInt(start_color.substr(4, 2), 16);
  1611. var end_red = parseInt(end_color.substr(0, 2), 16),
  1612. end_green = parseInt(end_color.substr(2, 2), 16),
  1613. end_blue = parseInt(end_color.substr(4, 2), 16);
  1614. // calculate new color
  1615. var diff_red = end_red - start_red;
  1616. var diff_green = end_green - start_green;
  1617. var diff_blue = end_blue - start_blue;
  1618. diff_red = ((diff_red * percent) + start_red).toString(16).split('.')[0];
  1619. diff_green = ((diff_green * percent) + start_green).toString(16).split('.')[0];
  1620. diff_blue = ((diff_blue * percent) + start_blue).toString(16).split('.')[0];
  1621. // ensure 2 digits by color
  1622. if (diff_red.length == 1)
  1623. diff_red = '0' + diff_red;
  1624. if (diff_green.length == 1)
  1625. diff_green = '0' + diff_green;
  1626. if (diff_blue.length == 1)
  1627. diff_blue = '0' + diff_blue;
  1628. return '#' + diff_red + diff_green + diff_blue;
  1629. },
  1630. getPart: function (value, part) {
  1631. if (ftui.isValid(part)) {
  1632. if ($.isNumeric(part)) {
  1633. var tokens = (ftui.isValid(value)) ? value.toString().split(" ") : '';
  1634. return (tokens.length >= part && part > 0) ? tokens[part - 1] : value;
  1635. } else {
  1636. var ret = '';
  1637. if (ftui.isValid(value)) {
  1638. var matches = value.match(new RegExp('^' + part + '$'));
  1639. if (matches) {
  1640. for (var i = 1, len = matches.length; i < len; i++) {
  1641. ret += matches[i];
  1642. }
  1643. }
  1644. }
  1645. return ret;
  1646. }
  1647. }
  1648. return value;
  1649. },
  1650. showModal: function (modal) {
  1651. if (modal)
  1652. $("#shade").fadeIn(ftui.config.fadeTime);
  1653. else
  1654. $("#shade").fadeOut(ftui.config.fadeTime);
  1655. },
  1656. precision: function (a) {
  1657. var s = a + "",
  1658. d = s.indexOf('.') + 1;
  1659. return !d ? 0 : s.length - d;
  1660. },
  1661. // 1. numeric, 2. regex, 3. negation double, 4. indexof
  1662. indexOfGeneric: function (array, find) {
  1663. if (!array) return -1;
  1664. for (var i = 0, len = array.length; i < len; i++) {
  1665. // leave the loop on first none numeric item
  1666. if (!$.isNumeric(array[i]))
  1667. return ftui.indexOfRegex(array, find);
  1668. }
  1669. return ftui.indexOfNumeric(array, find);
  1670. },
  1671. indexOfNumeric: function (array, val) {
  1672. var ret = -1;
  1673. for (var i = 0, len = array.length; i < len; i++) {
  1674. if (Number(val) >= Number(array[i]))
  1675. ret = i;
  1676. }
  1677. return ret;
  1678. },
  1679. indexOfRegex: function (array, find) {
  1680. var len = array.length;
  1681. for (var i = 0; i < len; i++) {
  1682. try {
  1683. var match = find.match(new RegExp('^' + array[i] + '$'));
  1684. if (match)
  1685. return i;
  1686. } catch (e) {}
  1687. }
  1688. // negation double
  1689. if (len === 2 && array[0] === '!' + array[1] && find !== array[0]) {
  1690. return 0;
  1691. }
  1692. if (len === 2 && array[1] === '!' + array[0] && find !== array[1]) {
  1693. return 1;
  1694. }
  1695. // last chance: index of
  1696. return array.indexOf(find);
  1697. },
  1698. isValid: function (v) {
  1699. return (v !== void 0 && typeof v !== typeof notusedvar);
  1700. },
  1701. // global date format functions
  1702. dateFromString: function (str) {
  1703. var m = str.match(/(\d+)-(\d+)-(\d+)[_\s](\d+):(\d+):(\d+).*/);
  1704. var m2 = str.match(/^(\d+)$/);
  1705. var m3 = str.match(/(\d\d).(\d\d).(\d\d\d\d)/);
  1706. var offset = new Date().getTimezoneOffset();
  1707. return (m) ? new Date(+m[1], +m[2] - 1, +m[3], +m[4], +m[5], +m[6]) :
  1708. (m2) ? new Date(70, 0, 1, 0, 0, m2[1], 0) :
  1709. (m3) ? new Date(+m3[3], +m3[2] - 1, +m3[1], 0, -offset, 0, 0) : new Date();
  1710. },
  1711. diffMinutes: function (date1, date2) {
  1712. var diff = new Date(date2 - date1);
  1713. return (diff / 1000 / 60).toFixed(0);
  1714. },
  1715. diffSeconds: function (date1, date2) {
  1716. var diff = new Date(date2 - date1);
  1717. return (diff / 1000).toFixed(1);
  1718. },
  1719. durationFromSeconds: function (time) {
  1720. var hrs = Math.floor(time / 3600);
  1721. var mins = Math.floor((time % 3600) / 60);
  1722. var secs = time % 60;
  1723. var ret = "";
  1724. if (hrs > 0) {
  1725. ret += "" + hrs + ":" + (mins < 10 ? "0" : "");
  1726. }
  1727. ret += "" + mins + ":" + (secs < 10 ? "0" : "");
  1728. ret += "" + secs;
  1729. return ret;
  1730. },
  1731. mapColor: function (value) {
  1732. return ftui.getStyle('.' + value, 'color') || value;
  1733. },
  1734. round: function (number, precision) {
  1735. var shift = function (number, precision, reverseShift) {
  1736. if (reverseShift) {
  1737. precision = -precision;
  1738. }
  1739. var numArray = ("" + number).split("e");
  1740. return +(numArray[0] + "e" + (numArray[1] ? (+numArray[1] + precision) : precision));
  1741. };
  1742. return shift(Math.round(shift(number, precision, false)), precision, true);
  1743. },
  1744. parseJsonFromString: function (str) {
  1745. return JSON.parse(str);
  1746. },
  1747. getAndroidVersion: function (ua) {
  1748. ua = (ua || navigator.userAgent).toLowerCase();
  1749. var match = ua.match(/android\s([0-9\.]*)/);
  1750. return match ? match[1] : false;
  1751. },
  1752. getStyle: function (selector, prop) {
  1753. var props = ftui.config.styleCollection[selector];
  1754. var style = (props && props[prop]) ? props[prop] : null;
  1755. if (style === null) {
  1756. var reverseSelector = '.' + selector.split('.').reverse().join('.');
  1757. reverseSelector = reverseSelector.substring(0, reverseSelector.length - 1);
  1758. props = ftui.config.styleCollection[reverseSelector];
  1759. style = (props && props[prop]) ? props[prop] : null;
  1760. }
  1761. return style;
  1762. },
  1763. getClassColor: function (elem) {
  1764. for (var i = ftui.config.stdColors.length - 1; i >= 0; i -= 1) {
  1765. if (elem.hasClass(ftui.config.stdColors[i])) {
  1766. return ftui.getStyle('.' + ftui.config.stdColors[i], 'color');
  1767. }
  1768. }
  1769. return null;
  1770. },
  1771. getIconId: function (iconName) {
  1772. if (!iconName || iconName === '' || !$('link[href$="lib/font-awesome.min.css"]').length)
  1773. return "?";
  1774. var cssFile = $('link[href$="lib/font-awesome.min.css"]')[0];
  1775. if (cssFile && cssFile.sheet && cssFile.sheet.cssRules) {
  1776. var rules = cssFile.sheet.cssRules;
  1777. for (var rule in rules) {
  1778. if (rules[rule].selectorText && rules[rule].selectorText.match(new RegExp(iconName + ':'))) {
  1779. var id = rules[rule].style.content;
  1780. if (!id)
  1781. return iconName;
  1782. id = id.replace(/"/g, '').replace(/'/g, "");
  1783. return (/[^\u0000-\u00ff]/.test(id)) ? id :
  1784. String.fromCharCode(parseInt(id.replace('\\', ''), 16));
  1785. }
  1786. }
  1787. }
  1788. },
  1789. disableSelection: function () {
  1790. $("body").each(function () {
  1791. this.onselectstart = function () {
  1792. return false;
  1793. };
  1794. this.unselectable = "on";
  1795. $(this).css('-moz-user-select', 'none');
  1796. $(this).css('-webkit-user-select', 'none');
  1797. });
  1798. },
  1799. hideWidgets: function (area) {
  1800. $('[data-type]', area).addClass('widget-hide');
  1801. },
  1802. toast: function (text, error) {
  1803. //https://github.com/kamranahmedse/jquery-toast-plugin
  1804. if (ftui.config.TOAST !== 0) {
  1805. var tstack = ftui.config.TOAST;
  1806. if (ftui.config.TOAST == 1)
  1807. tstack = false;
  1808. if (error && error === 'error') {
  1809. if (f7) {
  1810. f7.ftui.addNotification({
  1811. title: 'FTUI',
  1812. message: text,
  1813. hold: 1500
  1814. });
  1815. } else if ($.toast) {
  1816. return $.toast({
  1817. heading: 'Error',
  1818. text: text,
  1819. hideAfter: 20000, // in milli seconds
  1820. icon: 'error',
  1821. loader: false,
  1822. position: ftui.config.toastPosition,
  1823. stack: tstack
  1824. });
  1825. }
  1826. } else
  1827. if (f7) {
  1828. f7.ftui.addNotification({
  1829. title: 'FTUI',
  1830. message: text,
  1831. hold: 1500
  1832. });
  1833. } else if ($.toast) {
  1834. return $.toast({
  1835. text: text,
  1836. loader: false,
  1837. position: ftui.config.toastPosition,
  1838. stack: tstack
  1839. });
  1840. }
  1841. }
  1842. },
  1843. log: function (level, text) {
  1844. if (ftui.config.debuglevel >= level)
  1845. console.log(text);
  1846. },
  1847. };
  1848. // global helper functions
  1849. String.prototype.toDate = function () {
  1850. return ftui.dateFromString(this);
  1851. };
  1852. String.prototype.parseJson = function () {
  1853. return ftui.parseJsonFromString(this);
  1854. };
  1855. String.prototype.toMinFromMs = function () {
  1856. var x = Number(this) / 1000;
  1857. var ss = (Math.floor(x % 60)).toString();
  1858. var mm = (Math.floor(x /= 60)).toString();
  1859. return mm + ":" + (ss[1] ? ss : "0" + ss[0]);
  1860. };
  1861. String.prototype.toMinFromSec = function () {
  1862. var x = Number(this);
  1863. var ss = (Math.floor(x % 60)).toString();
  1864. var mm = (Math.floor(x /= 60)).toString();
  1865. return mm + ":" + (ss[1] ? ss : "0" + ss[0]);
  1866. };
  1867. String.prototype.toHoursFromSec = function () {
  1868. var x = Number(this);
  1869. var hh = (Math.floor(x / 3600)).toString();
  1870. var ss = (Math.floor(x % 60)).toString();
  1871. var mm = (Math.floor(x / 60) - (hh * 60)).toString();
  1872. return hh + ":" + (mm[1] ? mm : "0" + mm[0]) + ":" + (ss[1] ? ss : "0" + ss[0]);
  1873. };
  1874. Date.prototype.addMinutes = function (minutes) {
  1875. return new Date(this.getTime() + minutes * 60000);
  1876. };
  1877. Date.prototype.ago = function (format) {
  1878. var now = new Date();
  1879. var ms = (now - this);
  1880. var x = ms / 1000;
  1881. var seconds = Math.floor(x % 60);
  1882. x /= 60;
  1883. var minutes = Math.floor(x % 60);
  1884. x /= 60;
  1885. var hours = Math.floor(x % 24);
  1886. x /= 24;
  1887. var days = Math.floor(x);
  1888. var strUnits = (ftui.config.lang === 'de') ? ['Tag(e)', 'Stunde(n)', 'Minute(n)', 'Sekunde(n)'] : ['day(s)', 'hour(s)', 'minute(s)',
  1889. 'second(s)'];
  1890. var ret;
  1891. if (ftui.isValid(format)) {
  1892. ret = format.replace('dd', days);
  1893. ret = ret.replace('hh', (hours > 9) ? hours : '0' + hours);
  1894. ret = ret.replace('mm', (minutes > 9) ? minutes : '0' + minutes);
  1895. ret = ret.replace('ss', (seconds > 9) ? seconds : '0' + seconds);
  1896. ret = ret.replace('h', hours);
  1897. ret = ret.replace('m', minutes);
  1898. ret = ret.replace('s', seconds);
  1899. } else {
  1900. ret = (days > 0) ? days + " " + strUnits[0] + " " : "";
  1901. ret += (hours > 0) ? hours + " " + strUnits[1] + " " : "";
  1902. ret += (minutes > 0) ? minutes + " " + strUnits[2] + " " : "";
  1903. ret += seconds + " " + strUnits[3];
  1904. }
  1905. return ret;
  1906. };
  1907. Date.prototype.format = function (format) {
  1908. var YYYY = this.getFullYear().toString();
  1909. var YY = this.getYear().toString();
  1910. var MM = (this.getMonth() + 1).toString(); // getMonth() is zero-based
  1911. var dd = this.getDate().toString();
  1912. var hh = this.getHours().toString();
  1913. var mm = this.getMinutes().toString();
  1914. var ss = this.getSeconds().toString();
  1915. var eeee = this.eeee();
  1916. var eee = this.eee();
  1917. var ee = this.ee();
  1918. var ret = format;
  1919. ret = ret.replace('DD', (dd > 9) ? dd : '0' + dd);
  1920. ret = ret.replace('D', dd);
  1921. ret = ret.replace('MM', (MM > 9) ? MM : '0' + MM);
  1922. ret = ret.replace('M', MM);
  1923. ret = ret.replace('YYYY', YYYY);
  1924. ret = ret.replace('YY', YY);
  1925. ret = ret.replace('hh', (hh > 9) ? hh : '0' + hh);
  1926. ret = ret.replace('mm', (mm > 9) ? mm : '0' + mm);
  1927. ret = ret.replace('ss', (ss > 9) ? ss : '0' + ss);
  1928. ret = ret.replace('h', hh);
  1929. ret = ret.replace('m', mm);
  1930. ret = ret.replace('s', ss);
  1931. ret = ret.replace('eeee', eeee);
  1932. ret = ret.replace('eee', eee);
  1933. ret = ret.replace('ee', ee);
  1934. return ret;
  1935. };
  1936. Date.prototype.yyyymmdd = function () {
  1937. var yyyy = this.getFullYear().toString();
  1938. var mm = (this.getMonth() + 1).toString(); // getMonth() is zero-based
  1939. var dd = this.getDate().toString();
  1940. return yyyy + '-' + (mm[1] ? mm : "0" + mm[0]) + '-' + (dd[1] ? dd : "0" + dd[0]); // padding
  1941. };
  1942. Date.prototype.ddmmyyyy = function () {
  1943. var yyyy = this.getFullYear().toString();
  1944. var mm = (this.getMonth() + 1).toString(); // getMonth() is zero-based
  1945. var dd = this.getDate().toString();
  1946. return (dd[1] ? dd : "0" + dd[0]) + '.' + (mm[1] ? mm : "0" + mm[0]) + '.' + yyyy; // padding
  1947. };
  1948. Date.prototype.hhmm = function () {
  1949. var hh = this.getHours().toString();
  1950. var mm = this.getMinutes().toString();
  1951. return (hh[1] ? hh : "0" + hh[0]) + ':' + (mm[1] ? mm : "0" + mm[0]); // padding
  1952. };
  1953. Date.prototype.hhmmss = function () {
  1954. var hh = this.getHours().toString();
  1955. var mm = this.getMinutes().toString();
  1956. var ss = this.getSeconds().toString();
  1957. return (hh[1] ? hh : "0" + hh[0]) + ':' + (mm[1] ? mm : "0" + mm[0]) + ':' + (ss[1] ? ss : "0" + ss[0]); // padding
  1958. };
  1959. Date.prototype.ddmm = function () {
  1960. var mm = (this.getMonth() + 1).toString(); // getMonth() is zero-based
  1961. var dd = this.getDate().toString();
  1962. return (dd[1] ? dd : "0" + dd[0]) + '.' + (mm[1] ? mm : "0" + mm[0]) + '.'; // padding
  1963. };
  1964. Date.prototype.ddmmhhmm = function () {
  1965. var MM = (this.getMonth() + 1).toString(); // getMonth() is zero-based
  1966. var dd = this.getDate().toString();
  1967. var hh = this.getHours().toString();
  1968. var mm = this.getMinutes().toString();
  1969. return (dd[1] ? dd : "0" + dd[0]) + '.' + (MM[1] ? MM : "0" + MM[0]) + '. ' +
  1970. (hh[1] ? hh : "0" + hh[0]) + ':' + (mm[1] ? mm : "0" + mm[0]);
  1971. };
  1972. Date.prototype.eeee = function () {
  1973. var weekday_de = ['Sonntag', 'Montag', 'Dienstag', 'Mittwoch', 'Donnerstag', 'Freitag', 'Samstag'];
  1974. var weekday = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
  1975. if (ftui.config.lang === 'de')
  1976. return weekday_de[this.getDay()];
  1977. return weekday[this.getDay()];
  1978. };
  1979. Date.prototype.eee = function () {
  1980. var weekday_de = ['Son', 'Mon', 'Die', 'Mit', 'Don', 'Fre', 'Sam'];
  1981. var weekday = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
  1982. if (ftui.config.lang === 'de')
  1983. return weekday_de[this.getDay()];
  1984. return weekday[this.getDay()];
  1985. };
  1986. Date.prototype.ee = function () {
  1987. var weekday_de = ['So', 'Mo', 'Di', 'Mi', 'Do', 'Fr', 'Sa'];
  1988. var weekday = ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa'];
  1989. if (ftui.config.lang === 'de')
  1990. return weekday_de[this.getDay()];
  1991. return weekday[this.getDay()];
  1992. };
  1993. function onjQueryLoaded() {
  1994. /* EVENTS */
  1995. // event "page is loaded" -> start FTUI
  1996. ftui.init();
  1997. $('.menu').on('click', function () {
  1998. $('.menu').toggleClass('show');
  1999. });
  2000. $(window).on('beforeunload', function () {
  2001. ftui.log(5, 'beforeunload');
  2002. ftui.setOffline();
  2003. });
  2004. $(window).on('online offline', function () {
  2005. ftui.log(5, 'online offline');
  2006. if (navigator.onLine)
  2007. ftui.setOnline();
  2008. else
  2009. ftui.setOffline();
  2010. });
  2011. window.onerror = function (msg, url, lineNo, columnNo, error) {
  2012. var file = url.split('/').pop();
  2013. ftui.toast([file + ':' + lineNo, error].join('<br/>'), 'error');
  2014. return false;
  2015. };
  2016. $.fn.once = function (a, b) {
  2017. return this.each(function () {
  2018. $(this).off(a).on(a, b);
  2019. });
  2020. };
  2021. // for widget
  2022. $.fn.widgetId = function () {
  2023. return [$(this).data('type'), ($(this).data('device') ? $(this).data('device').replace(' ', 'default') : 'default'), $(this).data('get'), $(this).index()].join('.');
  2024. };
  2025. $.fn.wgid = function () {
  2026. var elem = $(this);
  2027. if (!elem.isValidData('wgid')) {
  2028. var wgid = elem.data('type') + '_xxxx-xxxx-xxxx'.replace(/[xy]/g, function (c) {
  2029. var r = Math.random() * 16 | 0,
  2030. v = c == 'x' ? r : (r & 0x3 | 0x8);
  2031. return v.toString(16);
  2032. });
  2033. elem.attr('data-wgid', wgid);
  2034. }
  2035. return elem.data('wgid');
  2036. };
  2037. $.fn.filterData = function (key, value) {
  2038. return this.filter(function () {
  2039. return $(this).data(key) == value;
  2040. });
  2041. };
  2042. $.fn.filterDeviceReading = function (key, device, param) {
  2043. return $(this).filter(function () {
  2044. return $(this).matchDeviceReading(key, device, param);
  2045. });
  2046. };
  2047. $.fn.matchDeviceReading = function (key, device, param) {
  2048. var elem = $(this);
  2049. var value = elem.data(key);
  2050. return (String(value) === param && String(elem.data('device')) === device) ||
  2051. (value === device + ':' + param || value === '[' + device + ':' + param + ']') ||
  2052. ($.inArray(param, value) > -1 && String(elem.data('device')) === device) ||
  2053. ($.inArray(device + ':' + param, value) > -1);
  2054. };
  2055. $.fn.isValidData = function (key) {
  2056. return ($(this).data(key) !== void 0);
  2057. };
  2058. $.fn.isValidAttr = function (key) {
  2059. return ($(this).attr(key) !== void 0);
  2060. };
  2061. $.fn.initData = function (key, value) {
  2062. var elem = $(this);
  2063. elem.data(key, elem.isValidData(key) ? elem.data(key) : value);
  2064. return elem;
  2065. };
  2066. $.fn.reinitData = function (key, value) {
  2067. var elem = $(this),
  2068. attrKey = 'data-' + key;
  2069. elem.data(key, elem.isValidAttr(attrKey) ? elem.attr(attrKey) : value);
  2070. return elem;
  2071. };
  2072. $.fn.initClassColor = function (key) {
  2073. var elem = $(this),
  2074. value = ftui.getClassColor(elem);
  2075. if (value) elem.attr('data-' + key, value);
  2076. };
  2077. $.fn.mappedColor = function (key) {
  2078. return ftui.getStyle('.' + $(this).data(key), 'color') || $(this).data(key);
  2079. };
  2080. $.fn.matchingState = function (key, value) {
  2081. if (!ftui.isValid(value)) {
  2082. return '';
  2083. }
  2084. var elm = $(this);
  2085. var state = String(ftui.getPart(value, elm.data(key + '-part')));
  2086. var onData = elm.data(key + '-on');
  2087. var offData = elm.data(key + '-off');
  2088. var on = String(onData);
  2089. var temp, device, reading, param;
  2090. if (on.match(/:/)) {
  2091. temp = on.split(':');
  2092. device = temp[0].replace('[', '');
  2093. reading = temp[1].replace(']', '');
  2094. param = ftui.getDeviceParameter(device, reading);
  2095. if (param && ftui.isValid(param)) {
  2096. on = param.val;
  2097. }
  2098. }
  2099. var off = String(offData);
  2100. if (off.match(/:/)) {
  2101. temp = off.split(':');
  2102. device = temp[0].replace('[', '');
  2103. reading = temp[1].replace(']', '');
  2104. param = ftui.getDeviceParameter(device, reading);
  2105. if (param && ftui.isValid(param)) {
  2106. off = param.val;
  2107. }
  2108. }
  2109. if (ftui.isValid(onData)) {
  2110. if (state === on) {
  2111. return 'on';
  2112. } else if (state.match(new RegExp('^' + on + '$'))) {
  2113. return 'on';
  2114. }
  2115. }
  2116. if (ftui.isValid(offData)) {
  2117. if (state === off) {
  2118. return 'off';
  2119. } else if (state.match(new RegExp('^' + off + '$'))) {
  2120. return 'off';
  2121. }
  2122. }
  2123. if (ftui.isValid(onData) && ftui.isValid(offData)) {
  2124. if (on === '!off' && !state.match(new RegExp('^' + off + '$'))) {
  2125. return 'on';
  2126. } else if (off === '!on' && !state.match(new RegExp('^' + on + '$'))) {
  2127. return 'off';
  2128. } else if (on === '!' + off && !state.match(new RegExp('^' + off + '$'))) {
  2129. return 'on';
  2130. } else if (off === '!' + on && !state.match(new RegExp('^' + on + '$'))) {
  2131. return 'off';
  2132. }
  2133. }
  2134. };
  2135. $.fn.isDeviceReading = function (key) {
  2136. var reading = $(this).data(key);
  2137. return reading && !$.isNumeric(reading) && typeof reading === 'string' && reading.match(/^[\w\s-.]+:[\w\s-]+$/);
  2138. };
  2139. $.fn.isExternData = function (key) {
  2140. var data = $(this).data(key);
  2141. if (!data) return '';
  2142. return (data.match(/^[#\.\[][^:]*$/));
  2143. };
  2144. $.fn.cleanWhitespace = function () {
  2145. var textNodes = this.contents().filter(
  2146. function () {
  2147. return (this.nodeType == 3 && !/\S/.test(this.nodeValue));
  2148. })
  2149. .remove();
  2150. return this;
  2151. };
  2152. $.fn.getReading = function (key, idx) {
  2153. var devname = String($(this).data('device')),
  2154. paraname = $(this).data(key);
  2155. if ($.isArray(paraname)) {
  2156. paraname = paraname[idx];
  2157. }
  2158. paraname = String(paraname);
  2159. if (paraname && paraname.match(/:/)) {
  2160. var temp = paraname.split(':');
  2161. devname = temp[0].replace('[', '');
  2162. paraname = temp[1].replace(']', '');
  2163. }
  2164. if (devname && devname.length > 0) {
  2165. var params = ftui.deviceStates[devname];
  2166. return (params && params[paraname]) ? params[paraname] : {};
  2167. }
  2168. return {};
  2169. };
  2170. $.fn.valOfData = function (key) {
  2171. var data = $(this).data(key);
  2172. if (!ftui.isValid(data)) return '';
  2173. return (data.toString().match(/^[#\.\[][^:]*$/)) ? $(data).data('value') : data;
  2174. };
  2175. $.fn.transmitCommand = function () {
  2176. if ($(this).hasClass('notransmit')) return;
  2177. var cmdl = [$(this).valOfData('cmd'), $(this).valOfData('device') + $(this).valOfData('filter'), $(this).valOfData('set'), $(
  2178. this).valOfData('value')].join(' ');
  2179. ftui.setFhemStatus(cmdl);
  2180. ftui.toast(cmdl);
  2181. };
  2182. }
  2183. // detect self location
  2184. var src = document.querySelector('script[src*="fhem-tablet-ui"]').getAttribute('src');
  2185. var file = src.split('/').pop();
  2186. src = src.replace('/' + file, '');
  2187. var dir = src.split('/').pop();
  2188. ftui.config.basedir = src.replace(dir, '');
  2189. if (ftui.config.basedir === '') ftui.config.basedir = './';
  2190. console.log('Base dir: ' + ftui.config.basedir);
  2191. // load jQuery lib
  2192. if (!ftui.isValid(window.jQuery)) {
  2193. var script = document.createElement('script');
  2194. script.type = 'text/javascript';
  2195. script.onload = function () {
  2196. (function ($) {
  2197. $(document).ready(function () {
  2198. console.log('jQuery dynamically loaded');
  2199. onjQueryLoaded();
  2200. });
  2201. })(jQuery);
  2202. };
  2203. script.src = ftui.config.basedir + "lib/jquery.min.js";
  2204. document.getElementsByTagName('head')[0].appendChild(script);
  2205. } else {
  2206. $(document).ready(function () {
  2207. onjQueryLoaded();
  2208. });
  2209. }