Skip to content

Commit 5b0476b

Browse files
committed
arrange indents and so on...
1 parent 6902193 commit 5b0476b

10 files changed

+453
-191
lines changed

backbone/indexed_collection.js

+11-8
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ LIMO.namespace('LIMO.backbone')(function (ns) {
55
*/
66
ns.IndexedCollection = Backbone.Collection.extend({
77

8-
initialize:function () {
9-
this.index = {} // {uid to user}
8+
initialize: function () {
9+
this.index = {}; // {uid to user}
1010
},
11-
getKey:function (model) {
11+
getKey: function (model) {
1212
throw new Error('shoud define getKey() as return model.get("key")');
1313
},
1414

15-
add:function (models, options) {
15+
add: function (models, options) {
1616

1717
var self = this;
1818
var ret = Backbone.Collection.prototype.add.call(this, models, options);
@@ -28,7 +28,7 @@ LIMO.namespace('LIMO.backbone')(function (ns) {
2828

2929
return ret;
3030
},
31-
remove:function (models, options) {
31+
remove: function (models, options) {
3232
var self = this;
3333
if (_.isArray(models)) {
3434
//is collection
@@ -42,12 +42,15 @@ LIMO.namespace('LIMO.backbone')(function (ns) {
4242

4343
return Backbone.Collection.prototype.remove.call(this, models, options);
4444
},
45-
reset:function (models, options) {
45+
reset: function (models, options) {
4646
this.index = {};
4747
return Backbone.Collection.prototype.reset.call(this, models, options);
4848
},
49-
findById:function (uid) {
50-
return this.index[uid];
49+
findById: function (key) {
50+
return this.index[key];
51+
},
52+
findByKey: function (key) {
53+
return this.index[key];
5154
}
5255

5356
});

device.js

+45-22
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,70 @@
1-
LIMO.namespace('LIMO')(function(ns){
1+
LIMO.namespace('LIMO')(function (ns) {
22

33
ns.Device = LIMO.extend(Object, {
4-
5-
isIPhone: function(){
4+
5+
isIPhone: function () {
66
return navigator.userAgent.indexOf('(iPhone;') != -1;
77
},
8-
9-
isIPad: function(){
8+
9+
getIPhoneVersion: function(){
10+
if(!this.isIPhone()){
11+
throw new Error('is not iphone device');
12+
}
13+
14+
var m = navigator.userAgent.match(/iPhone OS ([0-9]+)_/);
15+
return m[1];
16+
},
17+
18+
isIPad: function () {
1019
return navigator.userAgent.indexOf('(iPad;') != -1;
1120
},
12-
13-
isIPod: function(){
21+
22+
isIPod: function () {
1423
return navigator.userAgent.indexOf('(iPod;') != -1;
1524
},
16-
17-
isAndroid: function(){
25+
26+
isAndroid: function () {
1827
return navigator.userAgent.indexOf('Android') != -1;
1928
},
20-
21-
isPc: function(){
29+
30+
isTizen: function(){
31+
return navigator.userAgent.indexOf('Tizen') != -1;
32+
},
33+
34+
isPc: function () {
2235
if (!this.isMobileSize() && !this.isIPad() && !this.isAndroid()) {
2336
return true;
2437
}
2538
return false;
2639
},
27-
28-
getWidth: function(){
40+
41+
hasEvent: function(name){
42+
return (function () {
43+
return (name in this);
44+
})();
45+
},
46+
47+
isTouch: function () {
48+
return this.hasEvent('ontouchstart');
49+
},
50+
51+
getWidth: function () {
2952
return screen.width;
3053
},
31-
32-
getHeight: function(){
54+
55+
getHeight: function () {
3356
return screen.height;
3457
},
35-
36-
isLandscape: function(){
58+
59+
isLandscape: function () {
3760
return Math.abs(window.orientation) === 90;
3861
},
39-
40-
isMobileSize: function(){
41-
return this.getWidth() <= 320 && this.getHeight() <= 480;
62+
63+
isMobileSize: function () {
64+
return this.getWidth() <= 640 && this.getHeight() <= 1136;
4265
}
4366
});
44-
45-
67+
68+
4669
ns.device = new ns.Device();
4770
});

lang.js

+38-14
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
LIMO.namespace = function () {
6464

6565
var ctx = {
66-
global:global
66+
global: global
6767
};
6868

6969
var ns = global;
@@ -396,7 +396,28 @@
396396
LIMO.puts(child, arg);
397397
}
398398
return child;
399-
}
399+
};
400+
401+
LIMO.getLanguage = function () {
402+
try {
403+
return (navigator.browserLanguage || navigator.language || navigator.userLanguage).substr(0, 2);
404+
}
405+
catch (e) {
406+
return undefined;
407+
}
408+
};
409+
410+
var locales = {
411+
ja: 'ja_JP',
412+
en: 'en_US'
413+
};
414+
415+
LIMO.getLocale = function () {
416+
var lang = LIMO.getLanguage();
417+
var locale = locales[lang];
418+
return locale || 'en_US';
419+
};
420+
400421
///////////////////////////////////////////////////////////
401422
LIMO.namespace('LIMO.util')(function (ns) {
402423

@@ -419,22 +440,22 @@
419440

420441
var methods = {
421442

422-
hasEvent:function (eventName) {
443+
hasEvent: function (eventName) {
423444
var ev = events[eventName];
424445
return (ev !== undefined);
425446
},
426447
/**
427448
* @param {Object} eventName
428449
* @param {Object} option
429450
*/
430-
addEvent:function (eventName, option) {
451+
addEvent: function (eventName, option) {
431452
events[eventName] = option || true;
432453
},
433454
/**
434455
*
435456
* @param {Object} eventName
436457
*/
437-
fireEvent:function (eventName) {
458+
fireEvent: function (eventName) {
438459
checkEvent(eventName);
439460

440461
var ls = listeners[eventName];
@@ -449,7 +470,7 @@
449470
listener.apply(self, args);
450471
})
451472
},
452-
addListener:function (eventName, func) {
473+
addListener: function (eventName, func) {
453474
if (func === undefined || func === null) {
454475
throw new Error('func is not defined');
455476
}
@@ -463,15 +484,18 @@
463484
ls.push(func);
464485
}
465486
},
466-
clearListeners:function () {
487+
clearListeners: function () {
467488
listeners = {};
468489
}
469490
};
470491

471492
methods['on'] = methods.addListener;
493+
methods['trigger'] = methods.fireEvent;
472494

473495
LIMO.puts(this, methods);
474496
});
497+
498+
475499
});
476500
///////////////////////////////////////////////////////////
477501
LIMO.namespace('LIMO.util')(function (ns) {
@@ -484,15 +508,15 @@
484508

485509
LIMO.extend(ns.PropertyAppender, Object, {
486510

487-
getPropertyNameBody:function (name) {
511+
getPropertyNameBody: function (name) {
488512
return name.substr(0, 1).toUpperCase() + name.substr(1);
489513
},
490514

491-
getGetterName:function (name) {
515+
getGetterName: function (name) {
492516
return 'get' + this.getPropertyNameBody(name);
493517
},
494518

495-
getSetterName:function (name) {
519+
getSetterName: function (name) {
496520
return 'set' + this.getPropertyNameBody(name);
497521
},
498522

@@ -504,7 +528,7 @@
504528
* @param {Object} getterFunc getter(optional)
505529
* @param {Object} setterFunc setter(optional)
506530
*/
507-
add:function (target, propName, getterFunc, setterFunc) {
531+
add: function (target, propName, getterFunc, setterFunc) {
508532
var self = this;
509533
target = target || {};
510534

@@ -526,12 +550,12 @@
526550

527551
return this;
528552
},
529-
createGetter:function (target, name) {
553+
createGetter: function (target, name) {
530554
return function () {
531555
return target[name];
532556
};
533557
},
534-
createSetter:function (target, name) {
558+
createSetter: function (target, name) {
535559
return function (value) {
536560
target[name] = value;
537561
};
@@ -546,7 +570,7 @@
546570
return function (target, propName, getterFunc, setterFunc) {
547571
appender.add(target, propName, getterFunc, setterFunc);
548572
var handler = {
549-
add:function (pName, gFunc, sFunc) {
573+
add: function (pName, gFunc, sFunc) {
550574
appender.add(target, pName, gFunc, sFunc);
551575
return handler;
552576
}

request.js

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
LIMO.namespace('LIMO')(function (ns) {
2+
3+
function createError(jqXHR) {
4+
var header = jqXHR.getResponseHeader('Content-type');
5+
var err = new Error('Request Error');
6+
if (header.indexOf('json') != -1) {
7+
err.cause = JSON.parse(jqXHR.responseText);
8+
} else {
9+
err.message = jqXHR.responseText;
10+
}
11+
err.jqXHR = jqXHR;
12+
return err;
13+
}
14+
15+
/**
16+
* @param process promiseを返すFunction
17+
* @returns {*}
18+
*/
19+
ns.doRequest = function(process){
20+
var d = $.Deferred();
21+
var err;
22+
function doReq(process, count) {
23+
24+
return process().then(function (ret) {
25+
d.resolve(ret);
26+
}, function (jqXHR, textStatus) {
27+
28+
var statusCode = jqXHR.status;
29+
// null response is apply success
30+
if (statusCode == 200 && jqXHR.responseText == '') {
31+
d.resolve('');
32+
return;
33+
}
34+
35+
if (400 <= statusCode && statusCode < 500) {
36+
d.reject(createError(jqXHR));
37+
return;
38+
}
39+
40+
count += 1;
41+
if (count >= 3) {
42+
console.log('retry failed rejected');
43+
if (!err) {
44+
err = createError(jqXHR);
45+
}
46+
d.reject(err);
47+
return;
48+
}
49+
50+
setTimeout(function () {
51+
console.log('retry:' + count);
52+
doReq(process, count);
53+
}, 1000);
54+
});
55+
}
56+
57+
doReq(process, 0);
58+
59+
return d.promise();
60+
};
61+
62+
ns.request = function (options) {
63+
64+
if (options.cache === undefined) {
65+
options.cache = false;
66+
}
67+
68+
var process = function(){
69+
return $.ajax(options);
70+
};
71+
72+
return ns.doRequest(process);
73+
};
74+
75+
});

ui/dom.js

+23-1
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,27 @@ LIMO.namespace('LIMO.ui.dom')(function(ns){
1818
ns.remove = function(elm){
1919
elm.parentNode.removeChild(elm);
2020
}
21-
21+
22+
23+
ns.escapeHTML = function (val) {
24+
return $('<pre>').text(val).html();
25+
}
26+
27+
ns.unescapeHTML = function (val) {
28+
return $('<pre>').html(val).text();
29+
}
30+
31+
/**
32+
* 文字列の中に入っている不安要素をエスケープして、URL等は装飾する
33+
* @param text
34+
*/
35+
ns.safeHtml = function(text){
36+
text = ns.escapeHTML(text);
37+
text = text.replace(/\r?\n/g, ' <br>'); //半角スペースは次の処理でaタグを改行で区切ってやる為
38+
39+
//@see http://www.openspc2.org/JavaScript/Ajax/jQuery_plugin/chapter8/
40+
text = text.replace(/(https?:\/\/[\x21-\x7e]+)/gi, '<a target="_blank" href="$1">$1</a>');
41+
return text;
42+
}
43+
2244
});

0 commit comments

Comments
 (0)