Type.registerNamespace("Tandan.Portal.WeatherControl"); Tandan.Portal.WeatherControl = function (element) { Tandan.Portal.WeatherControl.initializeBase(this, [element]); Tandan.Portal.WeatherControl.instances[element.id] = this; var _t = this; this.__trigger_map = null; this.__templates = null; this.__ui_elements = null; this._locs = null; this._settingsUrl = "data/weather.js"; this._lgIconUrlBase = "/_layouts/tandan.portal/weather/img/128/{icon-id}.png"; this._smIconUrlBase = "/_layouts/tandan.portal/weather/img/48/{icon-id}.png"; this._cntHours = 8; this._cntDays = 7; this._tempUnit = 'C'; this._dateFormat = 'dd - MM'; this.showNextHours = false; this.showNextDays = false; this.settingLoading = false; this.currentLoading = false; this.nextDaysLoading = false; this.nextHoursLoading = false; this.dataContext = {}; function getCrossDomain(url, onsuccess, onerror, always) { // // for ie // $.support.cors = true; if (window.XDomainRequest) { var xdr = new XDomainRequest(); var query = url; if (xdr) { xdr.onload = function () { var data = eval("[" + xdr.responseText + "]")[0]; if (onsuccess) onsuccess(data); if (always) always(); } xdr.onerror = function () { if (onerror) onerror(); if (always) always(); } xdr.open('GET', query); xdr.send(); } } // other browsers else { $.ajax({ crossDomain: true, url: url, type: 'GET', datatype: 'jsonp', async: true }).done(onsuccess).fail(onerror).always(always); } } this.commands = { loadLocations: function (func) { _t.settingLoading = true; _t.triggers.settingLoading(); if (_t._locs == null) { $.ajax({ url: _t._settingsUrl, type: 'GET', async: true, datatype: 'application/json' }).done(function (data) { if (typeof data == 'string' || data instanceof String) data = eval(data)[0]; else data = data[0]; _t.dataContext.locations = data.locations; _t.dataContext.cods = data.cods; _t.dataContext.currentIndex = 0; _t.triggers.locationLoaded(func); }).always(function () { _t.settingLoading = false; _t.triggers.settingLoading(); }); } else { _t.dataContext.locations = _t._locs; _t.dataContext.currentIndex = 0; _t.triggers.locationLoaded(func); _t.settingLoading = false; _t.triggers.settingLoading(); } }, loadCurrent: function () { _t.currentLoading = true; _t.triggers.currentLoading(); getCrossDomain("https://api.openweathermap.org/data/2.5/weather?appid=e9bd1a23e7ded0475b409e7d1480fa10&id=" + _t.dataContext.locations[_t.dataContext.currentIndex].id, function (data) { _t.dataContext.current = data; _t.triggers.currentWeatherLoaded(); }, null, function () { _t.currentLoading = false; _t.triggers.currentLoading(); }); }, loadNextHours: function () { _t.nextHoursLoading = true; _t.triggers.nextHoursLoading(); getCrossDomain("https://api.openweathermap.org/data/2.5/forecast?appid=e9bd1a23e7ded0475b409e7d1480fa10&id=" + _t.dataContext.locations[_t.dataContext.currentIndex].id, function (data) { if (data.list.length > _t._cntHours) data.list.splice(_t._cntHours, data.list.length - _t._cntHours); _t.dataContext.nextHours = data; _t.triggers.nextHoursLoaded(); }, null, function () { _t.nextHoursLoading = false; _t.triggers.nextHoursLoading(); }); }, loadNextDays: function () { _t.nextDaysLoading = true; _t.triggers.nextDaysLoading(); getCrossDomain("https://api.openweathermap.org/data/2.5/forecast/daily?appid=e9bd1a23e7ded0475b409e7d1480fa10&id=" + _t.dataContext.locations[_t.dataContext.currentIndex].id + "&cnt=" + _t._cntDays, function (data) { _t.dataContext.nextDays = data; _t.triggers.nextDaysLoaded(); }, null, function () { _t.nextDaysLoading = false; _t.triggers.nextDaysLoading(); }); }, toggleNextHours: function () { _t.showNextHours = !_t.showNextHours; if (_t.showNextHours && !_t.dataContext.nextHours) _t.commands.loadNextHours(); _t.triggers.nextHoursShowChanged(); }, showNextHours: function (val) { _t.showNextHours = val; if (_t.showNextHours && !_t.dataContext.nextHours) _t.commands.loadNextHours(); _t.triggers.nextHoursShowChanged(); }, toggleNextDays: function () { _t.showNextDays = !_t.showNextDays; if (_t.showNextDays && !_t.dataContext.nextDays) _t.commands.loadNextDays(); _t.triggers.nextDaysShowChanged(); }, showNextDays: function (val) { _t.showNextDays = val; if (_t.showNextDays && !_t.dataContext.nextDays) _t.commands.loadNextDays(); _t.triggers.nextDaysShowChanged(); }, isShowNextHours: function () { return _t.showNextHours }, isShowNextDays: function () { return _t.showNextDays }, selectLoc: function (ind) { _t.dataContext.currentIndex = ind; _t.commands.loadCurrent(); if (_t.showNextHours) _t.commands.loadNextHours(); else _t.dataContext.nextHours = null; if (_t.showNextDays) _t.commands.loadNextDays(); else _t.dataContext.nextDays = null; }, toggleTempUnit: function () { if (_t._tempUnit == 'C') _t._tempUnit = 'F'; else _t._tempUnit = 'C'; _t.triggers.tempUnitChange(); }, setTempUnitC: function () { _t._tempUnit = 'C'; _t.triggers.tempUnitChange(); }, setTempUnitF: function () { _t._tempUnit = 'F'; _t.triggers.tempUnitChange(); }, getTempUnit: function () { return _t._tempUnit; }, currentIconLg: function () { var rex = /{icon-id}/g; return _t._lgIconUrlBase.replace(rex, _t.dataContext.current.weather[0].icon); }, currentIconSm: function () { var rex = /{icon-id}/g; return _t._smIconUrlBase.replace(rex, _t.dataContext.current.weather[0].icon); }, currentTemp: function () { if (_t.dataContext.current) return convertTemp(_t.dataContext.current.main.temp); }, currentWindSpeed: function () { if (_t.dataContext.current) return Math.round10(windSpeedMPSToKmPH(_t.dataContext.current.wind.speed), -2); }, currentCod: function () { return _t.dataContext.current.weather[0].id; }, currentCodText: function () { return _t.dataContext.cods["cod_" + _t.dataContext.current.weather[0].id]; }, isTempUnit: function (tu) { if (!tu) return false; if (_t._tempUnit.toLowerCase() == tu.toLowerCase()) return true; return false; }, isSettingLoading: function () { return _t.settingLoading; }, isCurrentLoading: function () { return _t.currentLoading; }, isNextDaysLoading: function () { return _t.nextDaysLoading; }, isNextHoursLoading: function () { return _t.nextHoursLoading; }, isLoading: function () { return _t.settingLoading || _t.currentLoading || _t.nextDaysLoading || _t.nextHoursLoading; } } // // triggers this.triggers = { locationLoaded: function (funcs) { var $dom = $(element); $(_t.getUIElement('wc-locations', 'select')).each(function () { var $t = $(this); var html = ''; if (_t.dataContext.locations) { for (var i = 0; i < _t.dataContext.locations.length; i++) html += ''; } $t.html(html); }); if (funcs) _t.executeCommandScript(funcs); _t.updateUI("locationLoaded"); }, currentWeatherLoaded: function () { var $dom = $(element); var icon = _t.commands.currentIconLg(); $(_t.getUIElement('wc-current-icon-large', 'img')).each(function () { this.setAttribute('src', icon); }); icon = _t.commands.currentIconSm(); $(_t.getUIElement('wc-current-icon-small', 'img')).each(function () { this.setAttribute('src', icon); }); _t.updateUI("currentWeatherLoaded"); }, nextHoursLoaded: function () { var nh = $(_t.getUIElement('wc-next-hours')); if (nh.length > 0) { nh.each(function () { var $t = $(this); $t.html(''); }); if (_t.dataContext.nextHours) { if (_t.dataContext.nextHours.list instanceof Array) { for (var i = 0; i < _t.dataContext.nextHours.list.length; i++) { var item = _t.dataContext.nextHours.list[i]; nh.each(function () { var iuis = _t.createItemUI(item, "wc-next-hours-item", _t.hourItemTransformation); $(this).append(iuis); Tandan.Portal.WeatherControl.executeItemDirective(_t, iuis[0], item, _t.hourItemTransformation); Tandan.Portal.WeatherControl.executeDirective(_t, iuis[0]); }); } } else { var c = 0; for (var i in _t.dataContext.nextHours.list) { var item = _t.dataContext.nextHours.list[i]; nh.each(function () { var iuis = _t.createItemUI(item, "wc-next-hours-item", _t.hourItemTransformation); $(this).append(iuis); Tandan.Portal.WeatherControl.executeItemDirective(_t, iuis[0], item, _t.hourItemTransformation); Tandan.Portal.WeatherControl.executeDirective(_t, iuis[0]); }); c++; if (c == _t._cntHours) break; } } } } _t.updateUI("nextHoursLoaded"); }, nextDaysLoaded: function () { var nh = $(_t.getUIElement('wc-next-days')); if (nh.length > 0) { nh.each(function () { var $t = $(this); $t.html(''); }); if (_t.dataContext.nextDays) { if (_t.dataContext.nextDays.list instanceof Array) { for (var i = 0; i < _t.dataContext.nextDays.list.length; i++) { var item = _t.dataContext.nextDays.list[i]; nh.each(function () { var iuis = _t.createItemUI(item, "wc-next-days-item", _t.dayItemTransfomation); Tandan.Portal.WeatherControl.executeItemDirective(_t, iuis[0], item, _t.dayItemTransfomation); Tandan.Portal.WeatherControl.executeDirective(_t, iuis[0]); $(this).append(iuis); }); } } else { var c = 0; for (var i in _t.dataContext.nextDays.list) { var item = _t.dataContext.nextDays.list[i]; nh.each(function () { var iuis = _t.createItemUI(item, "wc-next-days-item", _t.dayItemTransfomation); Tandan.Portal.WeatherControl.executeItemDirective(_t, iuis[0], item, _t.dayItemTransfomation); Tandan.Portal.WeatherControl.executeDirective(_t, iuis[0]); $(this).append(iuis); }); c++; if (c == _t._cntDays) break; } } } } _t.updateUI("nextDaysLoaded"); $(window).trigger('resize'); }, nextHoursShowChanged: function () { _t.updateUI("nextHoursShowChanged"); }, nextDaysShowChanged: function () { _t.updateUI("nextDaysShowChanged"); }, tempUnitChange: function () { _t.triggers.nextHoursLoaded(); _t.triggers.nextDaysLoaded(); _t.updateUI("tempUnitChange"); }, settingLoading: function () { _t.updateUI("setting-loading"); _t.triggers.loading(); }, currentLoading: function () { _t.updateUI("current-loading"); _t.triggers.loading(); }, nextDaysLoading: function () { _t.updateUI("next-days-loading"); _t.triggers.loading(); }, nextHoursLoading: function () { _t.updateUI("next-hours-loading"); _t.triggers.loading(); }, loading: function () { _t.updateUI("loading"); } } this.hourItemTransformation = { "main.temp": function (item) { return convertTemp(item.main.temp); }, "weather.icon": function (item) { return _t.ajustSmallIconUrl(item.weather[0].icon); }, "date": function (item) { var d = parseUnixTime(item.dt); var res = _t._dateFormat; res = res.replace(/dd/g, d.getDate()).replace(/MM/g, d.getMonth() + 1).replace(/yyyy/g, d.getFullYear()); return res; }, "time": function (item) { var d = parseUnixTime(item.dt); return d.getHours() + ':' + d.getMinutes(); }, "isNight": function (item) { var d = parseUnixTime(item.dt); return d.getHours() > 18 || d.getHours() < 6; } } this.dayItemTransfomation = { "temp.day": function (item) { return convertTemp(item.temp.day); }, "temp.min": function (item) { return convertTemp(item.temp.min); }, "temp.max": function (item) { return convertTemp(item.temp.max); }, "temp.morn": function (item) { return convertTemp(item.temp.morn); }, "temp.eve": function (item) { return convertTemp(item.temp.eve); }, "temp.night": function (item) { return convertTemp(item.temp.night); }, "weather.icon": function (item) { return _t.ajustSmallIconUrl(item.weather[0].icon); }, "date": function (item) { var d = parseUnixTime(item.dt); var res = _t._dateFormat; res = res.replace(/dd/g, d.getDate()).replace(/MM/g, d.getMonth() + 1).replace(/yyyy/g, d.getFullYear()); return res; }, "time": function (item) { var d = parseUnixTime(item.dt); return d.getHours() + ':' + d.getMinutes(); } } this.eval = function (item, exp, transform) { if (transform && transform[exp]) return transform[exp](item); else return eval('[item.' + exp + ']')[0]; } this.getUIElement = function (uiid, tag) { //return $(element).find((tag || '') + '[wc-ui="' + uiid + '"]'); return _t.__ui_elements[uiid]; } this.createItemUI = function (item, template, transfomer) { return $(_t.__templates[template]); } this.ajustSmallIconUrl = function (id) { var rex = /{icon-id}/g; return _t._smIconUrlBase.replace(rex, id); } this.ajustLargeIconUrl = function (id) { var rex = /{icon-id}/g; return _t._lgIconUrlBase.replace(rex, id); } this.executeCommand = function (cmd, context, transform) { var func = null; try { if (context && transform) { func = eval('[(function(){return transform["' + cmd + '"](context);})]')[0]; } else func = eval('[(function(){return _t.commands.' + cmd + ';})]')[0]; } catch (e) { return; } return (func)(); } this.executeCommandScript = function (funcs) { if (funcs) { funcs = funcs.split(';'); for (var i = 0; i < funcs.length; i++) { if (funcs[i] != '') _t.executeCommand(funcs[i]); } } } this.trigger = function (cmd) { var func = null; try { func = eval('[(function(){_t.triggers.' + cmd + ';})]')[0]; } catch (e) { return; } (func)(); } this.updateUI = function (trigger) { var tg = _t.__trigger_map[trigger]; if (tg) for (var i = 0; i < tg.length; i++) _t.updateUIElement(_t.__trigger_map[trigger][i], trigger); } this.updateUIElement = function (ele, trigger) { Tandan.Portal.WeatherControl.executeDirective(this, ele); } function convertTemp(k) { var c = k - 273.15; if (_t._tempUnit.toLowerCase() == 'f') return Math.round(c * 1.8000 + 32.00); return Math.round(c); } function convertUTCDateToLocalDate(date) { var newDate = new Date(date.getTime() + date.getTimezoneOffset() * 60 * 1000); var offset = date.getTimezoneOffset() / 60; var hours = date.getHours(); newDate.setHours(hours - offset); return newDate; } function parseUnixTime(unixT) { return new Date(unixT * 1000); } function windSpeedMPSToKmPH(val) { return val + 3.6; } function decimalAdjust(type, value, exp) { // If the exp is undefined or zero... if (typeof exp === 'undefined' || +exp === 0) { return Math[type](value); } value = +value; exp = +exp; // If the value is not a number or the exp is not an integer... if (isNaN(value) || !(typeof exp === 'number' && exp % 1 === 0)) { return NaN; } // Shift value = value.toString().split('e'); value = Math[type](+(value[0] + 'e' + (value[1] ? (+value[1] - exp) : -exp))); // Shift back value = value.toString().split('e'); return +(value[0] + 'e' + (value[1] ? (+value[1] + exp) : exp)); } // Decimal round if (!Math.round10) { Math.round10 = function (value, exp) { return decimalAdjust('round', value, exp); }; } }; Tandan.Portal.WeatherControl.prototype = { initialize: function () { var element = this.get_element(); var _t = this; var $dom = $(element); // ui this.__ui_elements = new Array(); $dom.find("[wc-ui]").each(function () { if (!_t.__ui_elements[this.getAttribute("wc-ui")]) _t.__ui_elements[this.getAttribute("wc-ui")] = new Array(); _t.__ui_elements[this.getAttribute("wc-ui")].push(this); }); $(_t.getUIElement('wc-locations', 'select')).change(function () { var ind = this.selectedIndex; _t.commands.selectLoc(ind); $(_t.getUIElement('wc-locations', 'select')).each(function () { this.selectedIndex = ind; }); }); $dom.find('[wc-click]').each(function () { var wcClick = this.getAttribute('wc-click'); $(this).click(function () { _t.executeCommandScript(wcClick); }).removeAttr('wc-click'); }); //trigger this.__trigger_map = new Array(); $dom.find('[wc-trigger]').each(function () { var tg = this.getAttribute('wc-trigger'); $(this).removeAttr('wc-trigger'); if (!tg) return; tg = tg.split(','); for (var i = 0; i < tg.length; i++) { var key = $.trim(tg[i]); if (key != "") { if (!_t.__trigger_map[key]) { _t.__trigger_map[key] = new Array(); } _t.__trigger_map[key].push(this); } } }); //templates this.__templates = new Array(); $dom.find('[wc-template]').each(function () { var tn = this.getAttribute("wc-template"); if (!tn) return; if ($.trim(tn) == '') return; _t.__templates[tn] = $(this).html(); $(this).remove(); }); // init control var initFuncs = element.getAttribute('wc-init'); $dom.removeAttr('wc-init'); _t.executeCommandScript(initFuncs); _t.commands.setTempUnitC(); _t.triggers.nextHoursShowChanged(); _t.triggers.nextDaysShowChanged(); }, dispose: function () { Tandan.Portal.WeatherControl.callBaseMethod(this, "dispose"); } // Name Property Accessors , set_Locations: function (value) { this._locs = value }, get_Locations: function () { return this._locs; }, set_LocationUrl: function (value) { this._settingsUrl = value }, get_LocationUrl: function () { return this._settingsUrl; }, set_LargeIconUrlBase: function (value) { this._lgIconUrlBase = value }, get_LargeIconUrlBase: function () { return this._lgIconUrlBase; }, set_SmallIconUrlBase: function (value) { this._smIconUrlBase = value }, get_SmallIconUrlBase: function () { return this._smIconUrlBase; }, set_HourItemCount: function (value) { this._cntHours = value }, get_HourItemCount: function () { return this._cntHours; }, set_DayItemCount: function (value) { this._cntDays = value }, get_DayItemCount: function () { return this._cntDays; }, set_TemperatureUnit: function (value) { this._tempUnit = value }, get_TemperatureUnit: function () { return this._tempUnit; } }; Tandan.Portal.WeatherControl._directives = []; Tandan.Portal.WeatherControl._item_directives = []; Tandan.Portal.WeatherControl.directive = function (key, func) { Tandan.Portal.WeatherControl._directives[key] = func; } Tandan.Portal.WeatherControl.itemDirective = function (key, func) { Tandan.Portal.WeatherControl._item_directives[key] = func; } Tandan.Portal.WeatherControl.executeDirective = function (ctrl, element) { var $ele = $(element); for (var i in Tandan.Portal.WeatherControl._directives) { if ($ele.is('[' + i + ']')) Tandan.Portal.WeatherControl._directives[i](ctrl, element, element.getAttribute(i)); $ele.find('[' + i + ']').each(function () { Tandan.Portal.WeatherControl._directives[i](ctrl, this, this.getAttribute(i)); }); } } Tandan.Portal.WeatherControl.executeItemDirective = function (ctrl, element,dataItem,transform) { var $ele = $(element); for (var i in Tandan.Portal.WeatherControl._item_directives) { if ($ele.is('[' + i + ']')) Tandan.Portal.WeatherControl._item_directives[i](ctrl, element, element.getAttribute(i), dataItem, transform); $ele.find('[' + i + ']').each(function () { Tandan.Portal.WeatherControl._item_directives[i](ctrl, this, this.getAttribute(i), dataItem, transform); }); } } Tandan.Portal.WeatherControl.directive("wc-class", function (ctrl, matchElement, attr) { if (!attr) return; var $ele = $(matchElement); var qindex = attr.indexOf('?'); if (qindex == -1) $ele.addClass(ctrl.executeCommand(attr)); else { var cmd = attr.substring(0, qindex); var r = attr.substring(qindex + 1); var bl = r.split(":"); var res = ctrl.executeCommand(cmd); if (res) { if (bl[1] && bl[1] != '') $ele.removeClass(bl[1]); $ele.addClass(bl[0]); } else { if (bl[0] && bl[0] != '') $ele.removeClass(bl[0]); if (bl[1] && bl[1] != '') $ele.addClass(bl[1]); } } }); Tandan.Portal.WeatherControl.directive("wc-bind-cmd", function (ctrl, matchElement, attr) { if (matchElement.tagName == "INPUT") matchElement.value = ctrl.executeCommand(attr); else $(matchElement).text(ctrl.executeCommand(attr)); }); Tandan.Portal.WeatherControl.directive("wc-bind", function (ctrl, matchElement, attr) { if (matchElement.tagName == "INPUT") matchElement.value = eval("[ctrl."+attr+"]")[0]; else $(matchElement).text(eval("[ctrl."+attr+"]")[0]); }); Tandan.Portal.WeatherControl.directive('wc-enable', function (ctrl, matchElement, attr) { var res = ctrl.executeCommand(attr); if (res) $(matchElement).removeAttr('disabled'); else $(matchElement).attr('disabled', 'disabled'); }); Tandan.Portal.WeatherControl.directive('wc-disable', function (ctrl, matchElement, attr) { var res = ctrl.executeCommand(attr); if (res)$(matchElement).attr('disabled', 'disabled'); else $(matchElement).removeAttr('disabled'); }); Tandan.Portal.WeatherControl.itemDirective("wc-item-class", function (ctrl, matchElement, attr, dataContext, transform) { if (!attr) return; var $ele = $(matchElement); var qindex = attr.indexOf('?'); if (qindex == -1) $ele.addClass(ctrl.eval(dataContext, attr, transform)); else { var cmd = attr.substring(0, qindex); var r = attr.substring(qindex + 1); var bl = r.split(":"); var res = ctrl.eval(dataContext, cmd, transform); if (res) { if (bl[1] && bl[1] != '') $ele.removeClass(bl[1]); $ele.addClass(bl[0]); } else { if (bl[0] && bl[0] != '') $ele.removeClass(bl[0]); if (bl[1] && bl[1] != '') $ele.addClass(bl[1]); } } }); Tandan.Portal.WeatherControl.itemDirective("wc-item-bind", function (ctrl, matchElement, attr, dataContext, transform) { if (matchElement.tagName == "INPUT") matchElement.value = ctrl.eval(dataContext,attr,transform); else $(matchElement).text(ctrl.eval(dataContext, attr, transform)); }); Tandan.Portal.WeatherControl.itemDirective("wc-item-bind-attr", function (ctrl, matchElement, attr, dataContext, transform) { var arr = attr.split(':'); $(matchElement).attr($.trim(arr[0]), ctrl.eval(dataContext, arr[1], transform)).removeAttr('wc-bind-item-attr'); }); Tandan.Portal.WeatherControl.itemDirective('wc-item-enable', function (ctrl, matchElement, attr, dataContext, transform) { var res = ctrl.eval(dataContext, attr, transform); if (res) $(matchElement).removeAttr('disabled'); else $(matchElement).attr('disabled', 'disabled'); }); Tandan.Portal.WeatherControl.itemDirective('wc-item-disable', function (ctrl, matchElement, attr, dataContext, transform) { var res = ctrl.eval(dataContext, attr, transform); if (res) $(matchElement).attr('disabled', 'disabled'); else $(matchElement).removeAttr('disabled'); }); Tandan.Portal.WeatherControl.instances = []; Tandan.Portal.WeatherControl.registerClass("Tandan.Portal.WeatherControl", Sys.UI.Control); if (typeof (Sys) !== "undefined") Sys.Application.notifyScriptLoaded();