Skip to content
This repository has been archived by the owner on Aug 26, 2024. It is now read-only.

Commit

Permalink
refs opencomponents#436 Fixed oc-client
Browse files Browse the repository at this point in the history
  • Loading branch information
elboletaire committed Apr 7, 2017
1 parent 87d3cde commit 0fad4df
Showing 1 changed file with 37 additions and 36 deletions.
73 changes: 37 additions & 36 deletions src/components/oc-client/src/oc-client.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use strict';
/* eslint no-var: 'off' */

var oc = oc || {};

Expand Down Expand Up @@ -29,8 +30,8 @@ var oc = oc || {};
oc.status = 'loading';
}

// Constants
let CDNJS_BASEURL = 'https://cdnjs.cloudflare.com/ajax/libs/',
// varants
var CDNJS_BASEURL = 'https://cdnjs.cloudflare.com/ajax/libs/',
IE9_AJAX_POLYFILL_URL = CDNJS_BASEURL + 'jquery-ajaxtransport-xdomainrequest/1.0.3/jquery.xdomainrequest.min.js',
JQUERY_URL = CDNJS_BASEURL + 'jquery/1.11.2/jquery.min.js',
RETRY_INTERVAL = oc.conf.retryInterval || 5000,
Expand All @@ -51,7 +52,7 @@ var oc = oc || {};
MESSAGES_RETRIEVING = 'Unrendered component found. Trying to retrieve it...';

// The code
let debug = oc.conf.debug || false,
var debug = oc.conf.debug || false,
noop = function(){},
nav = $window.navigator.userAgent,
is9 = !!(nav.match(/MSIE 9/)),
Expand All @@ -60,7 +61,7 @@ var oc = oc || {};
retries = {},
isBool = function(a){ return typeof(a) === 'boolean'; };

const logger = {
var logger = {
error: function(msg){
return console.log(msg);
},
Expand All @@ -69,7 +70,7 @@ var oc = oc || {};
}
};

const registeredTemplates = {
var registeredTemplates = {
'oc-template-handlebars': {
externals: [
{ global: 'Handlebars', url: 'https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.0.6/handlebars.runtime.min.js' }
Expand Down Expand Up @@ -97,7 +98,7 @@ var oc = oc || {};
registerTemplates(oc.conf.templates);
}

const retry = function(component, cb, failedRetryCb){
var retry = function(component, cb, failedRetryCb){
if(retries[component] === undefined){
retries[component] = RETRY_LIMIT;
}
Expand All @@ -112,9 +113,9 @@ var oc = oc || {};
retries[component]--;
};

const addParametersToHref = function (href, parameters) {
var addParametersToHref = function (href, parameters) {
if(href && parameters) {
const param = oc.$.param(parameters);
var param = oc.$.param(parameters);
if(href.indexOf('?') > -1) {
return href + '&' + param;
} else {
Expand Down Expand Up @@ -143,14 +144,14 @@ var oc = oc || {};
nameSpace = [nameSpace];
}

const needsToBeLoaded = function(){
let base = $window;
var needsToBeLoaded = function(){
var base = $window;

if(typeof(nameSpace) === 'undefined'){
return true;
}

for(let i = 0; i < nameSpace.length; i++){
for(var i = 0; i < nameSpace.length; i++){
base = base[nameSpace[i]];
if(!base){
return true;
Expand All @@ -160,14 +161,14 @@ var oc = oc || {};
return false;
};

const getObj = function(){
let base = $window;
var getObj = function(){
var base = $window;

if(typeof(nameSpace) === 'undefined'){
return undefined;
}

for(let i = 0; i < nameSpace.length; i++){
for(var i = 0; i < nameSpace.length; i++){
base = base[nameSpace[i]];
if(!base){
return undefined;
Expand All @@ -187,7 +188,7 @@ var oc = oc || {};
};


const processHtml = function($component, data, callback){
var processHtml = function($component, data, callback){

data.id = Math.floor(Math.random()*9999999999);

Expand Down Expand Up @@ -220,7 +221,7 @@ var oc = oc || {};
throw MESSAGES_ERRORS_NAME_PARAMETER_IS_REQUIRED;
}

const withFinalSlash = function(s){
var withFinalSlash = function(s){
s = s || '';

if(s.slice(-1) !== '/'){
Expand All @@ -230,15 +231,15 @@ var oc = oc || {};
return s;
};

let href = withFinalSlash(options.baseUrl) + withFinalSlash(options.name);
var href = withFinalSlash(options.baseUrl) + withFinalSlash(options.name);

if(options.version){
href += withFinalSlash(options.version);
}

if(options.parameters){
href += '?';
for(const parameter in options.parameters){
for(var parameter in options.parameters){
if(options.parameters.hasOwnProperty(parameter)){
href += parameter + '=' + options.parameters[parameter] + '&';
}
Expand All @@ -261,21 +262,21 @@ var oc = oc || {};

initialising = true;

const requirePolyfills = function($, cb){
var requirePolyfills = function($, cb){
if(is9 && !$.IE_POLYFILL_LOADED){
oc.require(IE9_AJAX_POLYFILL_URL, cb);
} else {
cb();
}
};

const done = function(){
var done = function(){
initialised = true;
initialising = false;

oc.events = (function(){

const obj = oc.$({});
var obj = oc.$({});

return {
fire: function(key, data){
Expand All @@ -295,7 +296,7 @@ var oc = oc || {};
oc.events.fire('oc:ready', oc);
oc.status = 'ready';

for(let i = 0; i < oc.cmd.length; i++){
for(var i = 0; i < oc.cmd.length; i++){
oc.cmd[i](oc);
}

Expand All @@ -304,7 +305,7 @@ var oc = oc || {};
};
};

const wasDollarThereAlready = !!$window.$;
var wasDollarThereAlready = !!$window.$;
oc.require('jQuery', JQUERY_URL, function(jQuery){

requirePolyfills(jQuery, function(){
Expand All @@ -325,27 +326,27 @@ var oc = oc || {};

oc.render = function(compiledViewInfo, model, callback){
oc.ready(function(){
let type = compiledViewInfo.type;
var type = compiledViewInfo.type;
if (type === 'jade') { type = 'oc-template-jade'; }
if (type === 'handlebars') { type = 'oc-template-handlebars'; }
const template = registeredTemplates[type];
var template = registeredTemplates[type];

if(template){
oc.require(['oc', 'components', compiledViewInfo.key], compiledViewInfo.src, function(compiledView){
if(!compiledView){
callback(MESSAGES_ERRORS_LOADING_COMPILED_VIEW.replace('{0}', compiledViewInfo.src));
} else {

const externals = template.externals;
let externalsRequired = 0;
var externals = template.externals;
var externalsRequired = 0;

externals.forEach(function(library, _index, externals){
oc.require(library.global, library.url, function(){
externalsRequired++;
if(externalsRequired === externals.length) {
if(type === 'oc-template-handlebars'){
try {
const linked = $window.Handlebars.template(compiledView, []);
var linked = $window.Handlebars.template(compiledView, []);
callback(null, linked(model));
} catch(e){
callback(e.toString());
Expand All @@ -366,7 +367,7 @@ var oc = oc || {};

oc.renderNestedComponent = function($component, callback){
oc.ready(function(){
let dataRendering = $component.attr('data-rendering'),
var dataRendering = $component.attr('data-rendering'),
dataRendered = $component.attr('data-rendered'),
isRendering = isBool(dataRendering) ? dataRendering : (dataRendering === 'true'),
isRendered = isBool(dataRendered) ? dataRendered : (dataRendered === 'true');
Expand All @@ -392,7 +393,7 @@ var oc = oc || {};
};

oc.renderByHref = function(href, retryNumberOrCallback, cb){
let callback = cb,
var callback = cb,
retryNumber = retryNumberOrCallback;

if(typeof retryNumberOrCallback === 'function') {
Expand All @@ -403,7 +404,7 @@ var oc = oc || {};
oc.ready(function(){

if(href !== ''){
let hrefWithCount = href;
var hrefWithCount = href;
if(RETRY_SEND_NUMBER) {
hrefWithCount = addParametersToHref(hrefWithCount, {
'__oc_Retry': retryNumber
Expand Down Expand Up @@ -435,7 +436,7 @@ var oc = oc || {};

if(apiResponse.html.indexOf('<' + OC_TAG) === 0){

let innerHtmlPlusEnding = apiResponse.html.slice(apiResponse.html.indexOf('>') + 1),
var innerHtmlPlusEnding = apiResponse.html.slice(apiResponse.html.indexOf('>') + 1),
innerHtml = innerHtmlPlusEnding.slice(0, innerHtmlPlusEnding.lastIndexOf('<'));

apiResponse.html = innerHtml;
Expand Down Expand Up @@ -464,18 +465,18 @@ var oc = oc || {};

oc.renderUnloadedComponents = function(){
oc.ready(function(){
let $unloadedComponents = oc.$(OC_TAG + '[data-rendered!=true]'),
var $unloadedComponents = oc.$(OC_TAG + '[data-rendered!=true]'),
toDo = $unloadedComponents.length;

const done = function(){
var done = function(){
toDo--;
if(!toDo){
oc.renderUnloadedComponents();
}
};

if(toDo > 0){
for(let i = 0; i < $unloadedComponents.length; i++){
for(var i = 0; i < $unloadedComponents.length; i++){
oc.renderNestedComponent(oc.$($unloadedComponents[i]), done);
}
}
Expand All @@ -490,7 +491,7 @@ var oc = oc || {};

if(oc.$(placeholder)){
oc.$(placeholder).html('<' + OC_TAG + ' href="' + href + '" />');
const newComponent = oc.$(OC_TAG, placeholder);
var newComponent = oc.$(OC_TAG, placeholder);
oc.renderNestedComponent(newComponent, function(){
callback(newComponent);
});
Expand Down

0 comments on commit 0fad4df

Please sign in to comment.