Investigating Code Snippet Technology

xiaoxiao2021-03-06  104

Download The Code.msi Sample File.

Understanding The Role of Code Snippets

Visual Studio 2005 and Visual C # Express Edition Both Support A Technology Dubbed Code Snippets, Which Are The Foundation for Two Related Code-Generation Techniques:

Expansion Templates: The ability of the IDE to generate type definitions, member definitions, and common coding constructs Surround With IntelliSense:. The ability of the IDE to surround a group of selected code statements within a relevant coding construct.

All in all, code snippets technology exists for a single reason-developer productivity Expansion templates and Surround With IntelliSense enable developers to rapidly generate blocks of code using any of the following approaches.:

The Edit | IntelliSense Menu of The Ide Ide-Dependent Keyboard Shortcuts Context-Sensitive Mouse Right-Click Typing A Snippet's Registered Shortcut (as defined by a snippet's xml file)

An Expansion Template EXAMPLE

To illustrate the role of Expansion templates, assume you have created a C # class type (named SportsCar) and wish to quickly add a property with a related backing field. Rather than manually declaring the member variable and authoring the property syntax by hand, you can Activate The Property Expansion (Using Any of The Previously Mentioned Techniques) That Automates The entire process.

For example, place the mouse cursor within the scope of the type definition and select the Edit | IntelliSense | Insert Expansion menu item From the resulting list of choices, select property as shown in Figure 1 below..

Figure 1. Activating a code Expansion

Once this code snippet Has Been ActiVated, you will find the following property / field declaration within the sportscar type:

Private int myvar; public int myProperty

{

Get {return myvar;}

Set {myvar = value;}

}

As you can see, the expansion is intentionally generic. However, if you look a bit closer at the expansion in Figure 2, you will notice that the property and field each have their name and type highlighted in yellow rectangles.

Figure 2. Yellow highlighting is your cue to edit the skeleton expansion.

..................... ..

Edits cascade throughout the expansion as required. Thus, if you Tab to the field's type (int by default) and change it to a string, the property's type declaration is updated automatically when you Tab off the item. Likewise, if you Tab to the Field name (MyVar by default) and update it to _carcolor, The Get and set scopes of the associated property is updated Automatical AS Shown in Figure 3.

Figure 3. Sorting out the details of the 'Property' Code Expansion

A surround with intelliSense Example

Surround With IntelliSense is another example of code snippet technology. Like Expansion templates, Surround With IntelliSense technology also results in code generation. The distinction, however, is that Surround With IntelliSense based code snippets allows you to select a block of code statements before applying the Expansion.

To illustrate, assume you now wish to place the SportsCar type into a region using the C # #region / #endregion directives First, select the code statements to be wrapped within the region and activate the Edit |. IntelliSense | Surround With ... menu Selection (Fighe 4).

Figure 4. Selecting Statements for Surround With IntelliSenseFrom The Resulting List, Select #Region and Edit The Region's Name As you see FIT USING TAB COMPLETION.

Note Surround With IntelliSense Can Be Used Without Selecting A Set of Statements. If this is the code, the code snippet is simply generated verbatim.

A catalog of code snippets

Visual Studio 2005 and Visual C # 2005 Express Edition Define The Same Intrinsic Code Snippets. The Following Table Documents Some (But Not All) Common Surround with code snippets.

Surround With SnippetDefinition # if #regionThese options allow you to wrap code with various C # preprocessor directives do while foreach for -. Iteration by index Reverse for -. Iteration by index These options wrap code within various C # looping constructs if statement else Wraps a code block within if / else logic.lock Wraps a code block within a lock scope.namespace Wraps a collection of types (or namespaces) into a new namespace. Try Catch Try Finally Wraps a block of code within structured exception logic.UsingWraps a statement inside a C # 'Using Scope' to Ensure Object Disposal.

The next Table Documents Some (But not all) of the expansion templates provided by The Visual Studio 2005 And Visual C # 2005 Express Edition IDES.

Expansion Template SnippetDefinitionclass interface enum structThese options generate an empty type definition. Named Iterator / Indexer pairIterators are constructs that allow you to build custom collections that expose their sub-objects using an array like index syntax. Basic attribute implementation Creates a new custom .NET attribute definition (according to best practices) .Destructor Stubs out a C # destructor (which is an overridden System.Object.Finalize in disguise) .Exception typeCreates a definition for a custom exception (according to best practices). Override System.Object.EqualsOverrides the virtual Equals () method inherited from System.Object. Property PropertygDefines a read / write property (property) or read-only property definition (propertyg) .Now, despite what you may be thinking, the remainder of this article will not illustrate the use Of Each Code Snippet. I am Quite Sure You Are Able To Investigate Each Option At Your Leisure.

What I will do is examine how a code snippet is composed under the hood. Once this is understood, you will learn how to create and register your own custom code snippets for use by Visual Studio 2005 and Visual C # 2005 Express Edition.

Locating Predefined Code Snippet Files

If you have been working with the .NET platform for any length of time, it should come as no surprise that code snippets are represented using XML (these days, what is not expressed as XML). Each code snippet is stored in a unique * .xml file, located by Default Under:

: / Program Files / Microsoft Visual Studio 8 / VC # / Expansion / 1033 / Expansions

Note This path is based on beta versions of Visual Studio 2005 / Visual C # 2005 Express Edition. If you are unable to locate the Expansions folder as listed above, simply search your machine for a file namedclass.xml. Assuming the name of this file has NOT CHANGED, You Should Be in Good Shape.

Code Snippets under the hood

. Now that you have (hopefully) located the * .xml files that represent the default code snippets, let's have a look at the details Open the class.xml file, which is responsible for inserting a new class definition:

class </ title></p> <p><Shortcut> Class </ Shortcut></p> <p><Description> Expansion Snippet for Class </ Description></p> <p><Snippettypes></p> <p><Snippettype> Expansion </ snippettype></p> <p><Snippettype> surroundswith </ snippettype></p> <p></ Snippettypes></p> <p></ Header></p> <p><Snippet></p> <p><Declarations></p> <p><Literal default = "true"></p> <p><Id> name </ id></p> <p><Tooltip> Class Name </ Tooltip></p> <p><Default> myclass </ default></p> <p></ Literal></p> <p></ Declarations></p> <p><Code language = "csharp" format = "cdata"></p> <p><! [Cdata [Class $ NAME $</p> <p>{</p> <p>$ SELECTED $$ End $</p> <p>}]]></p> <p></ Code></p> <p></ Snippet></p> <p></ CODESNIPPET></p> <p>The root element for any code snippet is appropriately named <Codesnippet>. Within this element scope area two key sub-elements: <header> and <snippet>.</p> <p>The <header> ELEMENT</p> <p>The <Header> element is used to describe the basic characteristics of the code snippet itself. The following table documents the key sub-elements of <Header>. <Header> Sub-ElementDefinition <Title> The display title for the code snippet. < Shortcut> Defines the shortcut for the code snippet. in the IDE, type in the shortcut name to select the snippet followed by the Tab. If you type a partial shortcut name ( 'cla' rather than 'class' for example), you will need to Tab twice;.. once to complete the named expansion and again to insert the snippet <Description> A human-readable description of the snippet, which is displayed when selecting a snippet from the IDE <SnippetType> Specifies which category the code snippet belongs to (Expansion, SurroundsWith or Refactoring). Do note that a single code snippet may belong to more than once group. Microsoft IDEs use this value to determine which context menu will be used to display the code snippet.</p> <p>Just to make sure you understand how these sub-elements are used within Visual Studio, activate the #region code snippet. Now, try to identify where the <Title>, <Description>, and <SnippetType> values ​​are realized within Figure 5 ( Open the related pp_region.xml file if you need a hint.</p> <p>Figure 5. The <title>, <description> and <snippettype> Elements in action.</p> <p>Note Code Snippet Technology Is The Foundation for Refactoring Support Withnin A Microsoft IDE. To Learn More About Refactoring Technology, Consult The Following</p> <p>Article.</p> <p>The <snippet> ELEMENT</p> <p>The <Snippet> sub-element is a bit more complex than <Header>, given that this is where the real action happens In a nutshell, the <Snippet> element defines the following information:. The set of 'variables' to be generated By The Expansion. THESE ARE THE YELLOW-Highlight TOKENS EXPLAINED EARLIER IN The ARTICLE. The C # Skeleton Code To Be generated overce the expansion is actid.</p> <p>The <Snippet> element uses the <Declarations> element to account for the expansion variables, each of which is represented by unique <Literal> element. Consider again the class.xml code snippet. This expansion needs only a single <Literal> to represent THE CLASS NAME:</p> <p><Declarations></p> <p><Literal default = "true"></p> <p><Id> name </ id></p> <p><Tooltip> Class Name </ Tooltip></p> <p><Default> myclass </ default></p> <p></ Literal></p> <p></ Declarations></p> <p>.</p> <p><Literal> Sub-ElementDefinition <ID> This value represents the name used to identify the variable in the code expansion. <ToolTip> This value is used by the IDE to display a tool tip when the mouse cursor is hovering over a yellow-highlighted Variable. <default> Specifier The Default Value for the code snippet variable.</p> <p>Again, Just to Be Sure You CAN Map There Elements to the IDE, Ponder Figure 6, Which Shows The Result of The Class Snippet Expansion.</p> <p>Figure 6. The <Tooltip> and <default> Elements in action</p> <p>The <code> Element</p> <p>Finally, we need to address the role of the <Code> sub-element defined within <Snippet>. This element is used to define the code to be inserted, represented by a CDATA section (recall that CDATA sections are used to defined data that Is Not Well-Formed XML, But Still Required By THE XML Document). Here is The <code> Element for the class.xml code snippet: <code language = "csharp" format = "cdata"></p> <p><! [Cdata [Class $ NAME $</p> <p>{</p> <p>$ SELECTED $$ End $</p> <p>}]]></p> <p></ Code></p> <p>Notice the various names sandwiched by $ tokens (such as $ name $). This syntax is used to reference the variables defined within a <Literal> element, as well as predefined literals understood by Visual Studio 2005 and Visual C # 2005 Express Edition (such AS $ SELECTED $).</p> <p>Also note that the $ name $ token is referring to the name <Literal> defined in the <Declarations> section of the class.xml document. The $ selected $ token represents the code statements selected within the IDE before the expansion was activated. As You Would Guess, $ END $ IS Used to Mark Where The User's Cursor Will Be Placed After The Snippet Has Expanded.</p> <p>Building Custom Code Snippets</p> <p>If you are an XML superstar, the previous section should be about all you need to define your custom code snippets (of course, I have not yet described how to register custom code snippets with Visual Studio, so keep reading). However, for those Of you wish to see Some Concrete Examples, Allow Me To Present a Few Custom Snippets Example Your Workday Productivity To Some Extent:</p> <p>The New Type Instant Expansion The New Web Method Expansion The Disposable Type Expansion</p> <p>If you wish to quickly leverage these code snippets, simply copy and paste the following XML documents into an appropriately named * .xml file. The final section of this article will explain how to register custom code snippets with the Visual Studio 2005 and Visual C # 2005 Express Edition IDs.building The New Type Instant Expansion</p> <p>The First Code Snippet We Will Create, Newobj, Is Responsible for Generating The Following C # Statement (Shown In Pseudo-Code):</p> <p>[TypeName] = new [typename] ();</p> <p>The [typeName] placeholder will be controlled by a <Literal> aptly named typename and will have a default value of object. The [varName] placeholder will be controlled by another <Literal> named varname, which has the default value of 'obj' ...................</p> <p>Figure 7. The Newobj Code Snippet in Action</p> <p>Create a new xml docuument (newobj.xml) and enter the folowing:</p> <p><? XML Version = "1.0" encoding = "UTF-8"?></p> <p><CODESNIPPET FORMAT = "1.0.0"></p> <p><HEADER></p> <p><Title> New Object </ Title></p> <p><Shortcut> Newobj </ Shortcut></p> <p><Description> Expansion Snippet To Allocate a New Type</p> <p></ Description></p> <p><Snippettypes></p> <p><Snippettype> Expansion </ snippettype></p> <p></ Snippettypes></p> <p></ Header></p> <p><Snippet></p> <p><Declarations></p> <p><Literal></p> <p><ID> typename </ id></p> <p><Tooltip> Type Name </ Tooltip></p> <p><Default> Object </ default></p> <p></ Literal></p> <p><Literal></p> <p><Id> varname </ id></p> <p><Tooltip> Variable Name </ tooltip></p> <p><Default> Obj </ default></p> <p></ Literal></p> <p></ Declarations></p> <p><Code language = "csharp" format = "cdata"> <! [Cdata [$ TYPENAME $ VARNAME $ = New $ TYPENAME $ ($ END $);]]></p> <p></ Code></p> <p></ Snippet></p> <p></ CODESNIPPET></p> <p>. The current version of newobj.xml generates code that triggers the type's default (no argument) constructor One enhancement you could make is to add a third <Literal> that represents a C-style code comment:</p> <p><Literal></p> <p><ID> CTORARGS </ ID></p> <p><Tooltip> Type in Your CTOR ARGS </ Tooltip></p> <p><Default> / * type args here * / </ default></p> <p></ Literal></p> <p>We could the update Our CData Section As SO:</p> <p><Code language = "csharp" format = "cdata"></p> <p><! [Cdata [$ TYPENAME $$ VarName $ = New $ Typename $ ($ CTORARGS $ $ END $);]></p> <p></ Code></p> <p>The End Result Is That We Have PROVIDED A TAB-REACHABLE PLACEHOLDER WHERE CONSTRUCTOR ARGUMENTS Can Be Manually Entered Like The One Shown in Figure 8.</p> <p>Figure 8. The Improved Newobj Code Snippet</p> <p>Building the New WebMMETHOD EXPANSION</p> <p>When you are building XML Web Services using the .NET platform, the almighty [WebMethod] attribute does a vast majority of the hard work. As you know, each public method adorned with [WebMethod] can be reached using HTTP requests and return its value Like XML Data REPRESENTATION. In The SimpleSt Format, [WebMethod] Takes No Arguments:</p> <p>[WebMethod]</p> <p>Public Sportscar getNewcar ()</p> <p>{</p> <p>Return new sportscar ();</p> <p>}</p> <p>However, The [WebMethod] Attribute Can take A Number of Optional Named Parameters, One of Which IS Description:</p> <p>[WebMethod (Description = "this method returns a new sportscar"]</p> <p>Public Sportscar getNewcar ()</p> <p>{</p> <p>Return new sportscar ();</p> <p>}</p> <p>Given this, our next code snippet, newWM, will account for the Description named parameter, as well as <Literal> elements that represent the method's return value and name Figure 9 shows newWM in action:. Figure 9. The newWM code snippet in action</p> <p>If You Followed The Logic Behind The Previous Code Snippet, The Contents of NewWebMethod.xml Should Be straightforward:</p> <p><? XML Version = "1.0" encoding = "UTF-8"?></p> <p><CODESNIPPET FORMAT = "1.0.0"></p> <p><HEADER></p> <p><Title> New Web Method </ Title></p> <p><Shortcut> NewWM </ Shortcut></p> <p><Description> Expansion Snippet to Generate A New WebMM</p> <p></ Description></p> <p><Snippettypes></p> <p><Snippettype> Expansion </ snippettype></p> <p></ Snippettypes></p> <p></ Header></p> <p><Snippet></p> <p><Declarations></p> <p><Literal></p> <p><ID> RetVal </ ID></p> <p><Tooltip> Return Value </ Tooltip></p> <p><Default> void </ default></p> <p></ Literal></p> <p><Literal></p> <p><ID> MethodName </ ID></p> <p><Tooltip> Method Name </ Tooltip></p> <p><Default> MyWebmethod </ default></p> <p></ Literal></p> <p><Literal></p> <p><ID> DESC </ ID></p> <p><Tooltip> Description of Web Method </ Tooltip></p> <p><Default> this is my web method </ default></p> <p></ Literal></p> <p></ Declarations></p> <p><Code language = "csharp" format = "cdata"></p> <p><! [Cdata [</p> <p>[WebMethod (Description = "$ desc $")]]</p> <p>Public $ RETVAL $ MethodName $ ()</p> <p>{</p> <p>$ END $</p> <p>}</p> <p>]]]]</p> <p></ Code></p> <p></ Snippet></p> <p></ CODESNIPPET></p> <p>Note to Compile the Source Code generated by the</p> <p>NewWM Expansion, Your Project Will Need To Reference The System.Web.Services.dll Assembly. The C # Source Code File Will Also Need To Specify It Isusing To</p> <p>System.Web.Services namespace.</p> <p>Building the disposable type expansion</p> <p>Code snippets can expand to as much code as you feel is necessary. For example, the Exception Type expansion generates an entire class deriving from System.ApplicationException that conforms to .NET best practices. The Basic attribute implementation code snippet expands to a new System. Attribute Derived Type (Also Constructed Acording to .Net Best Practices).</p> <p>In a similar vein, we will wrap up our examination of building an Expansion template that is responsible for building a disposable type As you may know, when you are creating a type that interacts with unmanaged resources, best practice dictates that you.:</p> <p>. Implement the IDisposable interface The object user should (ideally) call the defined Dispose () method to release any unmanaged resources Override System.Object.Finalize:. If the object user forgets to call the Dispose () method, the CLR garbage collector will Call The Type's Destructor.</p> <p>The DispClass Code Snippet Will Build Such A Type, Shown ActiVated In Figure 10.</p> <p>Figure 10. The Dispclass Code Snippet in Action</p> <p>Here is The XML, Which I Would Guess Requires No Comment At this point.</p> <p><? XML Version = "1.0" encoding = "UTF-8"?></p> <p><CODESNIPPET FORMAT = "1.0.0"></p> <p><HEADER></p> <p><Title> Disposable class </ title></p> <p><Shortcut> Dispclass </ Shortcut></p> <p><Description> Expansion Snippet for a Disposable Class</p> <p></ Description></p> <p><Snippettypes></p> <p><Snippettype> Expansion </ snippettype></p> <p></ Snippettypes> </ header></p> <p><Snippet></p> <p><Declarations></p> <p><Literal></p> <p><Id> classname </ id></p> <p><Tooltip> Class Name </ Tooltip></p> <p><Default> MyResourceWrapper </ default></p> <p></ Literal></p> <p></ Declarations></p> <p><Code language = "csharp" format = "cdata"></p> <p><! [CDATA [CLAS $ ClassName $: IDISPOSABLE</p> <p>{</p> <p>~ $ ClassName $ ()</p> <p>{</p> <p>Cleanup (false);</p> <p>}</p> <p>Public void dispose ()</p> <p>{</p> <p>Cleanup (TRUE);</p> <p>Gc.suppressFinalize (this);</p> <p>}</p> <p>Protected void cleanup (bool disposing)</p> <p>{</p> <p>IF (Disposing)</p> <p>{</p> <p>// free Other State (Managed Objects).</p> <p>}</p> <p>// free your owN state (unmanaged objects).</p> <p>// set large fields to null.</p> <p>}</p> <p>}]]></p> <p></ Code></p> <p></ Snippet></p> <p></ CODESNIPPET></p> <p>About $ SELECTED $</p> <p>Each of the expansions we have create, beelong to the expansion snippet category:</p> <p><Snippettypes></p> <p><Snippettype> Expansion </ snippettype></p> <p></ Snippettypes></p> <p>If the <SnippetType> element is set to SurroundsWith (rather than Expansion), the code snippet appears within the Surround With context menu. Recall that Surround With code snippets typically make use of a predefined literal named $ selected $, which represents the statements selected In The Code Editor.</p> <p>For the sake of illustration, here is a simple example of using the $ selected $ token to wrap selected code statements in a defining scope (note that this snippet belongs to the Expansion and SurroundsWith categories):</p> <p><? XML Version = "1.0" encoding = "UTF-8"?></p> <p><CODESNIPPET FORMAT = "1.0.0"></p> <p><HEADER></p> <p><Title> Dummy Scope </ Title></p> <p><Shortcut> DScope </ Shortcut></p> <p><Description> Surrounds SELECTED CODE WITHIN A DUMMY SCOPE</p> <p></ Description> <snippettypes></p> <p><Snippettype> Expansion </ snippettype></p> <p><Snippettype> surroundswith </ snippettype></p> <p></ Snippettypes></p> <p></ Header></p> <p><Snippet></p> <p><Code language = "csharp" format = "cdata"></p> <p><! [Cdata [</p> <p>{</p> <p>$ SELECTED $ END $</p> <p>}]]></p> <p></ Code></p> <p></ Snippet></p> <p></ CODESNIPPET></p> <p>Again, this example is purely illustrative given that we do not gain much by grouping code within a dummy scope. Nevertheless, when you are building custom code snippets, remember that the $ selected $ token allows you to capture the current selection within the IDE .</p> <p>Registering Custom Code Snippets</p> <p>Now That WE Have A Set of Custom Code Snippets, We need to advertise. Luckily for us, Visual Studio 2005 and Visual C # 2005 Express Edition Share Common Approaches to do so.</p> <p>First Of All, You Could To Simply Copy Your * .xml Files To The Directory Containing The Out of the Box Code Expansion Files As Shown In Figure 11.</p> <p>Figure 11. Registering Custom Code Snippets in The Default Expansions Folder</p> <p>IF You Follow this Approach, You Will See Your Custom CODE SNIPPPEAR ALPHABETICLY WITHIN THE Related Context Menu, Shown in Figure 12.</p> <p>Figure 12. Accessing Our code Snippets through intelliSense</p> <p>The other approach is to create a specific folder for your code snippets (for example, C: / MySnips) and register this location using the Tools | Code Snippet Manager menu selection Once activated, you are presented with a dialog box like Figure 13 that. Allows You to Specify Alternative Folders (Using The Add Button) That Contain Your XML Documents.</p> <p>Figure 13. Registering a CUSTOM SNIPPet location</p> <p>Note As suggested by Figure 13, the <Header> element may contain an <Author> sub-element to document the author of the snippet.With this approach, you will now need to select the folder from the IDE's context menu as shown in Figure 14.</p> <p>Figure 14. SELECTING THE CUSTOM LOCATION</p> <p>Once You Do, You Will See your snippets Appear as expected.</p> <p>Figure 15. SELECTING THE CUSTOM SNIPPET (in The Custom location)</p> <p>Now that you have learned the basic lay of the land, you should be in a good position to build any code expansion you may require. As a logical next step, read over the snippets that ship with your Microsoft IDE to further your understanding. Happy Coding!</p> <p>Summary</p> <p>Visual Studio 2005 and Visual C # 2005 Express Edition each ship with a set of predefined code snippets. Snippets are authored using a small set of XML elements and are used to quickly generate code for common tasks. In addition to leveraging the supplied code snippets, developers .</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-124227.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="124227" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.064</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = 'rZRpiu_2FZfgUfVgn4wkxZKBz_2Fm1l8VRBIYTMHtPP3BaXNV1n4jJDZwAjkM7MQ9xVnHFnxnesjC8wGr3Q4ewmw0Q_3D_3D'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>