001/*
002 * Copyright (c) 2009 The openGion Project.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
013 * either express or implied. See the License for the specific language
014 * governing permissions and limitations under the License.
015 */
016package org.opengion.plugin.column;
017
018import org.opengion.fukurou.util.Attributes;
019import org.opengion.fukurou.util.StringUtil;
020import org.opengion.fukurou.util.TagBuffer;
021import org.opengion.fukurou.util.XHTMLTag;
022import org.opengion.hayabusa.common.HybsSystem;
023import org.opengion.hayabusa.db.AbstractEditor;
024import org.opengion.hayabusa.db.CellEditor;
025import org.opengion.hayabusa.db.DBColumn;
026
027/**
028 * TEXTRICH エディターは、カラムのデータをリッチテキストで編集する場合に
029 * 使用するクラスです。
030 * サイズ指定はsize1,size2で高さ,幅がpxで設定されます。
031 * 初期値は250,600です。
032 *
033 * optionAttibutes属性にcleditorの設定が可能です。
034 * 詳細は下記ページを参照してください。
035 * http://www.premiumsoftware.net/cleditor/gettingstarted
036 *
037 * @og.rev 5.9.32.0 (2018/05/02) 新規作成
038 * @og.rev      5.10.1.0 (2018/06/29) クリアボタンとエラー画面からの戻る場合の対応
039 * @og.group データ編集
040 *
041 * @version  5
042 * @author   T.OTA
043 * @since    JDK5.0,
044 */
045public class Editor_RICHTEXT extends AbstractEditor {
046        //* このプログラムのVERSION文字列を設定します。   {@value} */
047        private static final String VERSION = "" ;
048
049        /**
050         * デフォルトコンストラクター。
051         * このコンストラクターで、基本オブジェクトを作成します。
052         *
053         *
054         */
055        public Editor_RICHTEXT() {
056        }
057
058        // デフォルトの値設定
059        private void defaultSet() {
060                size1 = "250";
061                size2 = "600";
062        }
063
064        /**
065         * コンストラクター。
066         *
067         * @param       clm     DBColumnオブジェクト
068         */
069        private Editor_RICHTEXT( final DBColumn clm ) {
070                super( clm );
071                String  disabled = clm.isWritable() ? null : "disabled" ;
072
073                // size に、"height,width" を指定できるように変更
074                // 2018/06/29 MODIFY size(ViewLength)のみ取得するように変更
075                // String param = StringUtil.nval( clm.getEditorParam(),clm.getViewLength() );
076                String param = clm.getViewLength();
077                if( param != null && param.length() != 0 ) {
078                        int st = param.indexOf( ',' );
079                        if( st > 0 ) {
080                                size1 = param.substring( 0, st );
081                                size2 = param.substring( st + 1);
082                        }else {
083                                defaultSet();
084                        }
085                }else {
086                        defaultSet();
087                }
088
089                attributes = new Attributes();
090                attributes.addAttributes( clm.getEditorAttributes() );
091                tagBuffer.add( XHTMLTag.textareaAttri( attributes ) );
092        }
093
094        /**
095         * 各オブジェクトから自分のインスタンスを返します。
096         * 自分自身をキャッシュするのか、新たに作成するのかは、各サブクラスの実装に
097         * まかされます。
098         *
099         * @param       clm     DBColumnオブジェクト
100         *
101         * @return      CellEditorオブジェクト
102         */
103        public CellEditor newInstance( final DBColumn clm ) {
104                return new Editor_RICHTEXT( clm );
105        }
106
107        /**
108         * データの編集用文字列を返します。
109         *
110         * @param   value 入力値
111         *
112         * @return  データの編集用文字列
113         */
114        @Override
115        public String getValue( final String value ) {
116                String id = "";
117
118                TagBuffer tag = new TagBuffer( "textarea" );
119                tag.add( "name"    , name );
120
121                id = attributes.get( "id" );
122                optAttr = attributes.get( "optionAttributes" );
123                if( id == null || id.length() == 0 ) {
124                        tag.add( "id"      , name );
125                        id = name;
126                }
127                tag.add( tagBuffer.makeTag() );
128                tag.add( optAttr );
129                tag.setBody( value );
130
131                return tag.makeTag() + createCLEditorSc(id);
132        }
133
134        /**
135         * name属性を変えた、データ表示/編集用のHTML文字列を作成します。
136         * テーブル上の name に 行番号を付加して、名前_行番号 で登録するキーを作成し,
137         * リクエスト情報を1つ毎のフィールドで処理できます。
138         *
139         * @param   row   行番号
140         * @param   value 入力値
141         *
142         * @return  データ表示/編集用の文字列
143         */
144        @Override
145        public String getValue( final int row,final String value ) {
146                String id = "";
147
148                TagBuffer tag = new TagBuffer( "textarea" );
149                String newName = name + HybsSystem.JOINT_STRING + row;
150                tag.add( "name"    , newName );
151                id = attributes.get( "id" );
152                if( id == null || id.length() == 0 ) {
153                        tag.add( "id"      , newName );
154                        id = newName;
155                }
156
157                tag.add( tagBuffer.makeTag() );
158                tag.add( optAttr );
159                tag.setBody( value );
160
161                return tag.makeTag( row,value ) + createCLEditorSc(id);
162        }
163
164        // CLEditorスクリプトの生成
165        private String createCLEditorSc(String id) {
166
167                StringBuilder js = new StringBuilder();
168                js.append("<script type='text/javascript'>");
169                js.append("var trg = $('#").append(id).append("').cleditor({");
170                js.append("bodyStyle:''");
171                js.append(",height:").append(size1);
172                js.append(",width:").append(size2);
173                js.append(",controls: 'bold size | color highlight | removeformat | link unlink | undo redo'");
174                String attr = attributes.get( "optionAttributes" );
175                if(attr != null && attr.length() > 0) {
176                        js.append(",").append(attr);
177                }
178                js.append("})[0];");
179                // editorをtextareaに反映(この処理で更新なしの場合も、><の文字ががエンコードされる。)
180                js.append("trg.updateTextArea();");
181                // readonly属性が設定されている場合は、変更不可。
182                if("readonly".equals(attributes.get("readonly"))) {
183                        js.append("trg.disable('true');");
184                        // linkは新規ウィンドウに表示
185                        js.append("$('#").append(id).append("').next('iframe').contents().find('a').attr('target','_blank');");
186                }
187                
188                // 2018/06/29 ADD START
189                // クリアボタン押下時の動作(textareaの値をiframeに反映して、初期状態に戻す)
190                // reset後に反映するために、setTimeoutで遅延
191                js.append("$('input[type=reset]').click(function(){setTimeout(function(){trg.updateFrame()},10);});");
192                // エラー画面からの戻る対応。textareaの値をiframeに反映する。
193                js.append("$(function(){trg.updateFrame();});");
194                // 2018/06/29 ADD END
195                js.append("</script>");
196
197                return js.toString();
198        }
199}