OSDN Git Service

pettanR version0.4.15
[pettanr/clientJs.git] / 0.4.x / javascripts / common.js
index 086afb0..744cd38 100644 (file)
@@ -1,10 +1,78 @@
 /*\r
  * pettanR common.js\r
- *   version 0.4.5\r
+ *   version 0.4.15\r
  * \r
  * author: itozyun\r
  */\r
 \r
+/*\r
+ * http://pettanr.sourceforge.jp/test/type.html
+ */\r
+       var Type = {\r
+               isObject : function(v) {\r
+                       return typeof v === 'object';\r
+               },\r
+               isFunction : function(v) {\r
+                       return typeof v === 'function';\r
+               },\r
+               isArray : function(v) {\r
+                       return Object.prototype.toString.call(v) === "[object Array]";\r
+               },\r
+               isBoolean : function(v) {\r
+                       return typeof v === 'boolean';\r
+               },\r
+               isString : function(v) {\r
+                       return typeof v === 'string';\r
+               },\r
+               isNumber : function(v) {\r
+                       return typeof v === 'number';\r
+               },\r
+               isFinite : function(v){\r
+                       return Type.isNumber(v) === true && isFinite(v);\r
+               },\r
+               isHTMLElement : function(v){\r
+                       if( 'HTMLElement' in window ){\r
+                               Type.isHTMLElement = function(v){\r
+                                       return v instanceof HTMLElement;\r
+                               }\r
+                       } else {\r
+                               Type.isHTMLElement = function(v) {\r
+                                       if( Type.isObject(v) === false ){\r
+                                               return false;\r
+                                       }\r
+                                       var r;\r
+                                       if(v && v.nodeType === 1){\r
+                                               try{\r
+                                                       r = v.cloneNode(false);\r
+                                               } catch(n) {\r
+                                                       return false;\r
+                                               }\r
+                                               if(r === v) return false;\r
+                                               try{\r
+                                                       r.nodeType = 9;\r
+                                                       return r.nodeType === 1;\r
+                                               } catch(n) {}\r
+                                               return true;\r
+                                       }\r
+                                       return false;\r
+                               }\r
+                       }\r
+                       return Type.isHTMLElement(v);\r
+               },\r
+               /*\r
+               isElementCollection : function(v) {\r
+                       return (Object.prototype.toString.call(v) === "[object HTMLCollection]");\r
+               },\r
+               */\r
+               isNull : function(v) {\r
+                       return v === null;\r
+               },\r
+               isUndefined : function(v) {\r
+                       return typeof v === 'undefined';\r
+               }\r
+       };\r
+\r
+\r
 var pettanr = ( function(){\r
        var     FUNCTION_ARRAY = [],\r
                URL = document.location.href.split( '#')[ 0],\r
@@ -22,7 +90,7 @@ var pettanr = ( function(){
                                        if( elm.length === 2){\r
                                                v = decodeURIComponent( elm[ 1]);\r
                                                if( '' + parseFloat( v) === v) v = parseFloat( v);\r
-                                               if( '' + parseInt( v) === '0' + v) v = parseInt( v);\r
+                                               if( '' + parseInt( v, 10 ) === '0' + v) v = parseInt( v, 10 );\r
                                                if( v === 'true') v = true;\r
                                                if( v === 'false') v = false;\r
                                                if( v === 'null') v = null;\r
@@ -40,7 +108,7 @@ var pettanr = ( function(){
                IS_DEBUG = typeof URL_PARAMS.debug === 'boolean' ? URL_PARAMS.debug : IS_LOCAL === true,\r
                jqWindow , jqDocument , jqBody;\r
        return {\r
-               version: '0.4.5',\r
+               version: '0.4.13',\r
                init: function(){\r
                        jqWindow = $( window);\r
                        jqDocument = $( document);\r
@@ -533,7 +601,15 @@ pettanr.util = ( function(){
                createIframe: function( id, callback){\r
                        var ua = pettanr.ua;\r
             var el = document.createElement( ua.isIE ? '<iframe name="' + id + '" frameborder="0" scrolling="no">' : 'iframe');\r
-            \r
+\r
+            if( ua.isIE){\r
+                               el.onreadystatechange = detect;\r
+            } else {\r
+               // iron(chrome) の場合、append の前に onload を指定しないと onload が呼ばれない\r
+               el.onload = onLoad;\r
+               //setTimeout( asynkCallback, 0 );\r
+                       }\r
+\r
                        document.body.appendChild( el);\r
             el.id = el.name = id;\r
             el.setAttribute( 'name', id);\r
@@ -544,20 +620,19 @@ pettanr.util = ( function(){
             \r
             window[id] = el;\r
 \r
-            if( ua.isIE){\r
-                               el.onreadystatechange = detect;\r
-            } else {\r
-               setTimeout( onLoad, 0);\r
-                       }\r
             function detect(){\r
-                if ( this.readyState == "complete") {\r
+                if ( this.readyState === "complete") {\r
                     this.onreadystatechange = new Function();\r
                     this.onreadystatechange = null;\r
-                    onLoad();\r
+                    setTimeout( asynkCallback, 0 );\r
                 }\r
-            }                  \r
+            }\r
                        function onLoad(){\r
-                               callback( el);\r
+                               el.onload = null;\r
+                               setTimeout( asynkCallback, 0 );\r
+                       }\r
+                       function asynkCallback(){\r
+                               callback( el );\r
                        }\r
                }\r
        }\r
@@ -695,23 +770,25 @@ pettanr.CONST = ( function(){
        return {\r
                PETTANR_ROOT_PATH:                      PETTANR_ROOT_PATH,\r
                URL_ORIGINAL_PICTURES_JSON: ( isLocal === true ? 'json\/' : PETTANR_ROOT_PATH) + 'original_pictures.json',\r
+               URL_RESOURCE_PICTURES_JSON: ( isLocal === true ? 'json\/' : PETTANR_ROOT_PATH) + 'resource_pictures.json',\r
+               URL_COMICS_JSON:                        ( isLocal === true ? 'json\/' : PETTANR_ROOT_PATH) + 'comics.json',\r
                URL_PANELS_JSON:                        ( isLocal === true ? 'json\/' : PETTANR_ROOT_PATH) + 'panels.json',\r
                NS_PETTANR_COMIC:                       'pettanr-comic',\r
                RESOURCE_PICTURE_PATH:          ( isLocal === true ? pettanr.ROOT_PATH : PETTANR_ROOT_PATH) + 'resource_pictures\/',\r
-               CREATE_COMIC_JS:                        ( isLocal === true ? 'js\/' : PETTANR_ROOT_PATH) + 'comics_new.js',\r
+               CREATE_COMIC_JS:                        isLocal === true ? 'js\/create_new_comic.js' : PETTANR_ROOT_PATH + 'comics\/new.js',\r
+               CREATE_PANEL_JS:                        isLocal === true ? 'js\/create_new_panel.js' : PETTANR_ROOT_PATH + 'panels\/new.js',\r
                UPLOAD_PICTURE_JS:                      isLocal === true ? 'js\/upload_picture.js' : PETTANR_ROOT_PATH + 'original_pictures\/new.js',\r
+               REGISTER_ARTIST_JS:                     isLocal === true ? 'js\/register_artist.js' : PETTANR_ROOT_PATH + 'artists\/new.js',\r
                JQUERY_URL:                                     isLocal === true ? 'javascripts\/jquery-1.6.2.min.js' : PETTANR_ROOT_PATH + 'assets\/jquery-162_min.js'\r
        }\r
 })();\r
 \r
-\r
 pettanr.view = ( function(){\r
        /* create iframe overlay for user console */\r
        var location = document.location.pathname,\r
                isWorkPage =    document.location.href.indexOf( 'http:\/\/localhost:3000\/' ) === 0 ||\r
                                                location.indexOf( pettanr.CONST.PETTANR_ROOT_PATH ) === 0 ||\r
-                                               location.indexOf( '\/work.html') !== -1 ||\r
-                                               location.indexOf( '\work.html') !== -1,\r
+                                               location.indexOf( '\/work.html') !== -1,\r
                deep =  location.indexOf( '\/diary\/') !== -1 ||\r
                                location.indexOf( '\/test\/') !== -1 ||\r
                                location.indexOf( '\/help\/') !== -1 ||\r
@@ -720,42 +797,26 @@ pettanr.view = ( function(){
                LoginUserNavi;\r
        \r
        if( isWorkPage === false){\r
-               LoginUserNavi = ( function(){\r
-                       var _body = document.body,\r
-                               _root = document.getElementsByTagName( 'html' )[ 0 ],\r
-                               filter,\r
-                               iframe, visible = false;\r
-                       return {\r
-                               show: function(){\r
-                                       document.location.href = [\r
-                                                       deep === true ? '../' : '',\r
-                                                       'work.html',\r
-                                                       typeof viewIndex === 'number' ? '?view=' + viewIndex : ''\r
-                                               ].join( '');\r
-                               },\r
-                               hide: function(){}\r
-                       }\r
-               })();\r
+               LoginUserNavi = {\r
+                       show: function(){\r
+                               var i = pettanr.util.getChildIndex( this.parentNode, this );\r
+                               document.location.href = [\r
+                                               deep === true ? '../' : '',\r
+                                               'work.html',\r
+                                               typeof i === 'number' ? '?view=' + i : ''\r
+                                       ].join( '');\r
+                               return false;\r
+                       },\r
+                       hide: function(){}\r
+               }\r
        } else {\r
-               LoginUserNavi = ( function(){\r
-                       return {\r
-                               show: function(){\r
-                                       var i = pettanr.util.getChildIndex( this.parentNode, this );\r
-                                       i !== -1 && pettanr.view.show( i );\r
-                                       return false;\r
-                               },\r
-                               hide: function(){}\r
-                       }\r
-               })();\r
-\r
-               if( pettanr.IS_IFRAME === true){\r
-                       var _nodes = document.getElementsByTagName( 'a'),\r
-                               _a, _href;\r
-                       for(var i=0, l = _nodes.length; i<l; i++){\r
-                               _a = _nodes[ i];\r
-                               _href = _a.href.split('?')[0].split('#')[0];\r
-                               if( pettanr.util.getAbsolutePath( _href) !== location) _a.target = '_parent';\r
-                       }\r
+               LoginUserNavi = {\r
+                       show: function(){\r
+                               var i = pettanr.util.getChildIndex( this.parentNode, this );\r
+                               i !== -1 && pettanr.view.show( i );\r
+                               return false;\r
+                       },\r
+                       hide: function(){}\r
                }\r
                \r
                if( pettanr.DEBUG === true){\r
@@ -811,10 +872,9 @@ pettanr.view = ( function(){
                        pettanr.DEBUG === true && ret.push( 'debug');\r
                        return ret;\r
                })(),\r
-               l = items.length,\r
                item;\r
        origin.href = '#';\r
-       for(var i=0; i<l; ++i){\r
+       for(var i=0, l = items.length; i<l; ++i){\r
                item = origin.cloneNode( true);\r
                item.appendChild( document.createTextNode( items[ i]) );\r
                item.onclick = LoginUserNavi.show;\r
@@ -859,21 +919,17 @@ pettanr.view = ( function(){
        \r
        pettanr.DEBUG === true && VIEW_ID_ARRAY.push( 'debug');\r
        \r
-       show();\r
+       open();\r
        \r
        function onWindowResize(){\r
                var _fn,\r
                        l = funcArray.length,\r
                        w = jqWindow.width(),\r
                        h = jqWindow.height();\r
-               //for( var i=0; i<l; ++i){\r
-               //      _fn = funcArray[ i];\r
-               //      _fn.onWindowResize && _fn.onWindowResize( w, h);\r
-               //}\r
                currentView && currentView.onWindowResize && currentView.onWindowResize( w, h);\r
                pettanr.overlay.currentID !== null && pettanr.overlay.onWindowResize( w, h);\r
        }\r
-       function show(){\r
+       function open( _option ){\r
                if( isWorkPage === false) return;\r
                \r
                var _elm = document.getElementById( currentID);\r
@@ -891,19 +947,31 @@ pettanr.view = ( function(){
                }\r
                \r
                if( !pettanr.view || pettanr.view.init !== undefined) return;\r
-               currentView = pettanr[ currentID];\r
-               if( !currentView) return;\r
-               typeof currentView.onOpen === 'function' && currentView.onOpen();\r
-               setTimeout( onWindowResize, 0);\r
+               typeof currentView.onClose === 'function' && currentView.onClose();\r
+               currentView = pettanr[ currentID ];\r
+               typeof currentView.onOpen === 'function' && currentView.onOpen( jqWindow.width(), jqWindow.height(), _option );\r
+               // setTimeout( onWindowResize, 0);\r
        }\r
        \r
        var AbstractBasicPane = function(){\r
-               this.UID = '', // element の ID と一致\r
-               this.onOpen = function(){};\r
-               this.onClose = function(){};\r
-               this.onPaneResize = function( _w, _h){};\r
-               this.MIN_WIDTH = 0;\r
-               this.MIN_HEIGHT = 0;\r
+               this.rootElement = null;\r
+               this.onOpen = function( _w, _h, _option ){};\r
+               this.onClose = function(){ return true; } // false の場合、close の拒否 \r
+               this.onPaneResize = function( _w, _h ){}\r
+               this.resize = function( _w, _h ){\r
+                       if( this.MIN_WIDTH > _w || this.MIN_HEIGHT > _h ){\r
+                               if( Type.isHTMLElement( this.rootElement ) === true ){\r
+                                       // 小さすぎる!、と表示\r
+                               }\r
+                               return;\r
+                       }\r
+                       this.onPaneResize( _w, _h );\r
+               }\r
+               this.MIN_WIDTH = 240;\r
+               this.MIN_HEIGHT = 240;\r
+       }\r
+       var AbstractApplication = function(){\r
+               this.prototype = new AbstractBasicPane();\r
        }\r
        \r
        return {\r
@@ -912,14 +980,15 @@ pettanr.view = ( function(){
                        jqWindow = pettanr.jqWindow();\r
                        jqWindow.resize( onWindowResize);\r
                        \r
-                       currentView = currentView || pettanr[ currentID];\r
-                       currentView && typeof currentView.onOpen === 'function' && currentView.onOpen();\r
+                       currentView = currentView || pettanr[ currentID ];\r
+                       var _option = {}; // urlパラメータ\r
+                       currentView && typeof currentView.onOpen === 'function' && currentView.onOpen( jqWindow.width(), jqWindow.height(), _option );\r
                        \r
-                       setTimeout( onWindowResize, 100);\r
+                       // setTimeout( onWindowResize, 100);\r
                        \r
                        delete pettanr.view.init;\r
                },\r
-               show: function( _viewID){\r
+               show: function( _viewID ){\r
                        if( typeof _viewID === 'number' && _viewID < VIEW_ID_ARRAY.length){\r
                                _viewID = VIEW_ID_ARRAY[ _viewID];\r
                        } else\r
@@ -932,20 +1001,27 @@ pettanr.view = ( function(){
                                if( pettanr.view[ key] === _viewID){\r
                                        if( currentID !== _viewID && document.getElementById( _viewID)){\r
                                                this.currentID = currentID = _viewID;\r
-                                               show();\r
+                                               open();\r
                                        }\r
                                        return;\r
                                }\r
                        }\r
                        alert( _viewID);\r
                },\r
-               resisterAsBasicPane: function( _basicPane){\r
+               registerAsBasicPane: function( _basicPane ){\r
                        if( pettanr.view.isBasicPaneInstance( _basicPane ) === true ) return;\r
                        _basicPane.prototype = new AbstractBasicPane();\r
                },\r
-               isBasicPaneInstance: function( _basicPane){\r
+               registerApplication: function( _application ){\r
+                       if( pettanr.view.isApplicationInstance( _application ) === true ) return;\r
+                       _application.prototype = new AbstractApplication();\r
+               },\r
+               isBasicPaneInstance: function( _basicPane ){\r
                        return  _basicPane instanceof AbstractBasicPane;\r
                },\r
+               isApplicationInstance: function( _application ){\r
+                       return _application instanceof AbstractApplication;\r
+               },\r
                currentID:      currentID,\r
                HOME:           HOME_ID,\r
                COMICS:         VIEW_ID_ARRAY[ 1],\r
@@ -989,9 +1065,11 @@ pettanr.overlay = ( function(){
                },\r
                show: function( _currentOverlay){\r
                        if( visible === true && currentOverlay === _currentOverlay) return;\r
+                       document.body.style.overflow = 'hidden';\r
                        jqConteiner.stop().css( {\r
                                filter:         '',\r
-                               opacity:        ''\r
+                               opacity:        '',\r
+                               top:            document.documentElement.scrollTop || document.body.scrollTop\r
                        }).fadeIn();\r
                        this.visible = visible = true;\r
                        currentOverlay = _currentOverlay;\r
@@ -1001,6 +1079,7 @@ pettanr.overlay = ( function(){
                hide: function(){\r
                        currentOverlay = null;\r
                        if( visible === false) return;\r
+                       document.body.style.overflow = '';\r
                        jqConteiner.stop().css( {\r
                                filter:         '',\r
                                opacity:        ''\r
@@ -1011,15 +1090,18 @@ pettanr.overlay = ( function(){
                visible: visible,\r
                currentID: null,\r
                onWindowResize: function( _windowW, _windowH){\r
-                       jqConteiner.css( { height:      _windowH});\r
+                       jqConteiner.css({\r
+                               height:         _windowH,\r
+                               top:            document.documentElement.scrollTop || document.body.scrollTop\r
+                       });\r
                        jqShadow.css( { height: _windowH});\r
                        windowW = _windowW;\r
                        windowH = _windowH;\r
                        // 先にeditorのcanvasを確定する。\r
                        currentOverlay && currentOverlay.onWindowResize && setTimeout( asyncResize, 0);\r
                },\r
-               resisterOverlay: function( _basicPane){\r
-                       pettanr.view.resisterAsBasicPane( _basicPane);\r
+               registerOverlay: function( _basicPane){\r
+                       var _api = pettanr.view.registerAsBasicPane( _basicPane);\r
                        // OverlayAPI\r
                        return {\r
                                show: function( _basicPane ){\r
@@ -1235,7 +1317,7 @@ pettanr.form = ( function(){
                                instance = this;\r
                                delete this.init;\r
                        },\r
-                       update: function( _value){\r
+                       value: function( _value){\r
                                if( _value !== undefined){\r
                                        elmA.innerHTML = _value;\r
                                        value = _value;\r
@@ -1245,6 +1327,7 @@ pettanr.form = ( function(){
                                        }\r
                                }\r
                                currentItem === instance && this.finish();\r
+                               return value;\r
                        },\r
                        start: function(){\r
                                onClick();\r
@@ -1261,7 +1344,7 @@ pettanr.form = ( function(){
                                \r
                                ON_UPDATE_FUNCTION && _newValue !== value && ON_UPDATE_FUNCTION( _newValue, value);\r
                                finish( instance);\r
-                               \r
+                               value = _newValue;\r
                                focus = false;\r
                        },\r
                        enabled: function(){\r
@@ -2192,7 +2275,7 @@ var VisualEffect = ( function(){
        var VisualEffectClass = function( ELM){\r
                var isHtmlElement;\r
                \r
-               function resisterAnime( _cssObject, _onComplete, _onEnterFrame, _time){\r
+               function registerAnime( _cssObject, _onComplete, _onEnterFrame, _time){\r
                        var _numFrames = Math.floor( _time / fpms);\r
                        startAnimation( ELM, _cssObject, _onComplete, _onEnterFrame, _numFrames);\r
                }\r
@@ -2207,7 +2290,7 @@ var VisualEffect = ( function(){
                        \r
                }\r
                \r
-               this.anime = resisterAnime;\r
+               this.anime = registerAnime;\r
                this.fadeIn = startFadeIn;\r
                this.fadeOut = startFadeOut;\r
                this.update = update;\r